springMVC,json视图,返回的json数据有null值,如何过滤掉不传递到前台,例如有些model,里面很多属性是null,我们希望过滤掉为null的属性,比如 a = null, b = "bb",传递到前台的只有{"b":"bb"}
spring MVC是通过jackson来进行序列化的,有两种方式可以解决
1通过注解的方式,在需要序列化的类上添加注解 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
第二种是通过配置的方式,
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prefixJson" value="false" />
<property name="supportedMediaTypes" value="application/json" />
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
这样就无需再每个类上单一注解了
spring MVC是通过jackson来进行序列化的,有两种方式可以解决
1通过注解的方式,在需要序列化的类上添加注解 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
第二种是通过配置的方式,
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prefixJson" value="false" />
<property name="supportedMediaTypes" value="application/json" />
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
这样就无需再每个类上单一注解了