【问题标题】:Spring and Jackson: set json ignore dynamicallySpring和Jackson:动态设置json忽略
【发布时间】:2017-05-19 08:02:41
【问题描述】:

我有一些 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 篇文章,但我不需要类别中的“childrenList”和“parent”字段。 2)在第二种方法中,我需要获得相同但序列化“父”字段。

我该如何解决这个问题? 如果我将对这些字段使用@JsonIgnore 注释,那么它们将永远不会被序列化。 还是我应该使用 DTO 类?

如何动态设置忽略字段?

【问题讨论】:

标签: java json spring spring-mvc jackson


【解决方案1】:

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

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

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

【讨论】:

  • 好的,谢谢,我是否应该为每个实体创建 DTO 类,即使字段相等?
【解决方案2】:

使用自定义序列化器,伪代码如下。

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)

【讨论】:

  • 那我需要使用两个自定义序列化器
  • 如果你在Article类的序列化过程中也有条件操作,那么你需要一个。请在需要时使用自定义序列化程序
猜你喜欢
  • 2013-03-24
  • 2011-05-09
  • 1970-01-01
  • 2022-01-01
  • 2011-06-28
  • 2012-12-29
  • 2017-05-25
  • 1970-01-01
  • 2014-08-23
相关资源
最近更新 更多