Spring Cloud Feign配置FastJson
项目集成FastJson解析框架,一般都会添加一个配置文件,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 @Configuration public class CustomFastJsonConfig { @Bean FastJsonHttpMessageConverter fastJsonHttpMessageConverter () { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter (); FastJsonConfig fastJsonConfig = new FastJsonConfig (); fastJsonConfig.setSerializerFeatures( SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteEnumUsingToString, SerializerFeature.DisableCircularReferenceDetect); fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss" ); fastJsonConfig.setCharset(StandardCharsets.UTF_8); List<MediaType> fastMediaTypes = new ArrayList <>(); fastMediaTypes.add(MediaType.APPLICATION_JSON); converter.setSupportedMediaTypes(fastMediaTypes); converter.setFastJsonConfig(fastJsonConfig); return converter; } }
但是,当Spring Cloud微服务项目使用Feign的时候,有一个比较坑的地方,就是,Feign并不共用Spring MVC的消息转换器链,而且它默认使用的是Jackson Json解析库,而项目使用的又是FastJson,这会导致混乱,甚至出现序列化/反序列化错误。
就比如上边这个配置,A应用通过Feign调用B应用的搜索接口,接口参数是一个实体对象,其中实体对象有一个Date类型的时间属性参数 downDate
,那么,由于上边配置的日期格式是:yyyy-MM-dd HH:mm:ss,在通过Feign调用的时候,就会产生一个异常:
1 2 3 4 5 6 7 8 9 10 11 {"timestamp":"2020-03-29T03:54:55.014+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot deserialize value of type `java.util.Date` from String \"2020-03-03 00:00:00\": not a valid representation (error: Failed to parse Date value '2020-03-03 00:00:00': Cannot parse date \"2020-03-03 00:00:00\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String \"2020-03-03 00:00:00\": not a valid representation (error: Failed to parse Date value '2020-03-03 00:00:00': Cannot parse date \"2020-03-03 00:00:00\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)) at [Source: (PushbackInputStream); line: 1, column: 154] (through reference chain: com.kangaroohy.entity.bo.SearchBO[\"downDate\"])","path":"/api/search"}
因此,需要为Feign单独配置FastJson,将原来的转换器替换成FastJson的,具体配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import feign.codec.Encoder;import org.springframework.beans.factory.ObjectFactory;import org.springframework.boot.autoconfigure.http.HttpMessageConverters;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 org.springframework.http.converter.HttpMessageConverter;import java.util.ArrayList;import java.util.List;@Configuration public class FeignClientConfig { @Bean public Encoder feignEncoder () { return new SpringEncoder (feignHttpMessageConverter()); } private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter () { final HttpMessageConverters httpMessageConverters = new HttpMessageConverters (createFastJsonConverter()); return () -> httpMessageConverters; } private HttpMessageConverter createFastJsonConverter () { FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter (); List<MediaType> supportedMediaTypes = new ArrayList <>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); supportedMediaTypes.add(MediaType.APPLICATION_PDF); supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); supportedMediaTypes.add(MediaType.APPLICATION_XML); supportedMediaTypes.add(MediaType.IMAGE_GIF); supportedMediaTypes.add(MediaType.IMAGE_JPEG); supportedMediaTypes.add(MediaType.IMAGE_PNG); supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); supportedMediaTypes.add(MediaType.TEXT_HTML); supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); supportedMediaTypes.add(MediaType.TEXT_PLAIN); supportedMediaTypes.add(MediaType.TEXT_XML); fastJsonConverter.setSupportedMediaTypes(supportedMediaTypes); FastJsonConfig fastJsonConfig = new FastJsonConfig (); fastJsonConfig.setSerializerFeatures( SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteEnumUsingToString, SerializerFeature.DisableCircularReferenceDetect); fastJsonConverter.setFastJsonConfig(fastJsonConfig); return fastJsonConverter; } }
此时,就不会发生上述的报错了。
特此记录。