涉及: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/