Fastjson 版本1.2.60
Spring 版本 5.1.5.RELEASE
SpringBoot 版本 2.1.5.RELEASE
SpringCloud 版本 2.1.1.RELEASE
SpringCloud 中配置 FastJson 如下:
@Configuration public class FastJsonConverterConfig extends WebMvcConfigurationSupport { /** * 使用fastjson代替jackson */ @Override protected void extendMessageConverters(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 (int i = converters.size() - 1; i >= 0; i--) { if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) { converters.remove(i); } } //自定义fastjson配置 FastJsonConfig config = new FastJsonConfig(); 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 = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); fastJsonHttpMessageConverter.setFastJsonConfig(config); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); converters.add(fastJsonHttpMessageConverter); //支持XML格式的请求 converters.add(new StringHttpMessageConverter()); } }
但是,在使用Feign的Spring Cloud微服务项目中,要特别注意需要为Feign单独配置。因为Feign并不共用Spring MVC的消息转换器链,默认使用的是Jackson Json解析库。
所以需要单独如下配置:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import com.alibaba.fastjson.support.springfox.SwaggerJsonSerializer; import feign.Logger; import feign.codec.Decoder; import feign.codec.Encoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.cloud.openfeign.support.SpringEncoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import java.util.ArrayList; import java.util.List; /** * Feign 客户端配置 * * @author xushiling * @date 2018/8/13 */ @Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel() { //这里记录所有,根据实际情况选择合适的日志level return Logger.Level.FULL; } @Bean public Encoder feignEncoder() { return new SpringEncoder(feignHttpMessageConverter()); } @Bean public Decoder feignDecoder() { return new SpringDecoder(feignHttpMessageConverter()); } /** * 设置解码器为fastjson * * @return */ private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(this.getFastJsonConverter()); return () -> httpMessageConverters; } private FastJsonHttpMessageConverter getFastJsonConverter() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); List<MediaType> supportedMediaTypes = new ArrayList<>(); MediaType mediaTypeJson = MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE); supportedMediaTypes.add(mediaTypeJson); converter.setSupportedMediaTypes(supportedMediaTypes); FastJsonConfig config = new FastJsonConfig(); config.getSerializeConfig().put(JSON.class, new SwaggerJsonSerializer()); config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); converter.setFastJsonConfig(config); return converter; } }
PS: