Search code examples
javajsonannotationsjackson

Json Ignore property only in specific method


I have a Class So as shown below:

@Entity
@Table(name = "so", schema = "public")
public class So implements java.io.Serializable , IBusiness {

    private int idSo;
    
    private Set<PartOrder> partOrders = new HashSet<PartOrder>();
    
    public So() {
        
    }

    @Id
    @SequenceGenerator(name = "seqGenerator", sequenceName = "so_seq", allocationSize=1, initialValue=1)
    @GeneratedValue(generator="seqGenerator", strategy=GenerationType.SEQUENCE)
    @Column(name = "id_so", unique = true, nullable = false)
    public int getIdSo() {
        return this.idSo;
    }

    public void setIdSo(int idSo) {
        this.idSo = idSo;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "so")
    public Set<PartOrder> getPartOrders() {
        return this.partOrders;
    }

    public void setPartOrders(Set<PartOrder> partOrders) {
        this.partOrders = partOrders;
    }


}

I have a method that returns a Json with the SO. But i don not want the partOrders attribute to be included on the response json. I was using @JsonIgnore on the attribute but now, i have another method that receives an SO object and i need to get the partOders.

The question is: Is there any way to specify when to ignore the attribute (in specific method) ?


Solution

  • You can achieve this by using Json Filter with a FilterProvider

    1. you use @JsonFilter annotation to assign a filter name to your POJO.
    2. before serialization, you attach an instance of SimpleBeanPropertyFilter to the filter name. the class has two factory methods for filters that work based on propertry names.

    Here is an example of the annotation declaration:

    @JsonFilter("filter properties by name")
    public class So {
        public String alwaysWrite = "alwaysWriteValue";
        public String somtimeIgnore = "somtimeIgnoreValue";
    }
    

    here is a method that assignes a filter that will ignore somtimeIgnore property

    public void dontWriteSomtimes(So so) {
        ObjectMapper mapper = new ObjectMapper();
        FilterProvider filters = new SimpleFilterProvider()  
          .addFilter("filter properties by name", SimpleBeanPropertyFilter.serializeAllExcept("somtimeIgnore"));  
        ObjectWriter writer = mapper.writer(filters);  
        try {
            System.out.println(writer.writeValueAsString(so));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    output:
    {"alwaysWrite":"alwaysWriteValue"}

    here is a method that will write everything: just assign a non-existent or empty property:

    public void writeEveryting(So so) {
        ObjectMapper mapper = new ObjectMapper();
        FilterProvider filters = new SimpleFilterProvider()  
          .addFilter("filter properties by name", SimpleBeanPropertyFilter.serializeAllExcept(""));  
        ObjectWriter writer = mapper.writer(filters);  
        try {
            System.out.println(writer.writeValueAsString(so));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    output:
    {"alwaysWrite":"alwaysWriteValue","somtimeIgnore":"somtimeIgnoreValue"}