Spring Boot项目下最优雅的http客户端工具,用它就够了,太香了


你们都知道okhttp是一款由square公司开源的java版本http客户端工具。实际上,square公司还开源了基于okhttp进一步封装的retrofit工具,用来支持经过接口的方式发起http请求若是你的项目中还在直接使用RestTemplate或者okhttp,或者基于它们封装的HttpUtils,那么你能够尝试使用Retrofitjava

retrofit-spring-boot-starter实现了Retrofit与spring-boot框架快速整合,而且支持了部分功能加强,从而极大的简化spring-boot项目下http接口调用开发。接下来咱们直接经过retrofit-spring-boot-starter,来看看spring-boot项目发送http请求有多简单。git

retrofit官方并无提供与spring-boot快速整合的starter。retrofit-spring-boot-starter是笔者封装的,已在生产环境使用,很是稳定。造轮子不易,跪求各位大佬star程序员

引入依赖

<dependency>
    <groupId>com.github.lianjiatech</groupId>
    <artifactId>retrofit-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency>

配置@RetrofitScan注解

你能够给带有 @Configuration 的类配置@RetrofitScan,或者直接配置到spring-boot的启动类上,以下:github

@SpringBootApplication
@RetrofitScan("com.github.lianjiatech.retrofit.spring.boot.test")
public class RetrofitTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RetrofitTestApplication.class, args);
    }
}

定义http接口

接口必须使用@RetrofitClient注解标记!http相关注解可参考官方文档:retrofit官方文档。面试

@RetrofitClient(baseUrl = "${test.baseUrl}")
public interface HttpApi {

    @GET("person")
    Result<Person> getPerson(@Query("id") Long id);
}

注入使用

将接口注入到其它Service中便可使用spring

@Service
public class TestService {

    @Autowired
    private HttpApi httpApi;

    public void test() {
        // 经过httpApi发起http请求
    }
}

只要经过上述几个步骤,就能实现经过接口发送http请求了,真的很简单。若是你在spring-boot项目里面使用过mybatis,相信你对这种使用方式会更加熟悉。接下来咱们继续介绍一下retrofit-spring-boot-starter更高级一点的功能。api

注解式拦截器

不少时候,咱们但愿某个接口下的某些http请求执行统一的拦截处理逻辑。这个时候可使用注解式拦截器。使用的步骤主要分为2步:mybatis

  1. 继承BasePathMatchInterceptor编写拦截处理器;架构

  2. 接口上使用@Intercept进行标注。app

下面以给指定请求的url后面拼接timestamp时间戳为例,介绍下如何使用注解式拦截器。

继承BasePathMatchInterceptor编写拦截处理器

@Component
public class TimeStampInterceptor extends BasePathMatchInterceptor {

    @Override
    public Response doIntercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl url = request.url();
        long timestamp = System.currentTimeMillis();
        HttpUrl newUrl = url.newBuilder()
                .addQueryParameter("timestamp", String.valueOf(timestamp))
                .build();
        Request newRequest = request.newBuilder()
                .url(newUrl)
                .build();
        return chain.proceed(newRequest);
    }
}

接口上使用@Intercept进行标注

@RetrofitClient(baseUrl = "${test.baseUrl}")
@Intercept(handler = TimeStampInterceptor.class, include = {"/api/**"}, exclude = "/api/test/savePerson")
public interface HttpApi {

    @GET("person")
    Result<Person> getPerson(@Query("id") Long id);

    @POST("savePerson")
    Result<Person> savePerson(@Body Person person);
}

上面的@Intercept配置表示:拦截HttpApi接口下/api/**路径下(排除/api/test/savePerson)的请求,拦截处理器使用TimeStampInterceptor。

扩展注解式拦截器

有的时候,咱们须要在拦截注解动态传入一些参数,而后再执行拦截的时候须要使用这个参数。这种时候,咱们能够扩展实现自定义拦截注解。自定义拦截注解必须使用@InterceptMark标记,而且注解中必须包括include()、exclude()、handler()属性信息。使用的步骤主要分为3步:

  1. 自定义拦截注解

  2. 继承BasePathMatchInterceptor编写拦截处理器

  3. 接口上使用自定义拦截注解;

例如咱们须要在请求头里面动态加入accessKeyId、accessKeySecret签名信息才能正常发起http请求,这个时候能够**自定义一个加签拦截器注解@Sign**来实现。下面以自定义@Sign拦截注解为例进行说明。

自定义@Sign注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@InterceptMark
public @interface Sign {
    /**
     * 密钥key
     * 支持占位符形式配置。
     *
     * @return
     */
    String accessKeyId();

    /**
     * 密钥
     * 支持占位符形式配置。
     *
     * @return
     */
    String accessKeySecret();

    /**
     * 拦截器匹配路径
     *
     * @return
     */
    String[] include() default {"/**"};

    /**
     * 拦截器排除匹配,排除指定路径拦截
     *
     * @return
     */
    String[] exclude() default {};

    /**
     * 处理该注解的拦截器类
     * 优先从spring容器获取对应的Bean,若是获取不到,则使用反射建立一个!
     *
     * @return
     */
    Class<? extends BasePathMatchInterceptor> handler() default SignInterceptor.class;
}

扩展自定义拦截注解有如下2点须要注意:

  1. 自定义拦截注解必须使用@InterceptMark标记。

  2. 注解中必须包括include()、exclude()、handler()属性信息。

实现SignInterceptor

@Component
public class SignInterceptor extends BasePathMatchInterceptor {

    private String accessKeyId;

    private String accessKeySecret;

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    @Override
    public Response doIntercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request newReq = request.newBuilder()
                .addHeader("accessKeyId", accessKeyId)
                .addHeader("accessKeySecret", accessKeySecret)
                .build();
        return chain.proceed(newReq);
    }
}

上述accessKeyId和accessKeySecret字段值会依据@Sign注解的accessKeyId()和accessKeySecret()值自动注入,若是@Sign指定的是占位符形式的字符串,则会取配置属性值进行注入。另外,accessKeyId和accessKeySecret字段必须提供setter方法

接口上使用@Sign

@RetrofitClient(baseUrl = "${test.baseUrl}")
@Sign(accessKeyId = "${test.accessKeyId}", accessKeySecret = "${test.accessKeySecret}", exclude = {"/api/test/person"})
public interface HttpApi {

    @GET("person")
    Result<Person> getPerson(@Query("id") Long id);

    @POST("savePerson")
    Result<Person> savePerson(@Body Person person);
}

这样就能在指定url的请求上,自动加上签名信息了。

链接池管理

默认状况下,全部经过Retrofit发送的http请求都会使用max-idle-connections=5 keep-alive-second=300的默认链接池。固然,咱们也能够在配置文件中配置多个自定义的链接池,而后经过@RetrofitClient的poolName属性来指定使用。好比咱们要让某个接口下的请求所有使用poolName=test1的链接池,代码实现以下:

  1. 配置链接池。 retrofit: # 链接池配置 pool: test1: max-idle-connections: 3 keep-alive-second: 100 test2: max-idle-connections: 5 keep-alive-second: 50 复制代码

  2. 经过@RetrofitClient的poolName属性来指定使用的链接池。 @RetrofitClient(baseUrl = "${test.baseUrl}", poolName="test1") public interface HttpApi { @GET("person") Result<Person> getPerson(@Query("id") Long id); } 复制代码

日志打印

不少状况下,咱们但愿将http请求日志记录下来。经过@RetrofitClient的logLevel和logStrategy属性,您能够指定每一个接口的日志打印级别以及日志打印策略。retrofit-spring-boot-starter支持了5种日志打印级别(ERROR, WARN, INFO, DEBUG, TRACE),默认INFO;支持了4种日志打印策略(NONE, BASIC, HEADERS, BODY),默认BASIC。4种日志打印策略含义以下:

  1. NONE:No logs.

  2. BASIC:Logs request and response lines.

  3. HEADERS:Logs request and response lines and their respective headers.

  4. BODY:Logs request and response lines and their respective headers and bodies (if present).

retrofit-spring-boot-starter默认使用了DefaultLoggingInterceptor执行真正的日志打印功能,其底层就是okhttp原生的HttpLoggingInterceptor。固然,你也能够自定义实现本身的日志打印拦截器,只须要继承BaseLoggingInterceptor(具体能够参考DefaultLoggingInterceptor的实现),而后在配置文件中进行相关配置便可。

retrofit:
  # 日志打印拦截器
  logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor

Http异常信息格式化器

当出现http请求异常时,原始的异常信息可能阅读起来并不友好,所以retrofit-spring-boot-starter提供了Http异常信息格式化器,用来美化输出http请求参数,默认使用DefaultHttpExceptionMessageFormatter进行请求数据格式化。你也能够进行自定义,只须要继承BaseHttpExceptionMessageFormatter,再进行相关配置便可。

retrofit:
  # Http异常信息格式化器
  http-exception-message-formatter: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultHttpExceptionMessageFormatter

调用适配器 CallAdapter

Retrofit能够经过调用适配器CallAdapterFactory将Call<T>对象适配成接口方法的返回值类型。retrofit-spring-boot-starter扩展2种CallAdapterFactory实现:

  1. BodyCallAdapterFactory 默认启用,可经过配置retrofit.enable-body-call-adapter=false关闭 同步执行http请求,将响应体内容适配成接口方法的返回值类型实例。 除了Retrofit.Call<T>、Retrofit.Response<T>、java.util.concurrent.CompletableFuture<T>以外,其它返回类型均可以使用该适配器。

  2. ResponseCallAdapterFactory 默认启用,可经过配置retrofit.enable-response-call-adapter=false关闭 同步执行http请求,将响应体内容适配成Retrofit.Response<T>返回。 若是方法的返回值类型为Retrofit.Response<T>,则可使用该适配器。

Retrofit自动根据方法返回值类型选用对应的CallAdapterFactory执行适配处理!加上Retrofit默认的CallAdapterFactory,可支持多种形式的方法返回值类型:

  • Call<T>: 不执行适配处理,直接返回Call<T>对象

  • CompletableFuture<T>: 将响应体内容适配成CompletableFuture<T>对象返回

  • Void: 不关注返回类型可使用Void。若是http状态码不是2xx,直接抛错!

  • Response<T>: 将响应内容适配成Response<T>对象返回

  • 其余任意Java类型: 将响应体内容适配成一个对应的Java类型对象返回,若是http状态码不是2xx,直接抛错!

    /**
     * Call<T>
     * 不执行适配处理,直接返回Call<T>对象
     * @param id
     * @return
     */
    @GET("person")
    Call<Result<Person>> getPersonCall(@Query("id") Long id);

    /**
     *  CompletableFuture<T>
     *  将响应体内容适配成CompletableFuture<T>对象返回
     * @param id
     * @return
     */
    @GET("person")
    CompletableFuture<Result<Person>> getPersonCompletableFuture(@Query("id") Long id);

    /**
     * Void
     * 不关注返回类型可使用Void。若是http状态码不是2xx,直接抛错!
     * @param id
     * @return
     */
    @GET("person")
    Void getPersonVoid(@Query("id") Long id);

    /**
     *  Response<T>
     *  将响应内容适配成Response<T>对象返回
     * @param id
     * @return
     */
    @GET("person")
    Response<Result<Person>> getPersonResponse(@Query("id") Long id);

    /**
     * 其余任意Java类型
     * 将响应体内容适配成一个对应的Java类型对象返回,若是http状态码不是2xx,直接抛错!
     * @param id
     * @return
     */
    @GET("person")
    Result<Person> getPerson(@Query("id") Long id);

咱们也能够经过继承CallAdapter.Factory扩展实现本身的CallAdapter;而后将自定义的CallAdapterFactory配置成spring的bean!

自定义配置的CallAdapter.Factory优先级更高!

数据转码器 Converter

Retrofi使用Converter将@Body注解标注的对象转换成请求体,将响应体数据转换成一个Java对象,能够选用如下几种Converter:

  • Gson: com.squareup.Retrofit:converter-gson

  • Jackson: com.squareup.Retrofit:converter-jackson

  • Moshi: com.squareup.Retrofit:converter-moshi

  • Protobuf: com.squareup.Retrofit:converter-protobuf

  • Wire: com.squareup.Retrofit:converter-wire

  • Simple XML: com.squareup.Retrofit:converter-simplexml

retrofit-spring-boot-starter默认使用的是jackson进行序列化转换!若是须要使用其它序列化方式,在项目中引入对应的依赖,再把对应的ConverterFactory配置成spring的bean便可

咱们也能够经过继承Converter.Factory扩展实现本身的Converter;而后将自定义的Converter.Factory配置成spring的bean!

自定义配置的Converter.Factory优先级更高!

全局拦截器 BaseGlobalInterceptor

若是咱们须要对整个系统的的http请求执行统一的拦截处理,能够自定义实现全局拦截器BaseGlobalInterceptor, 并配置成spring中的bean!例如咱们须要在整个系统发起的http请求,都带上来源信息。

@Component
public class SourceInterceptor extends BaseGlobalInterceptor {
    @Override
    public Response doIntercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request newReq = request.newBuilder()
                .addHeader("source", "test")
                .build();
        return chain.proceed(newReq);
    }
}

结语

至此,spring-boot项目下最优雅的http客户端工具介绍就结束了


推荐阅读:

牛皮了,马士兵老师全网首播阿里P8级技术、实现大型淘宝实战落

面试美团被JVM惨虐?阿里P9架构师用500分钟把JVM从入门讲到实战#合集

清华启蒙架构师马士兵针对应届生到开发十年的Java程序员作职业把脉

马士兵教育:Spring源码实战全集,资深架构师带你搞懂Spring源码底层从入门到入坟

阿里P9架构师120分钟带你掌握线程池,不在为线程而烦恼

相关文章
相关标签/搜索