在 Spring Data Rest 中使用自定义控制器将 URI 转换为实体? [英] converting URI to entity with custom controller in spring data rest?

查看:19
本文介绍了在 Spring Data Rest 中使用自定义控制器将 URI 转换为实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 jpa 实体.

i have an jpa entity like this.

@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 创建位置

i am using spring data rest i am able to create location with rest api by providing the following payload

{

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

现在我正在尝试编写将持久化实体的自定义控制器.这是我的自定义控制器

now i am trying to write my custom controller which will persist the entity. this is my custom controller

@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>

有人可以帮忙吗?

推荐答案

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

I am not sure why you are writing a custom controller however the issue would appear to be that you do not have a default no args constructor so Jackson cannot instantiate an instance.

这是因为您使用的是 Lombok 的 @Data 注释:

This is because you are using Lombok's @Data annotation:

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

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

You should also annotate you class with @NoArgsConstructor to have a default no-args constructor generated:

@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;
}

这篇关于在 Spring Data Rest 中使用自定义控制器将 URI 转换为实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆