We are using Spring MVC 3.0 (http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd) for our REST based services and one of the response types we have is JSON(produces = "application/json"). Currently, when we have any null fields in a JSON response, we are still writing null , which we want to exclude. I have looked up some posts where we can annotate @JsonSerialize on pojo and also some examples where we can use spring message-converters elements under "annotation-driven" element of spring mvc.
I particularly liked "message-convertes" approach like
But this element is available from Spring MVC 3.1 onwards, but not in Spring MVC 3.0.
Is there any equivalent solution in Spring MVC 3.0 ? or is a custom JsonSerializer the only solution to skip nulls
I tried to apply following changes as suggested at this, however I still see the nulls being included in the response. My changes are as below
However, we still see nulls in response. I enabled DEBUG in jackson and spring core and below is what I noticed
DEBUG 2014-11-24 17:56:15,056 [localhost-startStop-1] org.springframework.beans.BeanUtils findEditorByConvention No property editor [org.codehaus.jackson.map.annotate.JsonSerialize$InclusionEditor] found for type org.codehaus.jackson.map.annotate.JsonSerialize$Inclusion according to 'Editor' suffix convention
DEBUG 2014-11-24 17:57:03,748 [tomcat] org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor writeWithMessageConverters Written [com.apps.generated.Person@3eabe00c[firstName=John, lastName=<null>, middleName=<null>]] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@a053345]
Unfortunately this was a pain in the ass to do before 3.1. You are on the right track you need to configure the object mapper to not include nulls and then set that object mapper on the HttpMessageConverter. The unfortunate thing about how things were in Spring 3.0 is that the easiest way to do this would have been to supply your own AnnotationMethodHandlerAdapter and set the object mapper on it. However in order for that to work you can not use <mvc:annotation-driven /> or its ignored. This means you would have to manually do everything that <mvc:annotation-driven /> did for you (which is a lot)
Yes, I upgraded to Spring 3.2.1 and used the Message-Converters under mvc:annotation-driven and ableto fix that issue of skipping null values. But there was another approach my lead suggested to me. He wanted to have @JsonSerialize annotation to be added to class level, while generating JAXB objects.
So, I added like below
The above custom binding added @JsonSerialize annotation to the Calendar object generated via JAXB. However, I could not add @JsonSerialize(include=JsonSerialize.Inclusions.NON_NULL). How can I add the include part in the generated JAXB objects. I know this is not spring related question, but thought of checking.
I tried something like below
as suggested in a stackoverflow post, but not successful in generating the @JsonSerialize(include=JsonSerialize.Inclusions.NON_NULL) annotation added.
and now successfully see @JsonSerialize annotation on Calendar class, but no Inclusions.NON_NULL