This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jersey - Serialize and drop null properties  RSS feed

 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • 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.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • 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: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!