• Feign 系列(01)最简使用姿态


    Feign 系列(01)最简使用姿态

    Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/11563952.html#feign)

    更多使用案例见 Feign Github 官网

    1. 引入 maven 依赖

    <dependencies>
        <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>
    </dependencies>
    

    2. 基本用法

    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);
    
    }
    
    public static class Contributor {
      String login;
      int contributions;
    }
    
    public static class Issue {
      String title;
      String body;
      List<String> assignees;
      int milestone;
      List<String> labels;
    }
    
    public class MyApp {
      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 + ")");
        }
      }
    }
    

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

    3. Feign 声明式注解

    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);
    }
    

    每天用心记录一点点。内容也许不重要,但习惯很重要!

  • 相关阅读:
    外观模式
    解释器模式
    LoadRunner学习笔记(三)
    lr 中cookie的解释与用法
    LR使用web_add_cookie函数进行cookie模拟
    LoadRunner学习笔记(二)
    SVN服务器搭建和使用
    使用Jmeter监测服务器性能指标
    jmeter 使用白皮书
    intellij idea创建maven项目
  • 原文地址:https://www.cnblogs.com/binarylei/p/11576147.html
Copyright © 2020-2023  润新知