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

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

问题描述

相关:Spring Data Rest版本2.0.2

Relates to: Spring Data Rest version 2.0.2

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

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]

我发现奇怪的是我可以在地址的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.

任何指针都会受到赞赏。

Any pointer would be appreciated.

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 - POST关系的新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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