Skip to content

Commit

Permalink
自定义重试拦截器支持Spring Bean形式
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Jan 21, 2022
1 parent d4454a3 commit cad3089
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.19</version>
<version>2.2.20</version>
</dependency>
```

Expand All @@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.19</version>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.19</version>
<version>2.2.20</version>
</dependency>
```

Expand All @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.19</version>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.19</version>
<version>2.2.20</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In
// retryInterceptor
RetryProperty retry = retrofitProperties.getRetry();
Class<? extends BaseRetryInterceptor> retryInterceptor = retry.getRetryInterceptor();
BaseRetryInterceptor retryInterceptorInstance = retryInterceptor.newInstance();
BaseRetryInterceptor retryInterceptorInstance = ApplicationContextUtils.getBean(applicationContext, retryInterceptor);
if (retryInterceptorInstance == null) {
retryInterceptorInstance = retryInterceptor.newInstance();
}
BeanUtils.copyProperties(retry, retryInterceptorInstance);
retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.github.lianjiatech.retrofit.spring.boot.test;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result;
import com.github.lianjiatech.retrofit.spring.boot.test.http.InterceptApi;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

/**
* @author 陈添明
* @summary
* @since 2022/1/21 4:20 下午
*/
@SpringBootTest(classes = RetrofitTestApplication.class)
@RunWith(SpringRunner.class)
public class InterceptTest {

@Autowired
private InterceptApi interceptApi;

private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);


private MockWebServer server;

@Before
public void before() throws IOException {
System.out.println("=========开启MockWebServer===========");
server = new MockWebServer();
server.start(8080);

}

@After
public void after() throws IOException {
System.out.println("=========关闭MockWebServer===========");
server.close();
}

@Test
public void test() throws IOException {
// mock
Person mockPerson = new Person().setId(1L)
.setName("test")
.setAge(10);
Result mockResult = new Result<>()
.setCode(0)
.setMsg("ok")
.setData(mockPerson);
MockResponse response = new MockResponse()
.setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8")
.addHeader("Cache-Control", "no-cache")
.setBody(objectMapper.writeValueAsString(mockResult));
server.enqueue(response);

Result<Person> person = interceptApi.getPerson(1L);
Person data = person.getData();
Assert.assertNotNull(data);
Assert.assertEquals("test", data.getName());
Assert.assertEquals(10, data.getAge().intValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void testHttpResponseFailure() throws IOException {
}

@Test(expected = Throwable.class)
// @Test
public void testIOException() {
Person person = new Person().setId(1L).setName("test").setAge(10);
Person error = httpApi.error(person);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public interface HttpApi {
* @return .
*/
@POST("error")
@Retry(enable = false)
Person error(@Body Person person);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.lianjiatech.retrofit.spring.boot.test.http;

import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
* @author 陈添明
* @summary
* @since 2022/1/21 4:19 下午
*/
@RetrofitClient(baseUrl = "${test.baseUrl}")
public interface InterceptApi {


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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor;

import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryRule;
import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
* @author 陈添明
* @summary
* @since 2022/1/21 4:52 下午
*/
@Component
public class CustomRetryInterceptor extends BaseRetryInterceptor {

@Autowired
private TestService testService;

@Override
protected Response retryIntercept(int maxRetries, int intervalMs, RetryRule[] retryRules, Chain chain) {
System.out.println("=============执行重试=============");
testService.test();
try {
return chain.proceed(chain.request());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SourceInterceptor extends BaseGlobalInterceptor {
public class SourceGlobalInterceptor extends BaseGlobalInterceptor {

@Autowired
private TestService testService;

@Override
public Response doIntercept(Chain chain) throws IOException {
Request request = chain.request();
Request newReq = request.newBuilder()
.addHeader("source", "test")
.build();
System.out.println("===========执行全局重试===========");
testService.test();
return chain.proceed(newReq);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.lianjiatech.retrofit.spring.boot.test.service;

import org.springframework.stereotype.Service;

/**
* @author 陈添明
* @summary
* @since 2022/1/21 4:29 下午
*/
@Service
public class TestService {


public void test() {
}
}

0 comments on commit cad3089

Please sign in to comment.