3

I'm using apache-cxf 2.7.11 + jackson (codehaus) 1.9.13 + spring 3.5 in my REST services web-container. I was wondering what would be the best way to remove null value fields from REST responses.

For example:
My response is now like this:

{
 "name": "MyName",
 "age": 10,
 "address": null
}

I want my response to be like this (the address field has been removed):

{
 "name": "MyName",
 "age": 10
}

I've read about apache-cxf interceptors and filters here:

and wondering what is the best practice? is there any configurable setting that I can change instead of implementing my own filer or interceptor class?

I'm using beans.xml file for configuration, thus I'm looking on how to config it all here, where my beans are:

<bean id="jaxrsRestJacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
    <property name="mapper" ref="jaxrsRestJacksonObjectMapper"/>
</bean>


<jaxrs:server id="restContainer" address="/">
        <jaxrs:serviceBeans>

        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonProvider"/>
        </jaxrs:providers>

</jaxrs:server>

Cheers!

Ziv Levy
  • 1,904
  • 2
  • 21
  • 32

2 Answers2

5

Found it!
This is the answer I was looking for:

see the updated beans.xml file:

<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
    <property name="serializationInclusion" value="NON_NULL"/>
</bean>

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
    <property name="mapper" ref="jacksonObjectMapper"/>
</bean>
Ziv Levy
  • 1,904
  • 2
  • 21
  • 32
3

You could add

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) 

on top of your POJO class. This would ignore the null fields for that one class.

Or you could configure the ObjectMapper so it would apply globally

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

If you go with the latter, you can pass the ObjectMapper as a constructor arg to the JacksonJsonProvider or JacksonJaxbJsonProvider (whichever one you are currently using)


UPDATE

You could also use a ContextResolver as seen here, and register the ContextResolver like you would any other provider. This will work better more complicated configurations. You might want to do that instead. With the ContextResolver, you don't need to configure the ObjectMapper with the JacksonJsonProvider, but you still do need the JacksonJsonProvider.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • should this work recursively on objects? e.g. I have a list of Person class – Ziv Levy Jul 26 '15 at 12:45
  • Without testing, I'll just say that I would imagine so (at least with the ObjectMapper route). Have you tried it? – Paul Samsotha Jul 26 '15 at 12:50
  • yes it is working recursively on objects as expected. Your answer helped me find the exact values I had to set in my xml configuration file. I posted an answer with the xml file configuration. – Ziv Levy Jul 26 '15 at 14:07
  • You could also use a ContextResolver as seen [here](http://stackoverflow.com/a/28310779/2587435), and register the ContextResolver like you would any other provider. This will work better more complicated configurations. You might want to do that instead. I was going to add it to my answer but got lazy :-) With the ContextResolver, you don't need to configure the ObjectMapper with the JacksonJsonProvider, but you still do need the JacksonJsonProvider – Paul Samsotha Jul 26 '15 at 14:10
  • Another option is to just extend the JacksonJsonProvider and you can configure the ObjectMapper in the constructor. The less xml the better :-) – Paul Samsotha Jul 26 '15 at 14:21