Spring Data REST - 发布具有关系的新实体

标签 spring rest jpa jackson spring-data-rest

涉及:Spring Data Rest 2.0.2 版

我正在尝试将具有@ManyToOne(而不是示例中的@OneToOne)关系的实体(地址)发布到Person,如下所述:Embedded Entity references in complex object graphs但我得到一个 jackson 错误:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: (was java.lang.NullPointerException) (through reference chain: Address["person"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: Address["person"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]

奇怪的是,我可以在 Address 的“person”属性中发送带有 rel 和 href 的 JSON 对象,例如:

"person": {
    "rel" : "people.Person",
    "href" : "http://localhost:8080/people/1"
}

这仍然有效吗?引用的页面很旧(2012)。 是否有任何更新的文档。我已经查看了最新(迄今为止)2.1.0 RC1 的引用文档,但我发现它缺乏。

任何指针将不胜感激。

Stackoverflow 建议我查看 this question ,在我的情况下失败,HTTP Status 405 - Request method 'POST' not supported ...这可能是一个配置问题,但我原来的问题是存在的。

更新 1:从引用文档第 4.1 节中说:

Sometimes the behavior of the Spring Data REST's ObjectMapper, which has been specially configured to use intelligent serializers that can turn domain objects into links and back again, may not handle your domain model correctly. There are so many ways one can structure your data that you may find your own domain model isn't being translated to JSON correctly. It's also sometimes not practical in these cases to try and support a complex domain model in a generic way. Sometimes, depending on the complexity, it's not even possible to offer a generic solution.

问题可能是我正在使用支持 Hibernate 的自定义 Jackson ObjectMapper。我会尝试进一步挖掘。

更新 2:没关系更新 1,我删除了 Hibernate4Module 并且行为是相同的。

最佳答案

您链接到的页面上的格式已过期。我刚刚更新了它。正确的格式是:

{
    "postalCode": "12345",
    "province": "MO",
    "lines": ["1 W 1st St."],
    "city": "Univille",
    "person": "http://localhost:8080/people/1"
}

至少,这对我的实体有用。

关于Spring Data REST - 发布具有关系的新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23767233/

相关文章:

java - 当我使用 @JsonProperty 通过 Spring 数据从 MongoDB 检索对象时出现空字段

java - Spring: Http406 这个请求标识的资源只能够

java - RESTful @FormParam 400 错误请求

java - 使用 jQuery 通过 POST 向 java 方法发送和接收数据

java - Spring JPA 映射 - 第一步

database - Java EE/JPA : Improve query performance - store UUID as binary

java - 从数据库接收数据时出现空指针异常

java - UriComponentsBuilder 是什么意思?

angular - 下拉列表不会填充 Angular 中的 API 数据

java - 了解延迟加载集合的大小