在Feign接口中返回泛型类型——自定义Decoder

简介: 前几天对接了一套第三方接口,所有接口的请求地址一样,请求参数和响应结果中有很多共同的字段,所以就想把这些字段都抽出来,Feign定义的接口直接返回泛型类型。

前几天对接了一套第三方接口,所有接口的请求地址一样,请求参数和响应结果中有很多共同的字段,所以就想把这些字段都抽出来,Feign定义的接口直接返回泛型类型。


Feign定义

@FeignClient(name = "demoFeign", url = "${config.demo.domain}")
public interface DemoFeign {
    @PostMapping(value = "/open/post")
    public <R extends BaseResponse, T extends BaseRequest> R invoke(@RequestBody T request);
}


请求参数父类 BaseRequest


@Data
public class BaseRequest{
    private String requestId;
    private String timeStamp;
    private String method;
}

接口1的请求参数定义 Request01


@Data
public class Request01 extends BaseRequest{
    private String merchantId;
}


接口2的请求参数定义 Request02


@Data
public class Request02 extends BaseRequest{
    private String orderNo;
}


响应结果父类 BaseRequest


@Data
public class BaseResponse{
    private String code;
    private String message;
}

接口1的响应结果定义 Response01


@Data
public class Response01 extends BaseResponse{
    private String merchantId;
    private String merchantName;
}


接口2的响应结果定义 Response02


@Data
public class Response02 extends BaseResponse{
    private String orderNo;
    private String orderTime;
}

调用的时候报错:feign.codec.DecodeException: type is not an instance of Class or ParameterizedType: R


feign.codec.DecodeException: type is not an instance of Class or ParameterizedType: R
  at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:61)
  at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:62)
  at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:36)
  at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:176)
  at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:140)
  at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
  at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
  at com.sun.proxy.$Proxy129.invoke(Unknown Source)


原来是当接口返回类型定义成泛型时,Feign的解码器Decoder(Feign默认的解码器是SpringDecoder)在解析接口响应内容的时候,Type被解析成了TypeVariableImpl类型,导致反序列化响应内容失败。

7.png


Feign的编码器和解码器是可插拔的,可以自定义一个Feign的解码器来解决这个问题。


1、定义一个 解析返回类型为泛型Feign接口解码器GenericsFeignResultDecoder,需要实现Decoder接口;


public class GenericsFeignResultDecoder implements Decoder {
  private static NamedThreadLocal<Class> feignReturnTypeThreadLocal=new NamedThreadLocal<Class>("feignReturnTypeThreadLocal");
  // 调用Feign的泛型接口前,先调用GenericsFeignResultDecoder.setReturnType()方法设置接口返回类型
  public static void setReturnType(Class returnType){
        feignReturnTypeThreadLocal.set(returnType);
    }
    // 重写Decode
    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
      try{
          if (response.body() == null) {
              throw new DecodeException(response.status(), "no data response");
          }
          Class returnType=feignReturnTypeThreadLocal.get();
          String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
          return JSON.parseObject(bodyStr,returnType);
      } catch (Exception e) {
            log.error("GenericsFeignResultDecoder.decode error", e);
        }finally {
            feignReturnTypeThreadLocal.remove();
        }
        return null;
    }
}


2、定义一个CustomizedConfiguration类,用于包装GenericsFeignResultDecoder实例,用configuration属性为Feign指定自当前配置类。


@FeignClient(name = "demoFeign", url = "${config.demo.domain}", configuration = CustomizedConfiguration.class)
public interface DemoFeign {
    @PostMapping(value = "/open/post")
    public <R extends BaseResponse, T extends BaseRequest> R invoke(@RequestBody T request);
    public class CustomizedConfiguration{
        @Bean
        public Decoder feignDecoder() {
            return new GenericsFeignResultDecoder();
        }
        @Bean
        @Scope("prototype")
        public Feign.Builder feignBuilder() {
            return Feign.builder();
        }
    }
}


如果要为当前Spring容器管理的所有Feign都指定这个解码器,就把CustomizedConfiguration类挪到Feign接口外面,再加@Configuration,我这里为了方便就写到Feign接口里了;如果只是为一个Feign Client指定自定义的解码器,GenericsFeignResultDecoder就不要加Spring注解(不要被Spring管理)了,否则就成了全局的了。


调用Feign的时候,需要先设置Feign接口返回的具体类型,这里通过ThreadLocal来传递Feign接口返回值的具体类型,在调用前把返回值类型放在ThreadLocal中,调用完再remove掉。


需要注意的是,用这种方法需要设置Hystrix的隔离策略为信号量隔离(默认为线程隔离),或者为当前FeignClient禁止Hystrix,上面的代码就为DemoFeign禁止了Hystrix。


调用Feign


@Service
public class DemoService{
  @Autowired
  private DemoFeign demoFeign;
    public void function01(Request01 request){
    GenericsFeignResultDecoder.setReturnType(Response01.class);
    Response01 response=demoFeign.invoke(request);
    // ……
  }
    public void function02(Request02 request){
    GenericsFeignResultDecoder.setReturnType(Response02.class);
    Response02 response=demoFeign.invoke(request);
    // ……
  }
}



相关文章
|
6天前
open-feign自定义反序列化decoder
open-feign自定义反序列化decoder
85 0
|
7月前
43SpringMVC - 参数绑定(默认支持的参数类型)
43SpringMVC - 参数绑定(默认支持的参数类型)
25 0
|
负载均衡 前端开发 Java
Feign 踩坑指南 (接口返回泛型设置属性为null)
Feign 简介 Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。
1274 0
Feign 踩坑指南 (接口返回泛型设置属性为null)
|
负载均衡 前端开发 Java
Feign对服务的声明式定义和调用
在此之前,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端,使用起来很不方便,需要了解URL,有时还需要拼装真正请求的URL。有没有一种用起来更方便、更优雅的方式吗?答案是肯定的,Spring Cloud想到了这些————Feign。Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。在Spring Cloud中使用Feign, 我们只需要通过创建接口并用注解来配置既可完成对Web服务接口的绑定,可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,
接口参数注解验证案例
写作缘由 写接口的时候经常会有请求体里某字段不为null的需求;也有使用一个dto对象,但是插入和修改都想使用这个dto,那这样的话判断条件就不一样,因为修改操作必须有ID,所以参数验证还是挺麻烦的。所以写个demo记录一下,亲测可用。
117 0
|
负载均衡 Nacos
一起用feign来调用接口(有源码)
nacos很好的兼容了feign,feign默认集成了Ribbon,所以Nacos下使用Feign就默认实现了负载均衡 一、测试结果
111 0
一起用feign来调用接口(有源码)
|
前端开发 Java
Springboot 一个注解搞定返回参数key转换 【实用】
Springboot 一个注解搞定返回参数key转换 【实用】
201 0
Springboot 一个注解搞定返回参数key转换 【实用】
AOP + 注解 实现通用的接口参数校验
写移动端接口的时候,为了校验参数,传统的做法是加各种判断,写了很多重复的代码,而且也不美观。为了增加代码复用性,美观的校验参数,采用AOP + 注解的方式来实现接口的参数校验(使用拦截器也可以实现),在需要校验参数的方法上加上自定义的注解即可。
245 0
AOP + 注解 实现通用的接口参数校验
|
SQL XML Java
MyBatis——dao代理的使用、深入理解参数(传递一个参数、传递多个参数、使用entity实体类传递、使用自定义类传递、按位置传递、使用Map传递)
MyBatis——dao代理的使用、深入理解参数(传递一个参数、传递多个参数、使用entity实体类传递、使用自定义类传递、按位置传递、使用Map传递)
MyBatis——dao代理的使用、深入理解参数(传递一个参数、传递多个参数、使用entity实体类传递、使用自定义类传递、按位置传递、使用Map传递)
Springboot 接口需要接收参数类型是数组
Springboot 接口需要接收参数类型是数组
567 0
Springboot 接口需要接收参数类型是数组