java - How to ignore "null" or empty properties in json, globally, using Spring configuration -
i'm trying return properties have values, null ones being returned.
i know there's annotation ( @jsoninclude(include.non_null)
), need these in every single entity class.
so, question is: there way configure globally through spring config? (avoiding xml, preferably)
edit: seems question has been considered duplicate, don't think so. real question here how configure through spring config, couldn't find in other questions.
if using spring boot, easy as:
spring.jackson.serialization-inclusion=non_null
if not, can configure objectmapper in mappingjackson2httpmessageconverter so:
@configuration class webmvcconfiguration extends webmvcconfigurationsupport { @override protected void extendmessageconverters(list<httpmessageconverter<?>> converters) { for(httpmessageconverter converter: converters) { if(converter instanceof mappingjackson2httpmessageconverter) { objectmapper mapper = ((mappingjackson2httpmessageconverter)converter).getobjectmapper() mapper.setserializationinclusion(include.non_null); } } } }
Comments
Post a Comment