用jackson将双向JPA实体序列化为JSON [英] Serialize bi-directional JPA entities to JSON with jackson

查看:711
本文介绍了用jackson将双向JPA实体序列化为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下类:

  import com.fasterxml.jackson.annotation。*; 
import javax.persistence。*;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
私人字符串名称;

@JsonManagedReference
@OneToMany(mappedBy =parent,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set< Child>儿童;

// Getters and setters
}

p>

  import com.fasterxml.jackson.annotation。*; 
import javax.persistence。*;
import java.util.HashSet;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
私人字符串名称;

@JsonBackReference
@ManyToOne
@JoinColumn(name =parentId)
私有父母;

//获得者和设定者
}

我是使用POJO映射从模型序列化到JSON。当我序列化一个Parent对象时,我得到以下JSON:

  {
id:1,
name:John Doe,
children:[
{
id:1,
name:child1
} ,{
id:2,
name:child2
}
]
}

但是当我序列化一个孩子时,我得到以下JSON:

 id:1,
name:child1
}

缺少对父项的引用。
有没有办法解决这个问题?

解决方案

我想你必须在@JsonIdentityInfo和@ JsonBackReference / @ JsonManagedReference。



我会去:@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property =id)在你的实体上,删除@ JsonBackReference / @JsonManagedReference对。



在要排除的字段上添加@JsonIgnore。


I'm using Jackson to serialize my JPA model into JSON.

I have the following classes:

import com.fasterxml.jackson.annotation.*;
import javax.persistence.*;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)
@Entity
public class Parent {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String name;

  @JsonManagedReference
  @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  private Set<Child> children;

  //Getters and setters
}

and

import com.fasterxml.jackson.annotation.*;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
@Entity
public class Child {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String name;

  @JsonBackReference
  @ManyToOne
  @JoinColumn(name = "parentId")
  private Parent parent;

  //Getters and setters
}

I'm using the POJO mapping to serialize from model to JSON. When I serialize a Parent object I get the following JSON:

{
  "id": 1,
  "name": "John Doe",
  "children": [
    {
      "id": 1,
      "name": "child1"
    },{
      "id": 2,
      "name": "child2"
    }
  ]
}

But when I serialize a Child I get the following JSON:

{
  "id": 1,
  "name": "child1"
}

The reference to the parent is missing. Is there a way to solve this?

解决方案

I think you have to chose between the @JsonIdentityInfo and the @JsonBackReference/@JsonManagedReference.

I would go with : @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id") on your entities, removes @JsonBackReference/@JsonManagedReference pairs.

And add @JsonIgnore on the fields you want to exclude.

这篇关于用jackson将双向JPA实体序列化为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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