Spring和Jackson:动态设置json忽略

时间:2017-01-04 21:51:27

标签: java json spring spring-mvc jackson

我有一些JPA模型:"类别"和"文章":

@Entity
@Table(name = "categories")
public class Category {
private int id;
private String caption;
private Category parent;
private List<Category> childrenList;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Column
public String getCaption() {
    return caption;
}

public void setCaption(String caption) {
    this.caption = caption;
}

@ManyToOne
@JoinColumn(name = "parent_id")
public Category getParent() {
    return parent;
}

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

@OneToMany
@JoinColumn(name = "parent_id")
public List<Category> getChildrenList() {
    return childrenList;
}

public void setChildrenList(List<Category> childrenList) {
    this.childrenList = childrenList;
}
}



@Entity
@Table(name = "articles")
public class Article {
private int id;
private String caption;
private boolean isAvailable;
private String description;
private int price;
private Category category;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Column
public String getCaption() {
    return caption;
}

public void setCaption(String caption) {
    this.caption = caption;
}

@Column(name = "is_available")
@Type(type = "org.hibernate.type.NumericBooleanType")
public boolean getIsAvailable() {
    return isAvailable;
}

public void setIsAvailable(boolean available) {
    isAvailable = available;
}

@Column
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@Column
public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

@ManyToOne
@JoinColumn(name = "category_id")
public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}
}

我还有一些REST控制器有两种方法: 1)在第一种方法中,我需要获取并序列化最后10篇文章,但我不需要&#34; childrenList&#34;和父母&#34;在Categegory的领域。 2)在第二种方法中,我需要得到相同但序列化的父母&#34;字段。

我该如何解决这个问题? 如果我将@JsonIgnore注释用于这些字段,那么它们将永远不会被序列化。 或者我应该使用DTO课程吗?

如何动态设置字段以忽略?

2 个答案:

答案 0 :(得分:3)

我从不使用我的实体来生成JSON,我认为从长远来看,另一套DTO课程会让你更快乐。我的DTO通常有一个构造函数,它将Entity作为参数(如果你计划用它来解析传入的JSON,它仍然需要一个默认的构造函数)。

如果你真的想使用你的实体,我建议你使用MixIns,它允许你注册一个MixIn类,它增加了特定类的序列化。

这是我为另一个答案做的MixIn示例的link

答案 1 :(得分:0)

使用自定义序列化程序,psedo代码如下。

public class CategorySerializer extends StdSerializer<Category> {

    public CategorySerializer() {
        this(null);
    }

    public CategorySerializer(Class<Category> t) {
        super(t);
    }

    @Override
    public void serialize(Category value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        // put the logic here to write the parent and child value or not

        // here is the example to how the data is serialized
        jgen.writeStartObject();
        jgen.writeNumberField("id", value.id);
        jgen.writeStringField("caption", value.caption);

        jgen.writeEndObject();
    }
}

现在,要使用自定义序列化程序,请将此注释放在Catagory实体类之上。

@JsonSerialize(using = CategorySerializer.class)