JsonMappingException:已经有id的POJO [英] JsonMappingException: Already had POJO for id

查看:560
本文介绍了JsonMappingException:已经有id的POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用@JsonIdentityInfo jackson注释时出错。当我尝试反序列化对象时,我得到以下异常:

I have an error when trying to work with @JsonIdentityInfo jackson annotation. When I try to deserialize the object I get the folowing exception:


无法读取JSON:已经有id的POJO(java.lang。整数)[1](通过参考链:eu.cobiz.web.domain.Site [operators] - > eu.yavix.web.domain.Account [image] - > eu.cobiz.web.domain。 Image [@ Image]);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException 已经有id的POJO (java .lang.Integer)[1](通过引用链:eu.yavix.web.domain.Site [operators] - > eu.cobiz.web.domain.Account [image] - > eu.cobiz.web .domain.Image [@ Image])

Could not read JSON: Already had POJO for id (java.lang.Integer) [1] (through reference chain: eu.cobiz.web.domain.Site["operators"]->eu.yavix.web.domain.Account["image"]->eu.cobiz.web.domain.Image["@Image"]);nested exception is com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.Integer) [1] (through reference chain: eu.yavix.web.domain.Site["operators"]->eu.cobiz.web.domain.Account["image"]->eu.cobiz.web.domain.Image["@Image"])

我试图反序列化的JSON看起来像:

The JSON I am trying to deserialize looks like:

{
"@Site": 1,
"siteId": 1,
"name": "0",
"address": {
    "@Address": 2,
    "addressId": 4,
    "number": "22"
},
"operators": [
    {
        "accountId": 1,
        "email": "user982701361@yavix.eu",
        "image": {
            "@Image": 1,
            "imageId": 1,
            "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
        }
    },
    {
        "accountId": 2,
        "email": "user174967957@yavix.eu",
        "image": {
            "@Image": 2,
            "imageId": 2,
            "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
        }
    }
]
}

我的域对象注释为

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@Image")

问题出现在@Id注释上,因为如果我删除注释,问题就会消失(就像我对帐户所做的那样),但根据我的理解,新功能对循环依赖有用,这对我在其他场景中很有用。两个图像之间不应该存在冲突,因为它们是不同的对象。

The problem arises on @Id annotation since if I remove the annotation the problem disappears (as i did for account) but on my understanding the new feature is usefull for cyclic dependencies wich is usefull for me in other scenarios. There shouldn't be a conflict between the 2 images since they are differente objects.

是否有人知道如何解决或者问题是什么?

Does anybody have an idea on how to solve or what is the problem?

推荐答案

在注释ID时,应使用范围参数。然后解串器将确保id在范围内是唯一的。

You should use scope parameter when annotating the ids. Then the de-serializer would make sure the id is unique within the scope.

来自注释类型 JsonIdentityInfo


范围用于定义对象ID的适用性:所有ID在其范围内必须是唯一的;其中范围定义为此值与生成器类型的组合。

Scope is used to define applicability of an Object Id: all ids must be unique within their scope; where scope is defined as combination of this value and generator type.

例如。 @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,property =@ id,scope = Account.class)

这篇关于JsonMappingException:已经有id的POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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