4

According to third party API spec, I need to send null value in JSON using ObjectMapper if no value exists,

Expected results : "optional": null

If optional value exists, then send "optional": "value"

I didn't find such option in Jackson – Working with Maps and nulls

Code:

requestVO = new RequestVO(optional);
ObjectMapper mapper = new ObjectMapper();
String requestString = mapper.writeValueAsString(requestVO);

Class:

public class RequestVO {
   String optional;
   public RequestVO(String optional) {
      this.optional = optional;
   }

public String getOptional() {
    return optional;
}

public void setOptional(String optional) {
    this.optional= optional;
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233

2 Answers2

4

Add @JsonInclude(JsonInclude.Include.USE_DEFAULTS) annotation to your class.

@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
class RequestVO {
    String optional;

    public RequestVO(String optional) {
        this.optional = optional;
    }

    public String getOptional() {
        return optional;
    }

    public void setOptional(String optional) {
        this.optional = optional;
    }
}

Example :

RequestVO requestVO = new RequestVO(null);

ObjectMapper mapper = new ObjectMapper();
try {
    String requestString = mapper.writeValueAsString(requestVO);
    System.out.println(requestString);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

Output :

{"optional":null}

With value:

RequestVO requestVO = new RequestVO("test");

ObjectMapper mapper = new ObjectMapper();
try {
    String requestString = mapper.writeValueAsString(requestVO);
    System.out.println(requestString);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

Output:

{"optional":"test"}

You can use @JsonInclude annotation on even properties. So, this way you can either serialize as null or ignore some of the properties while serializing.

Emre Savcı
  • 3,034
  • 2
  • 16
  • 25
  • @user7294900 I updated the answer. It gives you an option to serializing some null properties and ignoring some other properties. – Emre Savcı Dec 16 '18 at 11:08
3

You can configure your ObjectMapper this way:

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

If no value is present on the JSON request your processed will have a null as you expected.

You can even configure a Spring bean for the ObjectMapper if you need.

EDIT:

I misunderstood the question, he was interested on the JSON response and not on the object parsed. The correct property is this case is JsonInclude.Include.USE_DEFAULTS.

Apologies for the confusion.

  • You mean `Include.NON_NULL`? it removes optional parameter – Ori Marko Dec 16 '18 at 10:54
  • Yes, in this case it will only process the value if is present, and will default to `null` otherwise. But reading again I think I misunderstood the question. You expect that the JSON response always had the field correct? Is not about processing the request. – Táizel Girão Martins Dec 16 '18 at 10:55
  • If you use this setting, it does not serialize as OP's ask. It does not serialize null values. Take a look at serialized string, it contains nothing when you pass Include.NON_NULL. – Emre Savcı Dec 16 '18 at 11:05
  • You are right @EmreSavcı I misunderstood the objective and already edited the question to reflect this and apologize for my confusion. – Táizel Girão Martins Dec 16 '18 at 11:12