Imagine I have an entity like this.
public class Person{
Long Id,
String name,
String city,
Long age
//getters, setters, constructor
}
When I create a repository and output using GET request for an entry for city is null, below is my json response.
{
"name": "jon",
"age": 34
}
But I want this instead.
{
"name": "jon",
"city": null,
"age": 34
}
i.e. showing null attributes.
What is the easiest work around?
Ensure that you don't have the following configuration in your ObjectMapper:
mapper.setSerializationInclusion(Include.NON_NULL);
If you have it, remove it or change to Include.ALWAYS.
Also check your application.properties. If you're using Spring Boot 1.3, the serialization inclusion is configured via the spring.jackson.serialization-inclusion property.
Jackson 2.7 and Spring Boot 1.4 uses a property named spring.jackson.default-property-inclusion.
Ensure that the value of such properties is non_null.
Alternativaly, annotate your class as follows:
@JsonInclude(Include.ALWAYS)
public class Person {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With