1

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) ?

nquincampoix
  • 508
  • 1
  • 4
  • 17
LuGaNO
  • 99
  • 1
  • 2
  • 11

2 Answers2

2

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"}

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

you put jSON ignore annotation for your class. i think it will overcome your requirement. you can leave unwanted properties here.

@JsonIgnoreProperties(ignoreUnknown = true)
class ClassName{

}
Kumaresan Perumal
  • 1,926
  • 2
  • 29
  • 35
  • kumaresan thanks for your response. Unfortunatly putting @JsonIgnoreProperties isn't a solution for as both methods are on the same class. As i mentioned before i want on method to take into account the attribute and another method to ignore it. Thanks though for your response – LuGaNO Nov 17 '15 at 19:27
  • you make second method value as null, then when you need, make it again.please. – Kumaresan Perumal Nov 17 '15 at 19:29
  • yes the null worked fine. I don't know if it's the most elegant solution but it can solve the fire. Thanks! – LuGaNO Nov 19 '15 at 15:58