Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore Null values in Post request body in Spring Boot

I am calling a REST service with POST verb. Post request body is constructed dynamically.In some cases few fields will be populated with null values.Even though I used my Request pojo with @JsonInclude(Include.NON_NULL) or @JsonInclude(JsonInclude.Include.NON_NULL) null value fields are not getting removed.

My sample request will be looks like below.

I am constructing requestEntity by myself and not parsing it before posting.

resetTemplate.exchange(uri,HTTP.POST,requestEntity,responseObject)

How do I remover in the request body fileds with null value

I am using Spring Boot 1.5.9 and it uses Jackson 2.x

like image 580
springbootlearner Avatar asked Jan 28 '23 18:01

springbootlearner


1 Answers

@JsonInclude(Include.NON_NULL) should have worked for you!

You seem to be constructing the request entity by yourself and not sure if you are passing on a JSON string with NON_NULL. Try this instead directly with entity if you have annotated with Jackson annotations.

restTemplate.postForEntity(uri, requestDto, ResponseDto.class);
like image 87
Karthik R Avatar answered Jan 31 '23 07:01

Karthik R