SpringBootカスタム構成

1529 ワード

@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;
        }
    }