JSON反序列化和循环依赖

时间:2014-12-29 16:42:12

标签: json rest jersey jackson gson

当我通过Jersey Rest Service发布时,我的下面的JSON没有被反序列化。当用Google搜索时,我发现存在循环依赖性导致此问题。

您能否告诉我如何解决此问题。

{
"asset":{
    "id":"123456",
    "price":"12.9",
    "quantity":"1",
    "asset":[
        {
            "id":"907",
            "price":"123"
        }
    ]
  }
}

这是我用于WS的映射和发布数据的Java类。

 public class Asset {

 private String id;
 private String price;
 private String quantity;
 private List<NestedAssset> asset = new ArrayList<NestedAsset>();
  -  --
 getters and setters follows
 }


 public class NestedAsset{
    private String id;
    private String price;
    -  --
    getters and setters follows
 }

当这个json映射到这些pojo并发送给POST请求时,看起来像使用服务无法反序列化这些对象

2 个答案:

答案 0 :(得分:1)

您需要为Jackson定义@JsonIdentity注释以解决周期性依赖关系:

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="childId", scope = Child.class)

示例应用程序:

public class CyclicalDependencyMain {

    public static void main(String [] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        String json = objectMapper.writeValueAsString(generateParent());
        System.out.println(json);

        Parent parent = objectMapper.readValue(json, Parent.class);
        System.out.println(parent);

    }

    public static Parent generateParent() {
        Parent parent = new Parent();
        Child child = new Child();

        child.setChildId(1);
        child.setParent(parent);

        parent.setParentId(1);
        parent.setChild(child);

        return parent;
    }
}

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="parentId", scope = Parent.class)
class Parent {
    private int parentId;
    private Child child;

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public Child getChild() {
        return child;
    }

    public void setChild(Child child) {
        this.child = child;
    }

    @Override
    public String toString() {
        return "Parent{" +
                "parentId=" + parentId +
                ", child=" + child +
                '}';
    }
}

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="childId", scope = Child.class)
class Child {
    private int childId;
    private Parent parent;

    public int getChildId() {
        return childId;
    }

    public void setChildId(int childId) {
        this.childId = childId;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "Child{" +
                "childId=" + childId +
                '}';
    }
}

输出:

{"parentId":1,"child":{"childId":1,"parent":1}}
Parent{parentId=1, child=Child{childId=1}}

更新基于@JsonManagedReference@JsonBackReference

的解决方案
class Parent {
    private int parentId;
    @JsonManagedReference
    private Child child;
    // getters/setters and everything else
}

class Child {
    private int childId;
    @JsonBackReference
    private Parent parent;
    // getters/setters and everything else
}

如果您始终将Parent类作为顶级类使用,则此方法也可以。如果您希望将Child序列化和/或反序列化为顶级类,那么这种方法将无效。

答案 1 :(得分:0)

使用@JsonIgnore而不是@XmlTransient来打破循环依赖。