2017-01-04 77 views
3

我有一些JPA机型:“类别”和“文章”:春杰克逊:集JSON动态忽略

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

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

如何动态设置字段忽略?

+0

欢迎来到Stack Overflow!我会想象这95%的代码与你的问题无关。请创建一个[**最小**,完整且可验证的示例](http://stackoverflow.com/help/mcve),以说明您的问题。 –

回答

2

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

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

这是一个link以MixIn为例,我作了另一个答案。

+0

好的,谢谢,我应该为每个实体创建DTO类,即使字段是平等的吗? –

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(); 
    } 
} 

现在,利用自定义序列把这个注解你的产品类别实体类以上。

@JsonSerialize(using = CategorySerializer.class) 
+0

然后我需要使用两个自定义序列化器 –

+0

如果您在文章类的序列化过程中有任何有条件的操作,那么您需要一个。请使用任何需要的自定义序列化程序 – Avinash