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

Spring Data REST - POST new entity with relationships

与之相关:Spring Data Rest版本2.0.2

我正尝试通过与Man的@ManyToOne(而不是示例中的@OneToOne)关系来发布实体(地址),如以下解释:复杂对象图中的嵌入式实体引用,但出现杰克逊错误:<铅>

1
2
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对象,例如:

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

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

任何指针将不胜感激。

Stackoverflow建议我看这个问题,在我的情况下失败,出现HTTP状态405-不支持请求方法\\'POST \\'...这可能是配置问题,但我的原始问题仍然存在。 >

更新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,其行为是相同的。


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

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

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