Spring and Jackson: set json to dynamically ignore

I have several JPA models: Category and Article:

@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;
}
}

I also have some REST controller with two methods: 1) In the first method, I need to get and serialize the last 10 articles, but I don’t need a “childList” and a “parent” field in Categegory. 2) In the second method I need to get the same, but serialize the “parent” field.

How can i solve this? If I use the @JsonIgnore annotation for these fields, they will never be serialized. Or should I use DTO classes?

How can I dynamically set a field to ignore?

+4
2

Entity JSON, , DTO . DTO , Entity ( , JSON).

, MixIns, MixIn, .

MixIn, .

+2

, 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)
0

Source: https://habr.com/ru/post/1665791/


All Articles