2

I have a POJO class which has the annotation @JsonInclude(Include.NON_NULL) for the class.

@JsonInclude(Include.NON_NULL)
@Getter
@Setter
public class Attribute implements Serializable {
    private String type;
    private List<Object> value;
    private String unit;
    private List<Object> metaInfo;
}

and controller looks like this.

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
  public Attribute getSingle() {
      return getAttribute(); // getAttribute() will get the object of type Attribute.
  }

Now, I have a requirement where based on the value of type field, need to return null for value field in the final JSON generated.

For example, if type='XYZ' the response will be { type:'XYZ'}. And if type='ABC', then response should be { type:'ABC',value:null}

How can I achieve this in Spring boot? Whats the best way to handle these kind of scenarios?

Santosh Hegde
  • 3,420
  • 10
  • 35
  • 51
  • please refer to this question https://stackoverflow.com/questions/35672131/how-to-add-jsonignore-annotated-fields-in-serializing-in-jackson-obectmapper/35672470#35672470 – ChenZhou Dec 17 '19 at 08:25

1 Answers1

0

Can this help ?

if (value.getClass() == Integer) {
      return null;
}
amer
  • 1,528
  • 1
  • 14
  • 22