Java JsonIgnoreType-class And Method Code Example


Here is an example of using the JsonIgnoreType annotation from the Apache Commons IO library to ignore a specific class or interface during serialization to JSON:

import org.apache.commons.io.output.JsonIgnoreType;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonIgnoreType
public interface IgnoredType {
    int getValue();
}

@JsonSerialize
public class MyClass {
    private String field;
    private IgnoredType ignored;

    public MyClass(String field, IgnoredType ignored) {
        this.field = field;
        this.ignored = ignored;
    }
    
    public String getField() {
        return field;
    }
    
    public IgnoredType getIgnored() {
        return ignored;
    }
}

In this example, the IgnoredType interface is annotated with @JsonIgnoreType which will be ignored during serialization.

In the MyClass class, ignored field of IgnoredType is also ignored during serialization, because of the class level annotation on IgnoredType class.

Make sure that you are using jackson as serializer.