SpringCloudFeign自定义配置(重试、拦截与错误码处理)-SpringCloudOpenFeign接口反序列化失效解决方案-《Java笔记》

admin 2025-10-19 05:55:06 编程 来源:ZONE.CI 全球网 0 阅读模式

Java SpringCloud OpenFeign

1、关于 Spring Boot 无侵入式API接口统一格式变得具有侵入性

可是最近几年随着Spring Cloud的流行,基于ResponseBodyAdvice在Spring Cloud OpenFeign的继承模式下 具有了侵入性,而且侵入性还很强大,还导致resfulAPI接口反序列化出现失败的问题。既然发现了问题,那就好好解决一下吧,解决方式有多种,至于选择就看大家喜欢了吧

  1. return object,简单轻松和谐
  2. return Result<?>,简单轻松和谐,但是client需要 调用Result.getData()方法
  3. feign.codec.Decoder 不轻松,需要自己去自定义Decoder规则

第一方案和第二方案都是硬编码,没有多少可延展性可说的,现在就把feign.codec.Decoder如何解决这种侵入性的问题解决。

2、Decoder解决ResponseBodyAdvice的侵入性

使用 Spring Cloud 2021.0.1定义feignService api接口,因为篇幅问题,就忽略 Controller实现和client调用了

  1. public interface UserService {
  2. /** 原生 ResponseBody返回 */
  3. @RequestMapping(method = RequestMethod.GET, value = "/users/{id}")
  4. User getUser(@PathVariable("id") long id);
  5. /** 自定义 ResultResponseBody 统一返回 */
  6. @RequestMapping(method = RequestMethod.GET, value = "/Resultv1")
  7. @ResultResponseBody
  8. User getResultv1();
  9. /** 硬编码 Result<User> 返回同时带上ResultResponseBody统一返回 */
  10. @RequestMapping(method = RequestMethod.GET, value = "/Resulv2")
  11. @ResultResponseBody
  12. Result<User> getResultv2();
  13. }

Decoder对Result进行解析

  1. public class ResultDecoder implements Decoder {
  2. private Decoder decoder;
  3. public ResultDecoder(Decoder decoder) {
  4. this.decoder = decoder;
  5. }
  6. @Override
  7. public Object decode(Response response, Type type) throws IOException, FeignException {
  8. Method method = response.request().requestTemplate().methodMetadata().method();
  9. boolean isResult = method.getReturnType() != Result.class && method.isAnnotationPresent(ResultResponseBody.class);
  10. if (isResult) {
  11. ParameterizedTypeImpl resultType = ParameterizedTypeImpl.make(Result.class, new Type[]{type}, null);
  12. Result<?> result = (Result<?>) this.decoder.decode(response, resultType);
  13. return result.getData();
  14. }
  15. return this.decoder.decode(response, type);
  16. }
  17. }

注册ResultDecoder到feign中

  1. @Configuration(proxyBeanMethods = false)
  2. public class FeignConfiguration {
  3. @Autowired
  4. private ObjectFactory<HttpMessageConverters> messageConverters;
  5. @Bean
  6. public Decoder feignDecoder(ObjectProvider<HttpMessageConverterCustomizer> customizers) {
  7. return new OptionalDecoder(new ResponseEntityDecoder(new ResultDecoder(new SpringDecoder(this.messageConverters, customizers))));
  8. }
  9. }

这样子就解决了在Spring Boot中无侵入性,但是在Spring Cloud中具有侵入性的问题。

以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  6