码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot总结

时间:2020-11-01 22:18:09      阅读:26      评论:0      收藏:0      [点我收藏+]

标签:number   condition   rri   cat   引用   get   port   public   ast   

Jackson

将null转成""

@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder){
        ObjectMapper objectMapper = jackson2ObjectMapperBuilder.createXmlMapper(false).build();
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator
            , SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString("");
            }
        });
        return objectMapper;
    }

}

FastJson

将null转成""

@Configuration
public class fastJsonConfig extends WebMvcConfigurationSupport {

/**
* 使用阿里 FastJson 作为JSON MessageConverter
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>>
converters) {
      FastJsonHttpMessageConverter converter = new
      FastJsonHttpMessageConverter();
      FastJsonConfig config = new FastJsonConfig();
      config.setSerializerFeatures(
      // 保留map空的字段
      SerializerFeature.WriteMapNullValue,
      // 将String类型的null转成""
      SerializerFeature.WriteNullStringAsEmpty,
      // 将Number类型的null转成0
      SerializerFeature.WriteNullNumberAsZero,
      // 将List类型的null转成[]
      SerializerFeature.WriteNullListAsEmpty,
      // 将Boolean类型的null转成false
      SerializerFeature.WriteNullBooleanAsFalse,
      // 避免循环引用
      SerializerFeature.DisableCircularReferenceDetect);
      converter.setFastJsonConfig(config);
      converter.setDefaultCharset(Charset.forName("UTF-8"));
      List<MediaType> mediaTypeList = new ArrayList<>();
      // 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces
      = "application/json"
      mediaTypeList.add(MediaType.APPLICATION_JSON);
      converter.setSupportedMediaTypes(mediaTypeList);
      converters.add(converter);
      }
}

SpringBoot总结

标签:number   condition   rri   cat   引用   get   port   public   ast   

原文地址:https://www.cnblogs.com/kwdlh/p/13910218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!