如何使用@JsonIgnore解决(com.fasterxml.jackson.databind)异常

时间:2018-05-17 13:36:01

标签: java hibernate hibernate-mapping

如果我没有在映射方法上使用@JsonIgnore注释,我在获取数据时会遇到此异常

at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)

请查看实体类

package com.rasvek.cg.entity;
// Generated May 14, 2018 11:39:07 PM by Hibernate Tools 5.1.7.Final

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * MasterCampusClass generated by hbm2java
 */
@Entity
@Table(name = "master_campus_class", catalog = "campus_guru_01")
public class MasterCampusClass implements java.io.Serializable {

    private Integer classId;
    private String className;
    private String classShortName;
@Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "class_id", unique = true, nullable = false)
    public Integer getClassId() {
        return this.classId;
    }

    public void setClassId(Integer classId) {
        this.classId = classId;
    }

    @Column(name = "class_name")
    public String getClassName() {
        return this.className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    @Column(name = "class_short_name")  
    public String getClassShortName() {
        return this.classShortName;
    }


//@JsonIgnore  
@OneToMany(fetch = FetchType.EAGER, mappedBy = "masterCampusClass")
    public Set<MasterCampusSection> getMasterCampusSections() {
        return this.masterCampusSections;
    }

    public void setMasterCampusSections(Set<MasterCampusSection> masterCampusSections) {
        this.masterCampusSections = masterCampusSections;
    }
}

如果我使用@JsonIgnore,当我保存MasterCampusSections时不接受json,由于@jsonIgnore注释,它没有收到json

我们不能在不使用@JsonIgnore注释的情况下解决上述异常吗?

如果可能请告诉我!谢谢。

1 个答案:

答案 0 :(得分:1)

您尝试做的似乎是维护父子实体引用。尝试使用@JsonManagedReference中的MasterCampusClass注释和@JsonBackReference中匹配字段的MasterCampusSection

但是,如果您需要一个引用JSON内部父类的显式值(如id),则必须在两端使用自定义序列化程序。 @JsonBackReference@JsonIgnore注释都会使其忽略自定义序列化程序。