springboot中返回值json中null转换空字符串

在实际项目中,我们难免会遇到一些无值。当我们转JSON时,不希望这些null出现,比如我们期望所有的null在转JSON时都变成“”“”这种空字符串,那怎么做呢?

Jackson中对null的处理

 1 @Configuration
 2 public class JacksonConfig {
 3     @Bean
 4     @Primary
 5     @ConditionalOnMissingBean(ObjectMapper.class)
 6     public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
 7         ObjectMapper objectMapper = builder.createXmlMapper(false).build();
 8         objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
 9             @Override
10             public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
11                 jsonGenerator.writeString("");
12             }
13         });
14         return objectMapper;
15     }
16 }

fastjson

使用fastjson需要导入依赖(https://mvnrepository.com/search?q=fastjson)

1 <dependency>
2     <groupId>com.alibaba</groupId>
3     <artifactId>fastjson</artifactId>
4     <version>1.2.58</version>
5 </dependency>

使用fastjson时,对null的处理和Jackson有些不同,需要继承WebMvcConfigurationSupport类,然后覆盖configureMessageConverters方法。在方法中,我们可以选择要实现null转换的场景,配置好即可。

 1 import com.alibaba.fastjson.serializer.SerializerFeature;
 2 import com.alibaba.fastjson.support.config.FastJsonConfig;
 3 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.http.MediaType;
 6 import org.springframework.http.converter.HttpMessageConverter;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 8 
 9 import java.nio.charset.Charset;
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 @Configuration
14 public class fastJsonConfig extends WebMvcConfigurationSupport {
15 
16     /**
17      * 使用阿里 fastjson 作为 JSON MessageConverter
18      * @param converters
19      */
20     @Override
21     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
22         FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
23         FastJsonConfig config = new FastJsonConfig();
24         config.setSerializerFeatures(
25                 // 保留 Map 空的字段
26                 SerializerFeature.WriteMapNullValue,
27                 // 将 String 类型的 null 转成""
28                 SerializerFeature.WriteNullStringAsEmpty,
29                 // 将 Number 类型的 null 转成 0
30                 SerializerFeature.WriteNullNumberAsZero,
31                 // 将 List 类型的 null 转成 []
32                 SerializerFeature.WriteNullListAsEmpty,
33                 // 将 Boolean 类型的 null 转成 false
34                 SerializerFeature.WriteNullBooleanAsFalse,
35                 // 避免循环引用
36                 SerializerFeature.DisableCircularReferenceDetect);
37 
38         converter.setFastJsonConfig(config);
39         converter.setDefaultCharset(Charset.forName("UTF-8"));
40         List<MediaType> mediaTypeList = new ArrayList<>();
41         // 解决中文乱码问题,相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
42         mediaTypeList.add(MediaType.APPLICATION_JSON);
43         converter.setSupportedMediaTypes(mediaTypeList);
44         converters.add(converter);
45     }
46 }

Jackson VS fastjson

选项 FASTJSON 杰克逊
上手难易程度 容易 中等
高级特性支持 中等 丰富
官方文档,示例支持 中文 英文
处理JSON速度 略快

关于Jackson和fastjson的对比,网上有很多资料可以查看,大家可以根据自己实际情况选择合适的框架。从扩展上来看,fastjson没有Jackson灵活,从速度或者上手难度来看,fastjson可以考虑,它也比较方便

猜你喜欢

转载自www.cnblogs.com/long88-club/p/11361174.html