从apache cxf REST响应中删除空值字段 [英] remove null value field from apache cxf REST response
问题描述
我正在使用 apache-cxf 2.7.11
+ jackson(codehaus)1.9.13
+ <$我的REST服务Web容器中的c $ c> spring 3.5 。我想知道从REST响应中删除 null
值字段的最佳方法是什么。
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
}
我读过关于 apache-cxf
拦截器和过滤器:
I've read about apache-cxf
interceptors and filters here:
- http://cxf.apache.org/docs/jax-rs-filters.html
- http://cxf.apache.org/docs/jax-rs-data-bindings.html
并想知道什么是最佳做法?是否有任何可配置的设置我可以更改而不是实现我自己的文件管理器或拦截器类?
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?
我正在使用 beans.xml
配置文件,因此我正在研究如何在这里配置它,我的bean是:
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>
干杯!
推荐答案
找到它!
这是我要找的答案:
Found it!
This is the answer I was looking for:
查看更新的 beans.xml
file:
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>
这篇关于从apache cxf REST响应中删除空值字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!