您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

杰克逊与Spring MVC复制嵌套对象不反序列化

杰克逊与Spring MVC复制嵌套对象不反序列化

这就是杰克逊设计JsonIdentityInfo批注逻辑的方式。

 * Annotation used for indicating that values of annotated type
 * or property should be serializing so that instances either
 * contain additional object identifier (in addition actual object
 * properties), or as a reference that consists of an object id
 * that refers to a full serialization. In practice this is done
 * by serializing the first instance as full object and object
 * identity, and other references to the object as reference values.

Jackson将第一次运行完整的序列化,只有第二次找到该对象时,才会对ID进行序列化。

因此,有两种解决方法

1)您可以简单地删除@JsonIdentityInfo批注,Jackson将按预期方式序列化对象,但将从响应中删除@id字段。这可能很好,因为您仍然会拥有’id’属性

2)我觉得您可以简单地重组对象并删除一些引用。我会说还是做这些更改是很好的。首先,您可以从UserLocation删除对State的引用。我要说的是,因为State附加在City上,所以不必在userLocation类中包含State。这样,您将可以从城市访问州,从而解决了您的问题。另外,我将从City类和State类中删除对userLocations列表的引用。

它看起来像:

UserLocation有城市,没有州。

城市有州,没有用户位置

州没有用户位置,也没有城市。

希望这可以帮助

Java 2022/1/1 18:28:26 有542人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶