From cad3089dbebccf17ddae1948c949f726202ea8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=BB=E6=98=8E?= Date: Fri, 21 Jan 2022 17:29:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E6=94=AF=E6=8C=81Spring=20Bean?= =?UTF-8?q?=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- README_EN.md | 4 +- pom.xml | 2 +- .../config/RetrofitAutoConfiguration.java | 5 +- .../spring/boot/test/InterceptTest.java | 77 +++++++++++++++++++ .../boot/test/RetrofitExceptionTest.java | 1 - .../spring/boot/test/http/HttpApi.java | 1 - .../spring/boot/test/http/InterceptApi.java | 28 +++++++ .../interceptor/CustomRetryInterceptor.java | 34 ++++++++ ...ptor.java => SourceGlobalInterceptor.java} | 12 ++- .../spring/boot/test/service/TestService.java | 16 ++++ 11 files changed, 174 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/InterceptTest.java create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/InterceptApi.java create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/CustomRetryInterceptor.java rename src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/{SourceInterceptor.java => SourceGlobalInterceptor.java} (61%) create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/service/TestService.java diff --git a/README.md b/README.md index 1bf4194..e42cc13 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.19 + 2.2.20 ``` @@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.19 + 2.2.20 com.squareup.okhttp3 diff --git a/README_EN.md b/README_EN.md index a4d9e8e..71533da 100644 --- a/README_EN.md +++ b/README_EN.md @@ -43,7 +43,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.19 + 2.2.20 ``` @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions. com.github.lianjiatech retrofit-spring-boot-starter - 2.2.19 + 2.2.20 com.squareup.okhttp3 diff --git a/pom.xml b/pom.xml index d8d5f71..dd02554 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.19 + 2.2.20 retrofit-spring-boot-starter retrofit-spring-boot-starter diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java index 72ade71..22ca79b 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java @@ -92,7 +92,10 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In // retryInterceptor RetryProperty retry = retrofitProperties.getRetry(); Class 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); diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/InterceptTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/InterceptTest.java new file mode 100644 index 0000000..300cd14 --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/InterceptTest.java @@ -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 = interceptApi.getPerson(1L); + Person data = person.getData(); + Assert.assertNotNull(data); + Assert.assertEquals("test", data.getName()); + Assert.assertEquals(10, data.getAge().intValue()); + } +} diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitExceptionTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitExceptionTest.java index 7f5d15f..09b5701 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitExceptionTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitExceptionTest.java @@ -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); diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java index 8e0cec6..1fb1210 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java @@ -105,7 +105,6 @@ public interface HttpApi { * @return . */ @POST("error") - @Retry(enable = false) Person error(@Body Person person); /** diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/InterceptApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/InterceptApi.java new file mode 100644 index 0000000..f21f3aa --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/InterceptApi.java @@ -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类型
+ * 将响应体内容适配成一个对应的Java类型对象返回,如果http状态码不是2xx,直接抛错!
+ * + * @param id id + * @return 其他任意Java类型 + */ + @GET("person") + Result getPerson(@Query("id") Long id); + +} diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/CustomRetryInterceptor.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/CustomRetryInterceptor.java new file mode 100644 index 0000000..0a0b1ae --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/CustomRetryInterceptor.java @@ -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; + } +} diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceInterceptor.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java similarity index 61% rename from src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceInterceptor.java rename to src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java index 34f5b47..98cf13c 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceInterceptor.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java @@ -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); } -} \ No newline at end of file +} diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/service/TestService.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/service/TestService.java new file mode 100644 index 0000000..4c396dd --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/service/TestService.java @@ -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() { + } +}