Spring Data REST - 发布具有关系的新实体 [英] Spring Data REST - POST new entity with relationships

查看:19
本文介绍了Spring Data REST - 发布具有关系的新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

涉及:Spring Data Rest 2.0.2 版

Relates to: Spring Data Rest version 2.0.2

我正在尝试使用@ManyToOne(而不是示例中的@OneToOne)关系向Person 发布一个实体(地址),如下所述:复杂对象图中的嵌入式实体引用 但我得到了一个杰克逊错误:

I'm trying to POST an entity (Address) with a @ManyToOne (instead of @OneToOne as in example) relationship to Person as explained in: Embedded Entity references in complex object graphs but I get a Jackson Error:

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

I find it strange that I can send a JSON object with rel and href in the "person" attribute of Address like:

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

这还有效吗?引用的页面很旧(2012).有没有更新的文档.我已经查看了最新(迄今为止)2.1.0 RC1 的参考文档,但我发现它缺乏.

Is this still valid? The refered page is quite old (2012). Are there any updated docs. I have reviewed the reference doc even for latest (to date) 2.1.0 RC1, but I find it lacking.

任何指针将不胜感激.

Stackoverflow 建议我查看这个问题,就我而言因 HTTP 状态 405 失败 - 不支持请求方法POST"...这可能是配置问题,但我的原始问题仍然存在.

Stackoverflow suggested me to see this question, which, in my case fail with a HTTP Status 405 - Request method 'POST' not supported ... which is probably a configuration issue, but my original question stands.

更新 1:参考文档第 4.1 节中说:

Update 1: From the Reference Doc section 4.1 it says:

有时,Spring Data REST 的 ObjectMapper 的行为可能无法正确处理您的域模型,它已被专门配置为使用可以将域对象转换为链接并返回的智能序列化程序.有很多方法可以构建您的数据,您可能会发现自己的域模型没有正确转换为 JSON.在这些情况下,尝试以通用方式支持复杂的域模型有时也不切实际.有时,根据复杂性,甚至不可能提供通用解决方案.

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.我会尝试进一步挖掘.

The problem could be that I'm using a custom Jackson ObjectMapper with Hibernate support. I'll try to dig further.

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

Update 2: Never mind update 1, I removed the Hibernate4Module and the behaviour is the same.

推荐答案

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

The format on the page you linked to was out of date. I just updated it. The correct format is:

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

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

At least, this is what works for me with my entities.

这篇关于Spring Data REST - 发布具有关系的新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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