SpringmvcJSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
来源:互联网 发布:windows 内核漏洞 编辑:程序博客网 时间:2024/06/17 12:31
我们在使用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的自定义值.
转载自此地址
阅读全文
0 0
- SpringmvcJSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
- Springmvc前端JSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
- Springmvc前端JSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
- Springmvc前端JSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
- MappingJackson2HttpMessageConverter 分类别处理null
- MappingJackson2HttpMessageConverter转换器
- springBoot 定制HTTP消息转换器 返回对象只为null或者空值的字段处理
- BeanUtils 转换器将Long ,Integer等字段的默认值设置为null
- BeanUtils 转换器将Long ,Integer等数字类型的默认值设置为null
- BeanUtils 转换器将Long ,Integer等数字类型的默认值设置为null
- pig对null的处理(实际,对空文本处理为两种取值null或‘’)
- springMVC 对参数为null或参数不为null的处理
- oracle 对null值的处理
- count()函数对null值的处理
- FreeMarker 对null值的处理技巧
- oracle中对null值的处理
- GROUP FUNCTION对NULL值的处理
- FreeMarker 对null值的处理
- volatile与内存屏障
- angularjs ng-click获取当前元素
- XML教程02_用途
- 完整的配置一个Log4j
- 最大公约数递归函数
- SpringmvcJSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
- 线性表初始化、判空、插入、删除、遍历、修改、查值、合并
- TreeSet 覆盖避免
- 安卓中的线程
- u3d DontDestroyOnLoad
- angularJs中自定义指令replace属性详解
- 密码字典的生成
- VUE基础API总结
- HDU 4864 Task