Skip to content

Commit f1a2c44

Browse files
Merge pull request #41 from LianjiaTech/develop
Develop
2 parents d71cffd + 6e0c89a commit f1a2c44

File tree

8 files changed

+61
-8
lines changed

8 files changed

+61
-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*/
1414
@Retention(RetentionPolicy.RUNTIME)
1515
@Target(ElementType.TYPE)
16-
@Documented
1716
@InterceptMark
17+
@Repeatable(Intercepts.class)
1818
public @interface Intercept {
1919
/**
2020
* 拦截器匹配路径pattern
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* @author 陈添明
7+
*/
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface Intercepts {
11+
12+
Intercept[] value();
13+
}

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

src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result;
77
import com.github.lianjiatech.retrofit.spring.boot.test.ex.TestErrorDecoder;
88
import com.github.lianjiatech.retrofit.spring.boot.test.interceptor.Sign;
9+
import com.github.lianjiatech.retrofit.spring.boot.test.interceptor.TimeStamp2Interceptor;
910
import com.github.lianjiatech.retrofit.spring.boot.test.interceptor.TimeStampInterceptor;
1011
import retrofit2.Call;
1112
import retrofit2.Response;
@@ -22,6 +23,7 @@
2223
@RetrofitClient(baseUrl = "${test.baseUrl}", poolName = "test1", errorDecoder = TestErrorDecoder.class)
2324
@Sign(accessKeyId = "${test.accessKeyId}", accessKeySecret = "${test.accessKeySecret}", exclude = {"/api/test/query"})
2425
@Intercept(handler = TimeStampInterceptor.class)
26+
@Intercept(handler = TimeStamp2Interceptor.class)
2527
public interface HttpApi {
2628

2729
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor;
2+
3+
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
4+
import okhttp3.HttpUrl;
5+
import okhttp3.Request;
6+
import okhttp3.Response;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.io.IOException;
10+
11+
/**
12+
* @author 陈添明
13+
*/
14+
@Component
15+
public class TimeStamp2Interceptor extends BasePathMatchInterceptor {
16+
17+
@Override
18+
public Response doIntercept(Chain chain) throws IOException {
19+
Request request = chain.request();
20+
HttpUrl url = request.url();
21+
long timestamp = System.currentTimeMillis();
22+
HttpUrl newUrl = url.newBuilder()
23+
.addQueryParameter("timestamp2", String.valueOf(timestamp))
24+
.build();
25+
Request newRequest = request.newBuilder()
26+
.url(newUrl)
27+
.build();
28+
return chain.proceed(newRequest);
29+
}
30+
}

0 commit comments

Comments
 (0)