2017-01-04 81 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