SpringCloud : Feign 使用 FastJson 解析数据
本文内容纲要:
Fastjson版本1.2.60
Spring版本5.1.5.RELEASE
SpringBoot版本2.1.5.RELEASE
SpringCloud版本2.1.1.RELEASE
SpringCloud中配置FastJson如下:
@Configuration
publicclassFastJsonConverterConfigextendsWebMvcConfigurationSupport{
/**
*使用fastjson代替jackson
*/
@Override
protectedvoidextendMessageConverters(List<HttpMessageConverter<?>>converters){
//先把JackSon的消息转换器删除.
//(1)源码分析可知,返回json的过程为:
//Controller调用结束后返回一个数据对象,for循环遍历conventers,找到支持application/json的HttpMessageConverter,然后将返回的数据序列化成json。
//具体参考org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor的writeWithMessageConverters方法
//(2)由于是list结构,我们添加的fastjson在最后。因此必须要将jackson的转换器删除,不然会先匹配上jackson,导致没使用fastjson
for(inti=converters.size()-1;i>=0;i--){
if(converters.get(i)instanceofMappingJackson2HttpMessageConverter){
converters.remove(i);
}
}
//自定义fastjson配置
FastJsonConfigconfig=newFastJsonConfig();
config.setSerializerFeatures(
//是否输出值为null的字段,默认为false,我们将它打开
SerializerFeature.WriteMapNullValue,
//将Collection类型字段的字段空值输出为[]
SerializerFeature.WriteNullListAsEmpty,
//将字符串类型字段的空值输出为空字符串
SerializerFeature.WriteNullStringAsEmpty,
//将数值类型字段的空值输出为0
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteDateUseDateFormat,
//禁用循环引用
SerializerFeature.DisableCircularReferenceDetect
);
//添加支持的MediaTypes;不添加时默认为*/*,也就是默认支持全部
//但是MappingJackson2HttpMessageConverter里面支持的MediaTypes为application/json
List<MediaType>fastMediaTypes=newArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
FastJsonHttpMessageConverterfastJsonHttpMessageConverter=newFastJsonHttpMessageConverter();
fastJsonHttpMessageConverter.setFastJsonConfig(config);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
converters.add(fastJsonHttpMessageConverter);
//支持XML格式的请求
converters.add(newStringHttpMessageConverter());
}
}
但是,在使用Feign的SpringCloud微服务项目中,要特别注意需要为Feign单独配置。因为Feign并不共用SpringMVC的消息转换器链,默认使用的是JacksonJson解析库。
所以需要单独如下配置:
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.serializer.SerializerFeature;
importcom.alibaba.fastjson.support.config.FastJsonConfig;
importcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
importcom.alibaba.fastjson.support.springfox.SwaggerJsonSerializer;
importfeign.Logger;
importfeign.codec.Decoder;
importfeign.codec.Encoder;
importorg.springframework.beans.factory.ObjectFactory;
importorg.springframework.boot.autoconfigure.http.HttpMessageConverters;
importorg.springframework.cloud.openfeign.support.SpringDecoder;
importorg.springframework.cloud.openfeign.support.SpringEncoder;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.http.MediaType;
importjava.util.ArrayList;
importjava.util.List;
/**
*Feign客户端配置
*
*@authorxushiling
*@date2018/8/13
*/
@Configuration
publicclassFeignConfig{
@Bean
Logger.LevelfeignLoggerLevel(){
//这里记录所有,根据实际情况选择合适的日志level
returnLogger.Level.FULL;
}
@Bean
publicEncoderfeignEncoder(){
returnnewSpringEncoder(feignHttpMessageConverter());
}
@Bean
publicDecoderfeignDecoder(){
returnnewSpringDecoder(feignHttpMessageConverter());
}
/**
*设置解码器为fastjson
*
*@return
*/
privateObjectFactory<HttpMessageConverters>feignHttpMessageConverter(){
finalHttpMessageConvertershttpMessageConverters=newHttpMessageConverters(this.getFastJsonConverter());
return()->httpMessageConverters;
}
privateFastJsonHttpMessageConvertergetFastJsonConverter(){
FastJsonHttpMessageConverterconverter=newFastJsonHttpMessageConverter();
List<MediaType>supportedMediaTypes=newArrayList<>();
MediaTypemediaTypeJson=MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE);
supportedMediaTypes.add(mediaTypeJson);
converter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfigconfig=newFastJsonConfig();
config.getSerializeConfig().put(JSON.class,newSwaggerJsonSerializer());
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
returnconverter;
}
}
PS:
https://blog.csdn.net/lppl010_/article/details/94215233
https://www.jianshu.com/p/b3b93a32cb05
本文内容总结:
原文链接:https://www.cnblogs.com/phpdragon/p/12118654.html