Skip to content

Commit 8eab882

Browse files
自定义重试拦截器支持Spring Bean形式
1 parent ebe19b9 commit 8eab882

File tree

11 files changed

+174
-10
lines changed

11 files changed

+174
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
5252
<dependency>
5353
<groupId>com.github.lianjiatech</groupId>
5454
<artifactId>retrofit-spring-boot-starter</artifactId>
55-
<version>2.2.19</version>
55+
<version>2.2.20</version>
5656
</dependency>
5757
```
5858

@@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
6767
<dependency>
6868
<groupId>com.github.lianjiatech</groupId>
6969
<artifactId>retrofit-spring-boot-starter</artifactId>
70-
<version>2.2.19</version>
70+
<version>2.2.20</version>
7171
</dependency>
7272
<dependency>
7373
<groupId>com.squareup.okhttp3</groupId>

README_EN.md

Lines changed: 2 additions & 2 deletions
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.19</version>
46+
<version>2.2.20</version>
4747
</dependency>
4848
```
4949

@@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
5353
<dependency>
5454
<groupId>com.github.lianjiatech</groupId>
5555
<artifactId>retrofit-spring-boot-starter</artifactId>
56-
<version>2.2.19</version>
56+
<version>2.2.20</version>
5757
</dependency>
5858
<dependency>
5959
<groupId>com.squareup.okhttp3</groupId>

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.19</version>
9+
<version>2.2.20</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/config/RetrofitAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In
9292
// retryInterceptor
9393
RetryProperty retry = retrofitProperties.getRetry();
9494
Class<? extends BaseRetryInterceptor> retryInterceptor = retry.getRetryInterceptor();
95-
BaseRetryInterceptor retryInterceptorInstance = retryInterceptor.newInstance();
95+
BaseRetryInterceptor retryInterceptorInstance = ApplicationContextUtils.getBean(applicationContext, retryInterceptor);
96+
if (retryInterceptorInstance == null) {
97+
retryInterceptorInstance = retryInterceptor.newInstance();
98+
}
9699
BeanUtils.copyProperties(retry, retryInterceptorInstance);
97100
retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance);
98101

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.test;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
7+
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result;
8+
import com.github.lianjiatech.retrofit.spring.boot.test.http.InterceptApi;
9+
import okhttp3.mockwebserver.MockResponse;
10+
import okhttp3.mockwebserver.MockWebServer;
11+
import org.junit.After;
12+
import org.junit.Assert;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.test.context.junit4.SpringRunner;
19+
20+
import java.io.IOException;
21+
22+
/**
23+
* @author 陈添明
24+
* @summary
25+
* @since 2022/1/21 4:20 下午
26+
*/
27+
@SpringBootTest(classes = RetrofitTestApplication.class)
28+
@RunWith(SpringRunner.class)
29+
public class InterceptTest {
30+
31+
@Autowired
32+
private InterceptApi interceptApi;
33+
34+
private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
35+
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
36+
37+
38+
private MockWebServer server;
39+
40+
@Before
41+
public void before() throws IOException {
42+
System.out.println("=========开启MockWebServer===========");
43+
server = new MockWebServer();
44+
server.start(8080);
45+
46+
}
47+
48+
@After
49+
public void after() throws IOException {
50+
System.out.println("=========关闭MockWebServer===========");
51+
server.close();
52+
}
53+
54+
@Test
55+
public void test() throws IOException {
56+
// mock
57+
Person mockPerson = new Person().setId(1L)
58+
.setName("test")
59+
.setAge(10);
60+
Result mockResult = new Result<>()
61+
.setCode(0)
62+
.setMsg("ok")
63+
.setData(mockPerson);
64+
MockResponse response = new MockResponse()
65+
.setResponseCode(200)
66+
.addHeader("Content-Type", "application/json; charset=utf-8")
67+
.addHeader("Cache-Control", "no-cache")
68+
.setBody(objectMapper.writeValueAsString(mockResult));
69+
server.enqueue(response);
70+
71+
Result<Person> person = interceptApi.getPerson(1L);
72+
Person data = person.getData();
73+
Assert.assertNotNull(data);
74+
Assert.assertEquals("test", data.getName());
75+
Assert.assertEquals(10, data.getAge().intValue());
76+
}
77+
}

src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitExceptionTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public void testHttpResponseFailure() throws IOException {
8181
}
8282

8383
@Test(expected = Throwable.class)
84-
// @Test
8584
public void testIOException() {
8685
Person person = new Person().setId(1L).setName("test").setAge(10);
8786
Person error = httpApi.error(person);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public interface HttpApi {
105105
* @return .
106106
*/
107107
@POST("error")
108-
@Retry(enable = false)
109108
Person error(@Body Person person);
110109

111110
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.test.http;
2+
3+
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
4+
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
5+
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result;
6+
import retrofit2.http.GET;
7+
import retrofit2.http.Query;
8+
9+
/**
10+
* @author 陈添明
11+
* @summary
12+
* @since 2022/1/21 4:19 下午
13+
*/
14+
@RetrofitClient(baseUrl = "${test.baseUrl}")
15+
public interface InterceptApi {
16+
17+
18+
/**
19+
* 其他任意Java类型 <br>
20+
* 将响应体内容适配成一个对应的Java类型对象返回,如果http状态码不是2xx,直接抛错!<br>
21+
*
22+
* @param id id
23+
* @return 其他任意Java类型
24+
*/
25+
@GET("person")
26+
Result<Person> getPerson(@Query("id") Long id);
27+
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor;
2+
3+
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
4+
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryRule;
5+
import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService;
6+
import okhttp3.Response;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* @author 陈添明
14+
* @summary
15+
* @since 2022/1/21 4:52 下午
16+
*/
17+
@Component
18+
public class CustomRetryInterceptor extends BaseRetryInterceptor {
19+
20+
@Autowired
21+
private TestService testService;
22+
23+
@Override
24+
protected Response retryIntercept(int maxRetries, int intervalMs, RetryRule[] retryRules, Chain chain) {
25+
System.out.println("=============执行重试=============");
26+
testService.test();
27+
try {
28+
return chain.proceed(chain.request());
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
return null;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor;
22

33
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor;
4+
import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService;
45
import okhttp3.Request;
56
import okhttp3.Response;
7+
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.stereotype.Component;
79

810
import java.io.IOException;
911

1012
@Component
11-
public class SourceInterceptor extends BaseGlobalInterceptor {
13+
public class SourceGlobalInterceptor extends BaseGlobalInterceptor {
14+
15+
@Autowired
16+
private TestService testService;
17+
1218
@Override
1319
public Response doIntercept(Chain chain) throws IOException {
1420
Request request = chain.request();
1521
Request newReq = request.newBuilder()
1622
.addHeader("source", "test")
1723
.build();
24+
System.out.println("===========执行全局重试===========");
25+
testService.test();
1826
return chain.proceed(newReq);
1927
}
20-
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.test.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* @author 陈添明
7+
* @summary
8+
* @since 2022/1/21 4:29 下午
9+
*/
10+
@Service
11+
public class TestService {
12+
13+
14+
public void test() {
15+
}
16+
}

0 commit comments

Comments
 (0)