Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How to exclude null values from JSON response using Spring MVC 3.0 specifically

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

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
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

application-servlet.xml


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]


What could be wrong here ?

 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)



You can see in the ticket below the workaround suggested was to right a bean post processor (yuck)
https://jira.spring.io/browse/SPR-7504

Anyways is there a reason you cannot upgrade Spring? There is a pretty well documented upgrade path, and would definitely be worth it to upgrade.
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill.

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.


Changed to


and now successfully see @JsonSerialize annotation on Calendar class, but no Inclusions.NON_NULL
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic