'Exclude inner class fields using JsonInclude.Include.NON_DEFAULT
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
I have a nested class structure as follows:
public class A{
String z;
String y;
float p;
List<B> b;
public static class B{
String x;
String w;
float q;
List<C> c;
public static class C{
String v;
String u;
float r;
}
}
}
I have another class as follows:
public class D{
List<B> b;
}
In my use case, I am returning class D as part of an API call. String x(in class B) and String v(in class C) will always be non-null. But String w and String u may or may not be null. I want to exclude null fields and default fields ( like 0.0 for float q and float r) from my response and therefore want to use @JsonInclude(JsonInclude.Include.NON_DEFAULT). But I don't want this to happen for all use cases, but only for my use case, i.e. only for class D? Is the following an effective way of achieving it?
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class D{
List<B> b;
}
Will the above way work on the inner classes as well? Please feel free to suggest another method because I'm unable to think of one.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
