引入依赖
<dependency>
    <groupId>com.github.lianjiatech</groupId>
   <artifactId>retrofit-spring-boot-starter</artifactId>
   <version>2.2.22</version>
</dependency>
整合简单的Demo
首先在yml配置文件定义一个通用的url地址
szx:
  blog:
    baseUrl: http://localhost:8091
在Controller类中定义一个test01接口
@RestController
public class TestController {

    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);

    @GetMapping("test01")
    public String test01(String username){
        System.out.println("test01接口被调用类...参数:" + username);
        return "请求成功";
    }

}
然后创建一个接口类,在接口中定义具体的方法
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import org.springframework.stereotype.Component;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * <p>
 * Description: Retrofit2测试接口
 * </p>
 *
 * @author songzixian
 * @version v2.0.0
 * @create 2022-04-10 0:49
 * @see com.xxl.job.executor.service
 */
@Component
@RetrofitClient(baseUrl = "${szx.blog.baseUrl}")
public interface BlogHttpApi {

    /**
     * description http接口
     *
     * @param:  [username]
     * @return {@link java.lang.String}
     * @Date   2022/4/10
    */
    @GET("test01")
    public String getPerson(@Query("username") String username);

}
测试HTTP请求
/**
 * <p>
 * Description: TODO
 * </p>
 *
 * @author songzixian
 * @version v2.0.0
 * @create 2022-04-10 0:55
 * @see com.xxl.job.executor.mvc.controller
 */
@RestController
public class TestHTTPController {


    @Autowired
    private BlogHttpApi blogHttpApi;

    @GetMapping("getUsername")
    public  String getUsername(){
        //  调用刚刚定义的test01这个http接口
        String data = blogHttpApi.getPerson("张三");
        return "请求相应参数:"+ data ;
    }
    
}
控制台输出
01:36:51.088 logback [xxl-job, JobThread-1-1649525811086] INFO  c.g.l.r.s.b.i.DefaultLoggingInterceptor - --> GET http://localhost:8091/test01?username=%E5%BC%A0%E4%B8%89 http/1.1
01:36:51.090 logback [http-nio-8091-exec-3] INFO  c.x.j.e.s.jobhandler.SampleXxlJob - test01接口被调用类...参数:张三

页面响应

求成功,响应结果:张三

到了这一步说明你的spring boot项目已经整合Retrofit成功了

Spring Boot整合Retrofit详细配置

按业务场景需要配置在yml文件中即可

retrofit:
   # 连接池配置
   pool:
      # test1连接池配置
      test1:
         # 最大空闲连接数
         max-idle-connections: 3
         # 连接保活时间(秒)
         keep-alive-second: 100

   # 是否禁用void返回值类型
   disable-void-return-type: false


   # 全局转换器工厂
   global-converter-factories:
      - com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory
      - retrofit2.converter.jackson.JacksonConverterFactory
   # 全局调用适配器工厂
   global-call-adapter-factories:
      - com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory
      - com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory

   # 日志打印配置
   log:
      # 启用日志打印
      enable: true
      # 日志打印拦截器
      logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
      # 全局日志打印级别
      global-log-level: info
      # 全局日志打印策略
      global-log-strategy: body


   # 重试配置
   retry:
      # 是否启用全局重试
      enable-global-retry: true
      # 全局重试间隔时间
      global-interval-ms: 1
      # 全局最大重试次数
      global-max-retries: 1
      # 全局重试规则
      global-retry-rules:
         - response_status_not_2xx
         - occur_io_exception
      # 重试拦截器
      retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor

   # 熔断降级配置
   degrade:
      # 是否启用熔断降级
      enable: true
      # 熔断降级实现方式
      degrade-type: sentinel
      # 熔断资源名称解析器
      resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
   # 全局连接超时时间
   global-connect-timeout-ms: 5000
   # 全局读取超时时间
   global-read-timeout-ms: 5000
   # 全局写入超时时间
   global-write-timeout-ms: 5000
   # 全局完整调用超时时间
   global-call-timeout-ms: 0
高级功能

超时时间设置
retrofit-spring-boot-starter支持两种方式设置超时时间,一种是全局超时时间设置,另一种是注解超时时间设置。

全局超时时间设置
在yaml文件中可配置全局超时时间,对所有接口生效。

retrofit:
   # 全局连接超时时间
   global-connect-timeout-ms: 5000
   # 全局读取超时时间
   global-read-timeout-ms: 5000
   # 全局写入超时时间
   global-write-timeout-ms: 5000
   # 全局完整调用超时时间
   global-call-timeout-ms: 0
注解式超时时间设置

在@RetrofitClient注解上可以设置超时时间,针对当前接口生效,优先级更高。具体字段有connectTimeoutMs、readTimeoutMs、writeTimeoutMs、callTimeoutMs等。

更多详细配置 https://gitee.com/lianjiatech/retrofit-spring-boot-starter#功能

Last modification:April 23, 2022
如果觉得这篇技术文章对你有用,请随意赞赏