Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude null properties from Spring Boot Rest controllers

I have a rest service exposed using spring boot rest controller but with the response i'm sending object's properties those has null values.

For ex : ReponseEntity.ok(list) and that list consist of Objects A with lot of null properties.

Is there an easy way of excluding those null properties with spring boot tools?

like image 373
dmj Avatar asked Jun 10 '26 18:06

dmj


1 Answers

You can try this in application.properties file

spring.jackson.default-property-inclusion=non_null

Ref - https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper

or you can try following annotation in class level or property level

@JsonInclude(JsonInclude.Include.NON_NULL)
like image 195
Tamil.S Avatar answered Jun 12 '26 08:06

Tamil.S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!