Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

rest - JsonMappingException: Already had POJO for id

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

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"])

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": "[email protected]",
        "image": {
            "@Image": 1,
            "imageId": 1,
            "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
        }
    },
    {
        "accountId": 2,
        "email": "[email protected]",
        "image": {
            "@Image": 2,
            "imageId": 2,
            "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
        }
    }
]
}

My domain object is annotated with

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

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 useful for cyclic dependencies which is useful for me in other scenarios. There shouldn't be a conflict between the 2 images since they are different objects.

How can I solve this or what is the problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

From Annotation Type JsonIdentityInfo:

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.

e.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...