美文网首页spring boot
13-springboot-retrofit工具

13-springboot-retrofit工具

作者: 16325 | 来源:发表于2020-07-20 16:56 被阅读0次

retrofit是基于okhttp的一个http客户端工具。基于接口注解的方式调用http接口,简化代码。

引入依赖

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

配置@RetrofitScan注解

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

@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中即可使用。

@Service
public class TestService {

    @Autowired
    private HttpApi httpApi;

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

注解式拦截器

很多时候,我们希望某个接口下的某些http请求执行统一的拦截处理逻辑。这个时候可以使用注解式拦截器。使用的步骤主要分为2步:

  • 继承BasePathMatchInterceptor编写拦截处理器;
  • 接口上使用@Intercept进行标注。

下面以给指定请求的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。

相关文章

  • 13-springboot-retrofit工具

    retrofit是基于okhttp的一个http客户端工具。基于接口注解的方式调用http接口,简化代码。 引入依...

  • 工具工具还是工具

    最近几天参加了一个数据分析的训练营,每天晚上8点钟开始,一直讲到10点多,老师很卖力,干货也很多。今天结营,就在这...

  • 2019-01-08 ps总结

    ps 抠图工具 套索工具 多边套索工具 文字工具 磁性套索工具 魔棒工具 渐变工具 蒙版 图章工具 alt 吸取颜...

  • 【工具箱-2-选区工具】

    【工具箱-2-选区工具】 【矩形选框工具组:】矩形选框工具、椭圆选框工具、单行选框工具、单列选框工具。 个人理解:...

  • AI 2019 Mac版常用快捷键大全

    移动工具:V 选取工具:A 钢笔工具:P 添加锚点工具:+ 删除锚点工具:- 文字工具:T 多边形工具:L 矩形、...

  • PS扣图

    1扣图工具 套索工具 多边套索工具 磁性套索工具 快速选择工具 魔棒工具 橡皮擦工具 背景橡皮擦工具 魔术橡皮擦工...

  • AI热键

    选择工具:v 直接选择工具:a 魔棒工具:y 套索工具:q 钢笔工具:p 转换描点工具:Ctrl+c 文字工具:t...

  • 工具?工具人?

    很多时候,我们发明工具的目的是方便工作,结果,适得其反,不仅没有方便,反而增添了工作量。原来往上级交个什么东西,都...

  • 工具的工具

    “一个人越是能够放弃一些东西,越是富有。”-《瓦尔登湖》 当人真正占有一些东西的时候,就成了它们的奴隶,尤其是那些...

  • 去觉知去用你的工具

    去觉知去用你的工具 在空性里, 身体是工具 思想是工具 情绪是工具 情感是工具 人性是工具 …… 发现这些工具 去...

网友评论

    本文标题:13-springboot-retrofit工具

    本文链接:https://www.haomeiwen.com/subject/tkeykktx.html