关于前后端交互 json转换值为null的操作

SpringmvcJSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理n 空值

我们在使用springmvc中的 @ResponseBody 注解返回JSON时可以配置Json转换器如下:

 
  1. <!-- 请求信息转换器,负责读取和写入json格式的数据 -->

  2. <bean id="mappingJackson2HttpMessageConverter"

  3. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

  4. <property name="supportedMediaTypes">

  5. <list>

  6. <value>application/json;charset=UTF-8</value>

  7. </list>

  8. </property>

  9. </bean>

有时我们返回的字段存在null的情况我们需要对其进行处理,objectMapper提供了默认的几种处理方式配置如下:

 
  1. <bean id="mappingJacksonHttpMessageConverter"  

  2.         class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  

  3.         <property name="supportedMediaTypes">  

  4.             <list>  

  5.                 <value>application/json;charset=UTF-8</value>  

  6.             </list>  

  7.         </property>  

  8.         <property name="objectMapper">  

  9.             <bean class="com.fasterxml.jackson.databind.ObjectMapper">  

  10.                 <property name="serializationInclusion">  

  11.                     <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>  

  12.                 </property>  

  13.             </bean>  

  14.         </property>    

  15. </bean>  

这种是将字段为null的清理掉不在结果Json中显示出来,其余的还有以下几种配置:

        ALWAYS,  NON_NULL,   NON_DEFAULT, NON_EMPTY

有些特殊情况,我们不能将null字段直接去除,而是需要给予一个默认的值,则可以设置如下配置:

 
  1. <bean id="mappingJacksonHttpMessageConverter"  

  2.     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  

  3.     <property name="supportedMediaTypes">  

  4.         <list>  

  5.             <value>application/json;charset=UTF-8</value>  

  6.         </list>  

  7.     </property>  

  8.     <property name="objectMapper">  

  9.         <bean class="com.fasterxml.jackson.databind.ObjectMapper">  

  10.             <property name="serializerProvider">  

  11.                 <bean class="com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.Impl">  

  12.                     <property name="nullValueSerializer">  

  13.                         <bean class="cn.com.mx.gome.flash.component.GomeSearchJsonSerializer"></bean>  

  14.                     </property>  

  15.                 </bean>  

  16.             </property>  

  17.         </bean>  

  18.     </property>  

  19. </bean>  

自定义的null处理类如下;

 
  1. import java.io.IOException;  

  2. import java.util.ArrayList;  

  3. import java.util.List;  

  4.   

  5. import com.fasterxml.jackson.core.JsonGenerator;  

  6. import com.fasterxml.jackson.core.JsonProcessingException;  

  7. import com.fasterxml.jackson.databind.JsonSerializer;  

  8. import com.fasterxml.jackson.databind.SerializerProvider;  

  9.   

  10. public class GomeSearchJsonSerializer extends JsonSerializer<Object> {  

  11.   

  12.     @Override  

  13.     public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)  

  14.             throws IOException, JsonProcessingException {  

  15. //        jgen.writeString("");  

  16.         jgen.writeObject(new JsonNullDef());  

  17.     }  

  18.   

  19. }  

  20.   

  21. class JsonNullDef{  

  22.       

  23.     private List<String> def = new ArrayList<>();  

  24.   

  25.     public List<String> getDef() {  

  26.         return def;  

  27.     }  

  28.   

  29.     public void setDef(List<String> def) {  

  30.         this.def = def;  

  31.     }    

  32.       

  33. }  

大家可以根据自己的需求来设置null的自定义值.

https://blog.csdn.net/qq_37180608/article/details/72865109

前后端交互时,后端返回给前端是一个json,json中的值是由一个对象转换而来的,有时候该对象中可能某些字段的值是空,返回给前端的json就会出现某些key的value是空,在默写情况下不利于前端处理。

其实在后端返回时可以进行数据过滤,将对象是为空的字段自动过滤掉。一行代码的事情,在需要序列化为json输出的类上增加@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)  即可。

试试效果吧!

https://blog.csdn.net/libing06081227/article/details/57084363

猜你喜欢

转载自blog.csdn.net/Chen_629/article/details/81240085