2

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?

1
  • this is strange. Anyway you should be able in configuring the ObjectMapper by properly setting the serializationInclusion property Commented Jul 19, 2018 at 11:26

2 Answers 2

8

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 {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

I think you should check to json annotation JsonInclude.Include and set it to ALWAYS : https://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.