【问题标题】:Fasterxml - How to exclude an object from json file?Fasterxml - 如何从 json 文件中排除对象?
【发布时间】:2014-04-15 09:15:41
【问题描述】:

我已将我以JSON 格式发送的实体映射到服务。这是我的实体

@Entity
@Table(name = "company")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Company implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column
    private String name;

    @OneToMany(mappedBy = "company")
    @Cascade(value = CascadeType.ALL)
    private Collection<Employee> employees;

我的员工类

@Entity
@Table(name = "employee")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;

    @Column
    String name;

    @ManyToOne()
    @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private Company company;

但我的 json 格式不合适。

{
    "id": 1,
    "name": "Tim",
    "company": {
        "id": 1,
        "name": "Microsoft",
                        "employees": [1, {
                            "id": 5,
                            "name": "Jack",
                            "company": 1
                        }, {
                            "id": 6,
                            "name": "Jack",
                            "company": 1
                        }, {
                            "id": 7,
                            "name": "Jack",
                            "company": 1
                        }, {
                            "id": 8,
                            "name": "Tommy",
                            "company": 1
                        }]
    }
}

但就像我说的,我不需要“公司”中的“员工”对象。如何在我的JSON 文件中排除它?

【问题讨论】:

  • 你可以试试@JsonIgnore注解。它应该有助于解决这个问题。

标签: java json hibernate jackson fasterxml


【解决方案1】:

您可以使用the Jackson's bi-directional references 将“员工”对象从“公司”中排除。这是一个基于您的代码的示例,用于演示该方法:

public class JacksonReferences {

    @JsonIdentityInfo(generator = ObjectIdGenerators.None.class, property = "id")
    static public class Company {
        public Integer id;
        public String name;
        @JsonManagedReference
        public Collection<Employee> employees;

        public Company(Integer id, String name, Collection<Employee> employees) {
            this.id = id;
            this.name = name;
            this.employees = employees;
        }
    }

    @JsonIdentityInfo(generator = ObjectIdGenerators.None.class, property = "id")
    static public class Employee {
        public Integer id;
        public String name;
        @JsonBackReference
        public Company company;

        public Employee(Integer id, String name, Company company) {
            this.id = id;
            this.name = name;
            this.company = company;
        }
    }


    public static void main(String[] args) throws IOException {
        Company company1 = new Company(1, "Microsoft", new ArrayList<Employee>());
        Company company2 = new Company(2, "Google", new ArrayList<Employee>());

        Employee employee1 = new Employee(1, "John", company1);
        company1.employees.add(employee1);
        Employee employee2 = new Employee(2, "Tim", company1);
        company1.employees.add(employee2);
        Employee employee3 = new Employee(3, "Bob", company2);
        company2.employees.add(employee3);

        ObjectMapper mapper = new ObjectMapper();

        System.out.println("JSON for company #1:");
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(company1));
        System.out.println("JSON for employee #1:");
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee1));
        System.out.println("JSON for company #2:");
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(company2));
    }
}

输出是:

JSON for company #1:
{
  "id" : 1,
  "name" : "Microsoft",
  "employees" : [ {
    "id" : 1,
    "name" : "John"
  }, {
    "id" : 2,
    "name" : "Tim"
  } ]
}
JSON for employee #1:
{
  "id" : 1,
  "name" : "John"
}
JSON for company #2:
{
  "id" : 2,
  "name" : "Google",
  "employees" : [ {
    "id" : 3,
    "name" : "Bob"
  } ]
}

【讨论】:

  • 没有@JsonIdentityInfo 的相同输出。
猜你喜欢
  • 1970-01-01
  • 2017-03-25
  • 2021-03-24
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 2014-10-15
  • 2021-08-10
相关资源
最近更新 更多