springboot整合retrofit

springboot整合retrofit

起男 1,290 2020-10-20

springboot整合retrofit

  1. 导入jar包

    <dependency>
        <groupId>com.github.lianjiatech</groupId>
        <artifactId>retrofit-spring-boot-starter</artifactId>
        <version>2.0.2</version>
    </dependency>
    
  2. 在启动类开启接口扫描

    @SpringBootApplication
    @RetrofitScan("com.dqn.retrofit.retrofitdemo.http")
    public class RetrofitDemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(RetrofitDemoApplication.class, args);
        }
    }
    
  3. 编写请求接口

    /**
    * RetrofitClient:表示这个接口是一个请求接口
    * baseUrl:必填,表示访问的地址
    * poolName:使用的连接池的名称
    * logLevel:日志等级
    * logStrategy:日志策略
    */
    @RetrofitClient(baseUrl = "http://localhost:8081",
            poolName = "test1",
            logLevel = Level.DEBUG,
            logStrategy = BaseLoggingInterceptor.LogStrategy.BODY)
    public interface HttpApi {
        ...
    }
    
  4. 编写请求方法

    @GET("my")
    ResponseBody getMy(@Query("id")Long id);
    
    • http请求方法:@GET、@POST、@PUT、@DELETE、@PATCH、@OPTIONS、@HTTP
    • 标记:@FormUrlEncoded(表示请求体是一个form表单)、@Multipart(表示请求体是一个支持文件上传的form表单)、@Streaming(表示响应体的数据用流的形式返回)
    • 参数:@Headers、@Header(用于设置请求头)、@Body(非表单请求体)、@Field、@FieldMap(用于表单字段)、@Part、@PartMap(用于上传文件)、@Path(路径参数)、@Query、@QueryMap(拼在url上的请求参数)、Url(拼接url)
    • 返回值:Call、CompletableFuture、Void、Response、其它java类型
  5. 配置文件(可选)

    retrofit:
      #连接池
      pool:
        test1:
          max-idle-connections: 3
          keep-alive-second: 100
        test2:
          max-idle-connections: 5
          keep-alive-second: 50
      #异常信息格式化
      http-exception-message-formatter: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultHttpExceptionMessageFormatter
      #日志
      logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
    
  6. 测试

    @Autowired
    private HttpApi httpApi;
    
    @Test
    void contextLoads() throws IOException {
        ResponseBody my = httpApi.getMy(11L);
        System.out.println(my);
    }
    

    拦截器

    1. 继承BasePathMatchInterceptor

      public class MyInterceptor extends BasePathMatchInterceptor {
          @Override
          protected Response doIntercept(Chain chain) throws IOException {
              Request request = chain.request();
      
              System.out.println("请求发送前");
              Response response = chain.proceed(request);
              System.out.println("请求发送后");
              return response;
          }
      }
      
    2. 请求接口上使用@Intercept

      @RetrofitClient(baseUrl = "http://localhost:8081",
              poolName = "test1",
              logLevel = Level.DEBUG,
              logStrategy = BaseLoggingInterceptor.LogStrategy.BODY)
      //handler:使用的拦截器
      //include:拦截路径,默认/**
      //exclude:跳过拦截
      @Intercept(handler = MyInterceptor.class)
      public interface HttpApi {
          @GET("my")
          ResponseBody getMy(@Query("id")Long id);
      }
      

    全局拦截器

    @Component
    public class MyGlobalInterceptor extends BaseGlobalInterceptor {
        @Override
        protected Response doIntercept(Chain chain) throws IOException {
            System.out.println("全局连接器--->前");
            Response proceed = chain.proceed(chain.request());
            System.out.println("全局拦截器--->后");
            return proceed;
        }
    }