Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Jackson json two way object reference

I have a pair of objects like

public class Obj1 {
    public int id;
    public String name;
    public Obj2 obj2;
}

public class Obj2 {
    public int id;
    public String name;
    public List<Obj1> obj1list;
}

I want to be able to convert this to Json via Jackson. I found the JsonManagedReference and JsonBackReference and annotated them but when you do that, the serialization only works in one way. It will only show when the class with the JsonManagedReference side is serialized.

If I serialize an "Obj1" I want to see the "Obj2" that is attached to it. And if I serialize the "Obj2" I want to see the list of "Obj1"s that is attached to it.

I also tried using JsonIdentityInfo annotation like so

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")

and this seems to work except that it adds the "id" value of the parent object into the subobject (or every subobject in the list case) which is a bit odd. Though I guess I can live with it.

Is there a way to get this to behave as I expect?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You are looking at @JsonIgnoreProperties, it will give what is needed and avoid json recursion.

public class Obj1 {
    public int id;
    public String name;
    @JsonIgnoreProperties("obj1list")
    public Obj2 obj2;
}

public class Obj2 {
    public int id;
    public String name;
    @JsonIgnoreProperties("obj2")
    public List<Obj1> obj1list;
}


UPDATE

I always perfers @JsonIgnoreProperties over JsonBackReference and JsonManagedReference. As it not only does the trick, but also not escapes any data serialization (which is the case required here).

I also have a sample project on github deals with this situation. Look for entity class codes School.java & District.java. Its an mvn spring-boot executable code, if you wanna run a test.

From Javadoc, starting with Jackson 2.0, @JsonIgnoreProperties annotation can be applied both to classes and to properties. If used for both, actual set will be union of all ignorals: that is, you can only add properties to ignore, not remove or override. So you can not remove properties to ignore using per-property annotation.


HOW IT WORKS

When you define @JsonIgnoreProperties at propety level, while serialization/deserization it will work on the refered property object.

So here, when Obj1 is being parsed, you asked parser to ignore obj1list property of obj2. And similary, while parsing Obj2 it will ignore contained obj2 references in the Obj collection. So your parsing will look like below:

Obj1 : {
    id : int,
    name : string,
    Obj2 : {
     id : int,
     name : string
     obj1list : //ignored avoids recursion
    }
}

Obj2 : {
    id : int,
    name : string,
    obj1list : [{
     id : int,
     name : string,
     obj2 : //ignored avoids recursion
    },
    {
     id : int,
     name : string
     obj2 : //ignored avoids recursion
    }
    ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...