Skip to content

Commit 6e0c89a

Browse files
@intercept支持多拦截器
1 parent 2e45332 commit 6e0c89a

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>com.github.lianjiatech</groupId>
4444
<artifactId>retrofit-spring-boot-starter</artifactId>
45-
<version>2.2.1</version>
45+
<version>2.2.2</version>
4646
</dependency>
4747
```
4848

@@ -179,13 +179,15 @@ public interface HttpApi3 {
179179

180180
### 注解式拦截器
181181

182-
很多时候,我们希望某个接口下的某些http请求执行统一的拦截处理逻辑。为了支持这个功能,`retrofit-spring-boot-starter`提供了**注解式拦截器**,同时做到了**基于url路径的匹配拦截**。使用的步骤主要分为2步:
182+
很多时候,我们希望某个接口下的某些http请求执行统一的拦截处理逻辑。为了支持这个功能,`retrofit-spring-boot-starter`提供了**注解式拦截器**,做到了**基于url路径的匹配拦截**。使用的步骤主要分为2步:
183183

184184
1. 继承`BasePathMatchInterceptor`编写拦截处理器;
185185
2. 接口上使用`@Intercept`进行标注。
186186

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

189+
> 如需配置多个拦截器,在接口上标注多个`@Intercept`注解即可!
190+
189191
#### 继承`BasePathMatchInterceptor`编写拦截处理器
190192

191193
```java

README_EN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>com.github.lianjiatech</groupId>
4545
<artifactId>retrofit-spring-boot-starter</artifactId>
46-
<version>2.2.1</version>
46+
<version>2.2.2</version>
4747
</dependency>
4848
```
4949

@@ -181,6 +181,8 @@ In many cases, we hope that certain http requests in a certain interface execute
181181
1. Inherit `BasePathMatchInterceptor` and write interceptor processor;
182182
2. Mark the interface with `@Intercept`.
183183

184+
> To configure multiple interceptors, just mark multiple `@Intercept` annotations on the interface!
185+
184186
The following is an example of how to use annotation interceptors *by splicing timestamp after the URL of a specified request*.
185187

186188
#### Inherit `BasePathMatchInterceptor` and write interceptor processor

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.lianjiatech</groupId>
88
<artifactId>retrofit-spring-boot-starter</artifactId>
9-
<version>2.2.1</version>
9+
<version>2.2.2</version>
1010

1111
<name>retrofit-spring-boot-starter</name>
1212
<description>retrofit-spring-boot-starter</description>

src/main/java/com/github/lianjiatech/retrofit/spring/boot/annotation/Intercept.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
@Retention(RetentionPolicy.RUNTIME)
1515
@Target(ElementType.TYPE)
16-
@Documented
1716
@InterceptMark
1817
@Repeatable(Intercepts.class)
1918
public @interface Intercept {

src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.github.lianjiatech.retrofit.spring.boot.core;
22

3-
import com.github.lianjiatech.retrofit.spring.boot.annotation.InterceptMark;
4-
import com.github.lianjiatech.retrofit.spring.boot.annotation.OkHttpClientBuilder;
5-
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
3+
import com.github.lianjiatech.retrofit.spring.boot.annotation.*;
64
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitConfigBean;
75
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties;
86
import com.github.lianjiatech.retrofit.spring.boot.degrade.*;
@@ -332,6 +330,12 @@ private List<Interceptor> findInterceptorByAnnotation(Class<?> retrofitClientInt
332330
if (annotationType.isAnnotationPresent(InterceptMark.class)) {
333331
interceptAnnotations.add(classAnnotation);
334332
}
333+
if (classAnnotation instanceof Intercepts) {
334+
Intercept[] value = ((Intercepts) classAnnotation).value();
335+
for (Intercept intercept : value) {
336+
interceptAnnotations.add(intercept);
337+
}
338+
}
335339
}
336340
for (Annotation interceptAnnotation : interceptAnnotations) {
337341
// 获取注解属性数据。Get annotation attribute data

0 commit comments

Comments
 (0)