您好,登錄后才能下訂單哦!
今天小編給大家分享一下SpringCloud Feign Jackson自定義配置的方法的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
Spring Cloud Feign 默認支持Spring MVC的注解 使用相同的HttpMessageConverters類轉換
官方文檔說明:
Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web.
但是我們一般在返回給前端JSON格式的時候 都會把相應的 NULL值轉為相應的"",這使得Spring Cloud Feign也使用相同的Jackson配置,例如我們項目的配置
@Bean public ObjectMapper jacksonObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); // objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { jg.writeString(""); sp.getDefaultNullKeySerializer(); } }); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); return objectMapper; }
在服務通過Feign進行請求的時候,傳NULL值引用類型值時,會出現類型轉換異常,由于HttpMessageConverters直接把NULL轉為了""。
自定義配置 Spring Cloud Feign Encoder與Decoder
官方文檔說明:
Spring Cloud Netflix provides the following beans by default for feign (BeanType beanName: ClassName):Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)Encoder feignEncoder: SpringEncoderLogger feignLogger: Slf4jLoggerContract feignContract: SpringMvcContractFeign.Builder feignBuilder: HystrixFeign.BuilderClient feignClient: if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used.
統一配置Feign 的Encoder和Decoder的Jackson轉換方式
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.support.ResponseEntityDecoder; 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.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import feign.codec.Decoder; import feign.codec.Encoder; @Configuration public class CustomFeignConfig { @Bean public Decoder feignDecoder() { HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper()); ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter); return new ResponseEntityDecoder(new SpringDecoder(objectFactory)); } @Bean public Encoder feignEncoder(){ HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper()); ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter); return new SpringEncoder(objectFactory); } public ObjectMapper customObjectMapper(){ ObjectMapper objectMapper = new ObjectMapper(); //Customize as much as you want objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); return objectMapper; } }
如果配置DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT 不起作用可以試試這種方式
public ObjectMapper customObjectMapper(){ ObjectMapper objectMapper = new ObjectMapper(); //Customize as much as you want objectMapper.registerModule(new StringSanitizerModule()); return objectMapper; } public class StringSanitizerModule extends SimpleModule { public StringSanitizerModule() { addDeserializer(String.class, new StdScalarDeserializer<String>(String.class) { @Override public String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { return StringUtils.trimToNull(jsonParser.getValueAsString()); } }); } }
nacos: 1.3.1
啟動nacos
cd /usr/local/nacos/bin
sh startup.sh -m standalone
1)添加依賴
加入nacos-discovery發現服務的依賴、web、actuator用于監控檢查,添加openfeign依賴才能使用Feign功能
2)修改配置文件
配置nacos注冊中心地址,因為不提供服務,所以不需要再注冊中心注冊因此register-enabled=false
3)添加Feign支持
配置FeignConfiguration類文件
Spring Cloud Netflix默認的SpringMvcController將替換為feign.Contract.Default
用feign.Contract.Default將契約改為Feign原生的默認契約,就可以使用feign自帶的注解了
4)自定義Feign接口
Feign接口文件需和啟動類再同一包目錄下,使用@FeignClient注解配置所需要調用服務,此處nacos-provider是需要在注冊中心提供服務
因為上面配置了feign.Contract.Deafault,所以在接口中可以使用Feign原生的注解@RequestLine
添加HelloController類文件
此處使用接口MyFeignClient調用hello方法獲取到nacos-provider客戶端提供的服務
1)添加配置項
在配置文件中添加記錄日志的包,* 而每個FeignClient都需要單獨配置,它只能響應debug級別的日志
2)設置日志等級
在FeignConfiguration類文件中配置日志等級
日志等級:
NONE
:不記錄(默認)
BASIC
:只記錄請求方法、URL、響應狀態碼和執行時間
HEADERS
:記錄基本信息,請求和響應標題
FULL
: 記錄請求和響應標題、正文和行數據
啟動nacos-provider、feign-config客戶端,進入nacos查詢nacos-provider服務是否注冊
進入瀏覽器端鍵入地址http://localhost:2334/hello,就能訪問到nacos-provider提供的服務內容
控制臺會輸出如下信息
[MyFeignClient#hello] <— HTTP/1.1 200 (405ms)
2020-08-13 16:16:23.021 DEBUG 3680 — [nio-2334-exec-1] com.kk.feign.MyFeignClient : [MyFeignClient#hello] content-length: 16
2020-08-13 16:16:23.021 DEBUG 3680 — [nio-2334-exec-1] com.kk.feign.MyFeignClient : [MyFeignClient#hello] content-type: text/plain;charset=UTF-8
2020-08-13 16:16:23.021 DEBUG 3680 — [nio-2334-exec-1] com.kk.feign.MyFeignClient : [MyFeignClient#hello] date: Thu, 13 Aug 2020 08:16:23 GMT
2020-08-13 16:16:23.021 DEBUG 3680 — [nio-2334-exec-1] com.kk.feign.MyFeignClient : [MyFeignClient#hello]
2020-08-13 16:16:23.021 DEBUG 3680 — [nio-2334-exec-1] com.kk.feign.MyFeignClient : [MyFeignClient#hello] Hello Gateway A!
2020-08-13 16:16:23.021 DEBUG 3680 — [nio-2334-exec-1] com.kk.feign.MyFeignClient : [MyFeignClient#hello] <— END HTTP (16-byte body)
以上就是“SpringCloud Feign Jackson自定義配置的方法”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。