SpringmvcJSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理n 空值
我们在使用springmvc中的 @ResponseBody 注解返回JSON时可以配置Json转换器如下:
-
<!-- 请求信息转换器,负责读取和写入json格式的数据 -->
-
<bean id="mappingJackson2HttpMessageConverter"
-
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
-
<property name="supportedMediaTypes">
-
<list>
-
<value>application/json;charset=UTF-8</value>
-
</list>
-
</property>
-
</bean>
有时我们返回的字段存在null的情况我们需要对其进行处理,objectMapper提供了默认的几种处理方式配置如下:
-
<bean id="mappingJacksonHttpMessageConverter"
-
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
-
<property name="supportedMediaTypes">
-
<list>
-
<value>application/json;charset=UTF-8</value>
-
</list>
-
</property>
-
<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>
这种是将字段为null的清理掉不在结果Json中显示出来,其余的还有以下几种配置:
ALWAYS, NON_NULL, NON_DEFAULT, NON_EMPTY
有些特殊情况,我们不能将null字段直接去除,而是需要给予一个默认的值,则可以设置如下配置:
-
<bean id="mappingJacksonHttpMessageConverter"
-
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
-
<property name="supportedMediaTypes">
-
<list>
-
<value>application/json;charset=UTF-8</value>
-
</list>
-
</property>
-
<property name="objectMapper">
-
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
-
<property name="serializerProvider">
-
<bean class="com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.Impl">
-
<property name="nullValueSerializer">
-
<bean class="cn.com.mx.gome.flash.component.GomeSearchJsonSerializer"></bean>
-
</property>
-
</bean>
-
</property>
-
</bean>
-
</property>
-
</bean>
自定义的null处理类如下;
-
import java.io.IOException;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import com.fasterxml.jackson.core.JsonGenerator;
-
import com.fasterxml.jackson.core.JsonProcessingException;
-
import com.fasterxml.jackson.databind.JsonSerializer;
-
import com.fasterxml.jackson.databind.SerializerProvider;
-
-
public class GomeSearchJsonSerializer extends JsonSerializer<Object> {
-
-
@Override
-
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
-
throws IOException, JsonProcessingException {
-
// jgen.writeString("");
-
jgen.writeObject(new JsonNullDef());
-
}
-
-
}
-
-
class JsonNullDef{
-
-
private List<String> def = new ArrayList<>();
-
-
public List<String> getDef() {
-
return def;
-
}
-
-
public void setDef(List<String> def) {
-
this.def = def;
-
}
-
-
}
大家可以根据自己的需求来设置null的自定义值.
https://blog.youkuaiyun.com/qq_37180608/article/details/72865109
前后端交互时,后端返回给前端是一个json,json中的值是由一个对象转换而来的,有时候该对象中可能某些字段的值是空,返回给前端的json就会出现某些key的value是空,在默写情况下不利于前端处理。
其实在后端返回时可以进行数据过滤,将对象是为空的字段自动过滤掉。一行代码的事情,在需要序列化为json输出的类上增加@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 即可。
试试效果吧!
https://blog.youkuaiyun.com/libing06081227/article/details/57084363