springboot 将null字段输出为空串

package spring.resultConfig;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;

@Configuration
public class ResultConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
---------------------
作者:ZChao_Smile
来源:CSDN
原文:https://blog.csdn.net/richangbiji/article/details/79433088
版权声明:本文为博主原创文章,转载请附上博文链接!

 

springmvc处理返回的JSON数据中把NULL替换成空或者0的问题

 

由于公司人手不足,因此也就担任起来了前后端的开发,当然只是简单的,目前太高大上的东西我也弄不好,好了,废话不多说,直接进入正题吧
  
直接上例子:
1.需求:
  前端现在需要用户的个人信息:
  {
    userName:”张三”,
    age:20,
    address:XX市XX区
  }
2.后端返回的是:
  {
    userName:”张三”,
    age:null,
    address:null
  }
从上面看,很明显我们不希望出现null的情况,希望的是如果age没有的情况下返回0,address是空的字符串(即:”“)但是这里让前端去判断或者转换的话又很费劲,于是就在后端返回JSON的时候统一处理一下。用的方法有两种,先来说一下第一种:
1)在返回JSON之前获取JSON的所有的key,判断key是否为NULL,如果是的话,那么就把它设置成”“,然后再返回,用的springmvc中默认的Jackson,具体用法如下:
<1>.先配置一个bean:

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<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="com.resource.util.json.TransferJsonSerializer"></bean>
</property>
</bean>
</property>
</bean>
</property>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
<2>.自定义一个类用来处理为NULL的字段:

public class TransferJsonSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeString("");
}
}
1
2
3
4
5
6
7
关于这个自定义的类,里面你希望怎么处理NULL就怎么处理,我这里只是一个例子。看另一种:
2)用的是阿里的fastjson,配置如下:

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<array>
<!-- 下面配置,默认是false-->
<value>WriteMapNullValue</value>
<value>WriteNullNumberAsZero</value>
<value>WriteNullListAsEmpty</value>
<value>WriteNullStringAsEmpty</value>
<value>WriteNullBooleanAsFalse</value>
<value>WriteDateUseDateFormat</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
上面配置字面意思一看就懂,好了,一切都结束了,测试结果如下:
前端接收到的:

{
  userName:"张三",
  age:0,
  address:""
}
---------------------
作者:ZChao_Smile
来源:CSDN
原文:https://blog.csdn.net/richangbiji/article/details/78816902
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2018-12-12 11:45  全栈冲鸭!  阅读(1982)  评论(0编辑  收藏  举报