首页 文章

JsonMappingException:已经有id的POJO

提问于
浏览
14

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

无法读取JSON:已经有id(java.lang.Integer)[1]的POJO(通过引用链: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 [ “@图片”])

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

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

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

2 回答

  • 6

    要避免id冲突,请尝试使用 ObjectIdGenerators.PropertyGenerator.classObjectIdGenerators.UUIDGenerator.class 而不是 ObjectIdGenerators.IntSequenceGenerator.class

  • 20

    注释id时应使用 scope 参数 . 然后,反序列化器将确保id在范围内是唯一的 .

    从注释类型 JsonIdentityInfo

    范围用于定义对象标识的适用性:所有标识在其范围内必须是唯一的;其中scope被定义为此值和生成器类型的组合 .

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

相关问题