• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jersey - Serialize and drop null properties

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to use Jersey for a RESTful web service in a pattern such as the following:



On serialization, I get a body back that includes null properties such as:



What I really want, though is for the null properties to be removed, such as:



Is there a simple way to enable this in Jersey? I found one post that indicated I should set the annotation "@XmlElement(nillable=true)" on every property I want removed, but this would involve adding an annotation to thousands of properties over hundreds of POJOs. I have believe there is a way to enable this feature on the server, or at least the method, level. In short, I'm looking for a solution that does not involve modifying the POJOs.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's been a while since I've worked in this area, but we had something like this in our code:


I haven't tested it, so not sure if it works.

Here's the documentation of JsonInclude http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html#NON_NULL.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks useful, but our resources are extending the io.dropwizard.Application class, which doesn't appear to have such options. Jersey is being launched via drop wizard on our server.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's my understanding that as long as this class makes it to the classpath of the application, it will be picked up and initialized. However, I don't enough of your application to be sure about it.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on JacksonHowToCustomSerializers you need to add a custom serializer using the Module interface.

In this thread someone else tries to modify the serializing process using dropwizard & jackson. And the last post also suggest using a Jackson module.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone! You've steered me to the right place. I just needed the following in my DropWizard start up:



It's a bit like a needle in a haystack, so thanks for the help!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic