【问题标题】:converting URI to entity with custom controller in spring data rest?在spring data rest中使用自定义控制器将URI转换为实体?
【发布时间】:2017-09-21 22:23:08
【问题描述】:

我有一个这样的 jpa 实体。

@Entity
@Table(name = "location")
@Data
public class Location {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "LOCATION_ID", unique = true)
    @NotEmpty(message = "Please Enter Location ID")

    private String name;

    @Column(name = "LOCATION_DESCRIPTION")
    @NotEmpty(message = "Please Enter Location Description")
    private String description;

    @ManyToOne
    @NotNull(message = "Please Choose a Building")


    Building building;



    @Version
    Long version;





}

和这样的存储库。

public interface LocationRepository extends PagingAndSortingRepository<Location, Long> {  
    Location findByName(@Param("name") String name);

}

我正在使用 spring data rest 我可以通过提供以下有效负载来使用 rest api 创建位置

{

"name":"adminxxxxx","description":"adminxxx" , "building": "http://localhost:8080/buildings/2"
}

现在我正在尝试编写将保留实体的自定义控制器。这是我的自定义控制器

@ExposesResourceFor(Location.class)
@RepositoryRestController
@BasePathAwareController
public class LocationController {

    @Autowired
    LocationRepository locationDao;

    @Autowired
    LocationResourceAssembler resourceAssembler;

    @Value("${buildings.error.messages.uniqueconstraintviolation}")
    String uniqueConstrainMessage;
    static final String TAG = LocationController.class.getSimpleName();

    @RequestMapping(value="locations",method = org.springframework.web.bind.annotation.RequestMethod.POST)
    public ResponseEntity<?> save(@RequestBody @Valid Location location) {

        try {
            location = locationDao.save(location);
            LocationResource b = resourceAssembler.toResource(location);
            return ResponseEntity.ok().body(b);
        } catch (DataIntegrityViolationException e) {

            if (locationAlreadyExists(location.getName()))
                throw new LocationAlreadyExistException(uniqueConstrainMessage, location);

            else
                throw new RuntimeException("Some Error Occured");
        }

    }

我收到了这个错误

exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.alamdar.model.Building: no String-argument constructor/factory method to deserialize from String value (&#39;http://localhost:8080/buildings/2&#39;)
 at [Source: java.io.PushbackInputStream@5d468b16; line: 3, column: 60] (through reference chain: com.alamdar.model.Location[&quot;building&quot;])</div></body></html>

有人可以帮忙吗?

【问题讨论】:

标签: spring spring-data-rest


【解决方案1】:

我不确定您为什么要编写自定义控制器,但问题似乎是您没有默认的无参数构造函数,因此 Jackson 无法实例化实例。

这是因为你使用的是 Lombok 的 @Data 注解:

https://projectlombok.org/features/Data.html

您还应该使用 @NoArgsConstructor 注释您的类以生成默认的无参数构造函数:

@Entity
@Table(name = "location")
@Data
@NoArgsConstructor
public class Location {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "LOCATION_ID", unique = true)
    @NotEmpty(message = "Please Enter Location ID")

    private String name;

    @Column(name = "LOCATION_DESCRIPTION")
    @NotEmpty(message = "Please Enter Location Description")
    private String description;

    @ManyToOne
    @NotNull(message = "Please Choose a Building")
    Building building;

    @Version
    Long version;
}

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 2018-01-19
    • 2016-02-07
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2015-03-08
    • 2015-10-03
    相关资源
    最近更新 更多