I have a spring bean for ObjectMapper, and it has a configuration:
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
I have class with a map inside:
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class MyClass {
private Map<String, Object> value = new HashMap<>();
}
and Map looks like:
{
"value" : {
"a": null,
"b": "notnull",
"c" : [ {
"c1" : null
}, {
"c2" : "abc"
} ]
}
}
when I called rest endpoint, the response looks like:
"value": {
"b": "notnull",
"c": [
{},
{
"c2": "abc"
}
]
}
}
I tried to update config specific for my class:
JsonInclude.Include nonDefaultInclude = JsonInclude.Include.ALWAYS;
JsonInclude.Value includeValue = JsonInclude.Value.construct(nonDefaultInclude, nonDefaultInclude);
mapper.configOverride(MyClass.class).setIncludeAsProperty(includeValue);
but it is not working, whether it works, if I do something like:
JsonInclude.Include nonDefaultInclude = JsonInclude.Include.ALWAYS;
JsonInclude.Value includeValue = JsonInclude.Value.construct(nonDefaultInclude, nonDefaultInclude);
mapper.configOverride(Map.class).setIncludeAsProperty(includeValue);
I also tried to use 1st approach and put @JsonInclude on the map inside - no luck
if I will use 2nd approach, then I will override all Maps in the project - no way to do that.
Are the any way to achieve my goal?
You can control the Jackson ObjectMapper behavior from your
application.propertiesfile. To always include fields, regardless of their value, set the following configuration value:Docs: Customize the Jackson ObjectMapper