背景:
使用@responseBody设置以json格式返回数据时候. 有时候被返回的对象有些属性是null值, 默认还是会输出. 例如下面代码. 在与移动端交互时候会很浪费流量.
{
"fpassword" : "sssssssss",
"favator" : "",
"fbirthday" : null,
"fcredType" : null,
"fcredid" : null,
"fregistedTime" : null,
"fstate" : 1,
"flstate" : 1,
"fstatusMask" : 0,
"fstatusMask1" : 0,
"fcreateTime" : 1443260277000,
"fmodifyTime" : 1443260277000,
"fstandby0" : null,
"fstandby1" : null,
"fstandby2" : null,
"fstandby3" : null,
"fstandby4" : null,
"fstandby5" : null,
"fstandby6" : null,
"fpassFlag" : 1,
"fquestion1" : null,
"fanswer1" : null,
"fquestion2" : null,
"fanswer2" : null,
"fregDeviceId" : null,
"fregClientIp" : null,
"fregChannel" : null,
"fpassModifyTime" : null
}
有两种方法设置不返回null值属性.
1. 在被返回的对象例如User类, 添加注解@JsonInclude(Include.NON_NULL)即可. 在spring4.1.6, jackson-databind 2.5.1版本亲测有效
spring使用的是fasterxml.jackson组件解析对象. 因此依赖一下包..
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
问题来了!! 第二种方法:
2. spring mvc配置文件,
查看API文件, 发现com.fasterxml.jackson.databind.ObjectMapper有一下方法, 而JsonInclude.Include枚举类有个值: NON_NULL(感觉应该和方法1的注解是同一个),
public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) {
_serializationConfig = _serializationConfig.withSerializationInclusion(incl);
return this;
}
因此对jackson的objectMapper设置属性
<property name="serializationInclusion">
<!-- 注入枚举类型 -->
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
最后就是这样:
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<!-- <bean class="com.threeStepTech.ObjectMapper.CustomObjectMapper"/> -->
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<property name="serializationInclusion">
<!-- 注入枚举类型 -->
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
但测试过还是无效.....
也尝试了自己写子类继承com.fasterxml.jackson.databind.ObjectMapper.
public class CustomObjectMapper extends ObjectMapper {
private static final long serialVersionUID = 3072523733092288622L;
public CustomObjectMapper() {
super.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
super.getSerializationConfig().withSerializationInclusion(
JsonInclude.Include.NON_NULL);
}
}
然后注入到上诉代码被注释的地方替换com.fasterxml.jackson.databind.ObjectMapper.. 还是无用....
求助哪位大神能帮忙解答一下.... 明显第二种方法优于第一种..... 但苦于无效...
------解决思路----------------------
思路覆盖默认的MappingJacksonHttpMessageConverter。
重载MappingJacksonHttpMessageConverter,提供配置排除null值:
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean class="org.codehaus.jackson.map.ObjectMapper">
<beans:property name="serializationInclusion">
<util:constant
static-field="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_NULL" />
</beans:property>
</beans:bean>
</beans:property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>