Fegin系列一:简单使用

  1. 引入maven依赖

    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-core</artifactId>
        <version>10.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-gson</artifactId>
        <version>10.4.0</version>
    </dependency>
    
  2. 基本用法

    public class FeginApp {
        public static void main(String... args) {
            GitHub github = Feign.builder()
                    .decoder(new GsonDecoder())
                    .target(GitHub.class, "https://api.github.com");
    
            // Fetch and print a list of the contributors to this library.
            List<Contributor> contributors = github.contributors("OpenFeign", "feign");
            for (Contributor contributor : contributors) {
                System.out.println(contributor.login + " (" + contributor.contributions + ")");
            }
        }
    }
    
    interface GitHub {
        @RequestLine("GET /repos/{owner}/{repo}/contributors")
        List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
    
        @RequestLine("POST /repos/{owner}/{repo}/issues")
        void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
    
    }
    
    class Contributor {
        String login;
        int contributions;
    }
    
    class Issue {
        String title;
        String body;
        List<String> assignees;
        int milestone;
        List<String> labels;
    }
    

    总结: Feign.target() 实际上是创建了一个 GitHub 的动态代理。

  3. Fegin声明式注解

    Feign 通过 Contract 接口将方法上标注的注解解析成 MethodMetadata,最终将参数解析成 Http 请求的请求行、请求行、请求体,然后使用 HttpClient 发送请求。

    Annotation Interface Target Usage
    @RequestLine Method 定义HttpMethod 和 UriTemplate. UriTemplate 中使用{} 包裹的表达式,可以通过在方法参数上使用@Param 自动注入
    @Param Parameter 定义模板变量,模板变量的值可以使用名称的方式使用模板注入解析
    @Headers Method, Type 定义头部模板变量,使用@Param 注解提供参数值的注入。如果该注解添加在接口类上,则所有的请求都会携带对应的Header信息;如果在方法上,则只会添加到对应的方法请求上
    @QueryMap Parameter 定义一个Map或 POJO,参数值将会被转换成URL上的 query 字符串上
    @HeaderMap Parameter Map ->Http Headers
    @Body Method Defines a Template, similar to a UriTemplate and HeaderTemplate, that uses @Param annotated values to resolve the corresponding Expressions.

    注解的基本使用方法如下:

    public interface FeignService {
      // @Headers
      @RequestLine("GET /api/documents/{contentType}")
      @Headers("Accept: {contentType}")
      String getDocumentByType(@Param("contentType") String type);
      
      // @QueryMap: Map or POJO
      @RequestLine("GET /find")
      V find(@QueryMap Map<String, Object> queryMap);
      @RequestLine("GET /find")
      V find(@QueryMap CustomPojo customPojo);
      
      // @HeaderMap: Map
      @RequestLine("POST /")
      void post(@HeaderMap Map<String, Object> headerMap);
        
      // @Body
      @RequestLine("POST /")
      @Headers("Content-Type: application/xml")
      @Body("<login \"user_name\"=\"{user_name}\" \"password\"=\"{password}\"/>")
      void xml(@Param("user_name") String user, @Param("password") String password);
    
      @RequestLine("POST /")
      @Headers("Content-Type: application/json")
      @Body("%7B\"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D")
      void json(@Param("user_name") String user, @Param("password") String password);
    }
    
posted @ 2021-01-22 18:05  adtle123  阅读(77)  评论(0)    收藏  举报