美文网首页工作生活
SpringBoot自定义配置

SpringBoot自定义配置

作者: mml_慢慢来 | 来源:发表于2019-07-03 10:53 被阅读0次
@Bean
@Scope("singleton")
public ObjectMapper objectMapper(){
    return new ObjectMapper();
}

声明这个json处理会覆盖掉spring的,导致json解析失败
下面是使用自定义的fastjson

自定义springboot配置

    @Configuration
    @ConditionalOnClass({FastJsonHttpMessageConverter.class})
    @ConditionalOnProperty(
            name = {"spring.http.converters.preferred-json-mapper"},
            havingValue = "fastjson",
            matchIfMissing = true
    )
    static class FastJson2HttpMessageConverterConfiguration{
        @Bean
        @ConditionalOnMissingBean({FastJsonHttpMessageConverter.class})
        public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
            FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullNumberAsZero,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullBooleanAsFalse,
                    SerializerFeature.WriteDateUseDateFormat,
                    SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteClassName
            );
            converter.setFastJsonConfig(fastJsonConfig);
            return converter;
        }
    }

相关文章

网友评论

    本文标题:SpringBoot自定义配置

    本文链接:https://www.haomeiwen.com/subject/pyhthctx.html