在实际项目中,我们难免会遇到一些无值。当我们转JSON时,不希望这些null出现,比如我们期望所有的null在转JSON时都变成“”“”这种空字符串,那怎么做呢?
Jackson中对null的处理
1 1 @Configuration 2 2 public class JacksonConfig { 3 3 @Bean 4 4 @Primary 5 5 @ConditionalOnMissingBean(ObjectMapper.class) 6 6 public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { 7 7 ObjectMapper objectMapper = builder.createXmlMapper(false).build(); 8 8 objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { 9 9 @Override 1010 public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { 1111 jsonGenerator.writeString(""); 1212 } 1313 }); 1414 return objectMapper; 1515 } 1616 }
fastjson
使用fastjson需要导入依赖(https://mvnrepository.com/search?q=fastjson)
11 <dependency> 22 <groupId>com.alibaba</groupId> 33 <artifactId>fastjson</artifactId> 44 <version>1.2.58</version> 55 </dependency>
使用fastjson时,对null的处理和Jackson有些不同,需要继承WebMvcConfigurationSupport类,然后覆盖configureMessageConverters方法。在方法中,我们可以选择要实现null转换的场景,配置好即可。
1 1 import com.alibaba.fastjson.serializer.SerializerFeature; 2 2 import com.alibaba.fastjson.support.config.FastJsonConfig; 3 3 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 4 4 import org.springframework.context.annotation.Configuration; 5 5 import org.springframework.http.MediaType; 6 6 import org.springframework.http.converter.HttpMessageConverter; 7 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 8 8 9 9 import java.nio.charset.Charset; 1010 import java.util.ArrayList; 1111 import java.util.List; 1212 1313 @Configuration 1414 public class fastJsonConfig extends WebMvcConfigurationSupport { 1515 1616 /** 1717 * 使用阿里 fastjson 作为 JSON MessageConverter 1818 * @param converters 1919 */ 2020 @Override 2121 public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 2222 FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); 2323 FastJsonConfig config = new FastJsonConfig(); 2424 config.setSerializerFeatures( 2525 // 保留 Map 空的字段 2626 SerializerFeature.WriteMapNullValue, 2727 // 将 String 类型的 null 转成"" 2828 SerializerFeature.WriteNullStringAsEmpty, 2929 // 将 Number 类型的 null 转成 0 3030 SerializerFeature.WriteNullNumberAsZero, 3131 // 将 List 类型的 null 转成 [] 3232 SerializerFeature.WriteNullListAsEmpty, 3333 // 将 Boolean 类型的 null 转成 false 3434 SerializerFeature.WriteNullBooleanAsFalse, 3535 // 避免循环引用 3636 SerializerFeature.DisableCircularReferenceDetect); 3737 3838 converter.setFastJsonConfig(config); 3939 converter.setDefaultCharset(Charset.forName("UTF-8")); 4040 List<MediaType> mediaTypeList = new ArrayList<>(); 4141 // 解决中文乱码问题,相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json" 4242 mediaTypeList.add(MediaType.APPLICATION_JSON); 4343 converter.setSupportedMediaTypes(mediaTypeList); 4444 converters.add(converter); 4545 } 4646 }
Jackson VS fastjson
选项
FASTJSON
杰克逊
上手难易程度
容易
中等
高级特性支持
中等
丰富
官方文档,示例支持
中文
英文
处理JSON速度
略快
快
关于Jackson和fastjson的对比,网上有很多资料可以查看,大家可以根据自己实际情况选择合适的框架。从扩展上来看,fastjson没有Jackson灵活,从速度或者上手难度来看,fastjson可以考虑,它也比较方便。