WebMvcConfigurationSupport跨域和fastjson全局替换

前端之家收集整理的这篇文章主要介绍了WebMvcConfigurationSupport跨域和fastjson全局替换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@Configuration
public class WarnWebMvcConfigurationSupport extends WebMvcConfigurationSupport {

    /**
     * @Author AlanMa
     * @Description 全局fastJson替换
     * @Param [converters]
     * @return void
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteEnumUsingToString,SerializerFeature.WriteNullStringAsEmpty);

        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST","GET","PUT","OPTIONS","DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}
原文链接:/json/729740.html

猜你在找的Json相关文章