Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show null attributes json response spring data rest?

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?

like image 977
Supun Wijerathne Avatar asked Jul 21 '26 10:07

Supun Wijerathne


1 Answers

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 {
    ...
}
like image 137
cassiomolin Avatar answered Jul 24 '26 00:07

cassiomolin