From f119d4c782ca43629454dbc7bc9c64ff40f444ca Mon Sep 17 00:00:00 2001 From: chentianming Date: Wed, 30 Mar 2022 23:36:03 +0800 Subject: [PATCH] GlobalInterceptor and NetworkInterceptor support Order --- README.md | 4 +- README_EN.md | 8 +-- pom.xml | 2 +- .../config/RetrofitAutoConfiguration.java | 54 ++++++++------- .../boot/config/RetrofitConfigBean.java | 31 ++++----- .../spring/boot/core/RetrofitFactoryBean.java | 69 +++++++++++++------ .../interceptor/BaseGlobalInterceptor.java | 29 -------- .../GlobalAndNetworkInterceptorFinder.java | 33 +++++++++ .../boot/interceptor/GlobalInterceptor.java | 11 +++ .../boot/util/ApplicationContextUtils.java | 13 ---- .../spring/boot/test/RetrofitStarterTest.java | 41 ++++++----- .../interceptor/SourceGlobalInterceptor.java | 18 +++-- .../TestPriorityGlobalInterceptor.java | 24 +++++++ 13 files changed, 199 insertions(+), 138 deletions(-) delete mode 100644 src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BaseGlobalInterceptor.java create mode 100644 src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalAndNetworkInterceptorFinder.java create mode 100644 src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalInterceptor.java create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/TestPriorityGlobalInterceptor.java diff --git a/README.md b/README.md index d677c8f..a626620 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.21 + 2.2.22 ``` @@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.21 + 2.2.22 com.squareup.okhttp3 diff --git a/README_EN.md b/README_EN.md index 68402ec..bfa0233 100644 --- a/README_EN.md +++ b/README_EN.md @@ -42,8 +42,8 @@ ```xml com.github.lianjiatech - retrofit-spring-boot-starter - 2.2.21 + retrofit-spring-boot-starter + 2.2.22 ``` @@ -52,8 +52,8 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions. ```xml com.github.lianjiatech - retrofit-spring-boot-starter - 2.2.21 + retrofit-spring-boot-starter + 2.2.22 com.squareup.okhttp3 diff --git a/pom.xml b/pom.xml index 8d4fd88..7b7e2cd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.21 + 2.2.22 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 22ca79b..34c3824 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 @@ -1,17 +1,9 @@ package com.github.lianjiatech.retrofit.spring.boot.config; -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.core.*; -import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser; -import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer; -import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor; -import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor; -import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor; -import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor; -import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils; -import okhttp3.ConnectionPool; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -27,15 +19,27 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; + +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.core.AutoConfiguredRetrofitScannerRegistrar; +import com.github.lianjiatech.retrofit.spring.boot.core.NoValidServiceInstanceChooser; +import com.github.lianjiatech.retrofit.spring.boot.core.PrototypeInterceptorBdfProcessor; +import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitFactoryBean; +import com.github.lianjiatech.retrofit.spring.boot.core.ServiceInstanceChooser; +import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser; +import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalAndNetworkInterceptorFinder; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils; + +import okhttp3.ConnectionPool; import retrofit2.CallAdapter; import retrofit2.Converter; import retrofit2.converter.jackson.JacksonConverterFactory; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - /** * @author 陈添明 */ @@ -60,10 +64,16 @@ public static PrototypeInterceptorBdfProcessor prototypeInterceptorBdfProcessor( } } + @Bean + public GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder() { + return new GlobalAndNetworkInterceptorFinder(); + } + @Bean @ConditionalOnMissingBean public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, InstantiationException { - RetrofitConfigBean retrofitConfigBean = new RetrofitConfigBean(retrofitProperties); + RetrofitConfigBean retrofitConfigBean = + new RetrofitConfigBean(retrofitProperties, globalAndNetworkInterceptorFinder()); // Initialize the connection pool Map poolRegistry = new ConcurrentHashMap<>(4); Map pool = retrofitProperties.getPool(); @@ -85,10 +95,6 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In Class[] globalConverterFactories = retrofitProperties.getGlobalConverterFactories(); retrofitConfigBean.setGlobalConverterFactoryClasses(globalConverterFactories); - // globalInterceptors - Collection globalInterceptors = ApplicationContextUtils.getBeans(applicationContext, BaseGlobalInterceptor.class); - retrofitConfigBean.setGlobalInterceptors(globalInterceptors); - // retryInterceptor RetryProperty retry = retrofitProperties.getRetry(); Class retryInterceptor = retry.getRetryInterceptor(); @@ -99,10 +105,6 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In BeanUtils.copyProperties(retry, retryInterceptorInstance); retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance); - // add networkInterceptor - Collection networkInterceptors = ApplicationContextUtils.getBeans(applicationContext, NetworkInterceptor.class); - retrofitConfigBean.setNetworkInterceptors(networkInterceptors); - // add ServiceInstanceChooserInterceptor ServiceInstanceChooser serviceInstanceChooser; try { diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitConfigBean.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitConfigBean.java index 6dbc56a..4f55b4f 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitConfigBean.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitConfigBean.java @@ -1,17 +1,19 @@ package com.github.lianjiatech.retrofit.spring.boot.config; +import java.util.List; +import java.util.Map; + import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser; -import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalAndNetworkInterceptorFinder; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor; import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor; import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor; import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor; + import okhttp3.ConnectionPool; import retrofit2.CallAdapter; import retrofit2.Converter; -import java.util.Collection; -import java.util.Map; - /** * @author 陈添明 */ @@ -21,9 +23,9 @@ public class RetrofitConfigBean { private Map poolRegistry; - private Collection globalInterceptors; + private final List globalInterceptors; - private Collection networkInterceptors; + private final List networkInterceptors; private BaseRetryInterceptor retryInterceptor; @@ -40,8 +42,11 @@ public RetrofitProperties getRetrofitProperties() { } - public RetrofitConfigBean(RetrofitProperties retrofitProperties) { + public RetrofitConfigBean(RetrofitProperties retrofitProperties, + GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder) { this.retrofitProperties = retrofitProperties; + this.globalInterceptors = globalAndNetworkInterceptorFinder.getGlobalInterceptors(); + this.networkInterceptors = globalAndNetworkInterceptorFinder.getNetworkInterceptors(); } @@ -53,14 +58,10 @@ public void setPoolRegistry(Map poolRegistry) { this.poolRegistry = poolRegistry; } - public Collection getGlobalInterceptors() { + public List getGlobalInterceptors() { return globalInterceptors; } - public void setGlobalInterceptors(Collection globalInterceptors) { - this.globalInterceptors = globalInterceptors; - } - public BaseRetryInterceptor getRetryInterceptor() { return retryInterceptor; } @@ -69,14 +70,10 @@ public void setRetryInterceptor(BaseRetryInterceptor retryInterceptor) { this.retryInterceptor = retryInterceptor; } - public Collection getNetworkInterceptors() { + public List getNetworkInterceptors() { return networkInterceptors; } - public void setNetworkInterceptors(Collection networkInterceptors) { - this.networkInterceptors = networkInterceptors; - } - public ServiceInstanceChooserInterceptor getServiceInstanceChooserInterceptor() { return serviceInstanceChooserInterceptor; } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java index 8f693da..94fb2a3 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java @@ -1,18 +1,19 @@ package com.github.lianjiatech.retrofit.spring.boot.core; -import com.github.lianjiatech.retrofit.spring.boot.annotation.*; -import com.github.lianjiatech.retrofit.spring.boot.config.DegradeProperty; -import com.github.lianjiatech.retrofit.spring.boot.config.LogProperty; -import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitConfigBean; -import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties; -import com.github.lianjiatech.retrofit.spring.boot.degrade.*; -import com.github.lianjiatech.retrofit.spring.boot.interceptor.*; -import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils; -import com.github.lianjiatech.retrofit.spring.boot.util.BeanExtendUtils; -import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils; -import okhttp3.ConnectionPool; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; @@ -25,15 +26,43 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; + +import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercept; +import com.github.lianjiatech.retrofit.spring.boot.annotation.InterceptMark; +import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercepts; +import com.github.lianjiatech.retrofit.spring.boot.annotation.OkHttpClientBuilder; +import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.config.DegradeProperty; +import com.github.lianjiatech.retrofit.spring.boot.config.LogProperty; +import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitConfigBean; +import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties; +import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser; +import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade; +import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeStrategy; +import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType; +import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory; +import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRule; +import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer; +import com.github.lianjiatech.retrofit.spring.boot.degrade.SentinelDegradeInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseLoggingInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogLevel; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogStrategy; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils; +import com.github.lianjiatech.retrofit.spring.boot.util.BeanExtendUtils; +import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils; + +import okhttp3.ConnectionPool; +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; import retrofit2.CallAdapter; import retrofit2.Converter; import retrofit2.Retrofit; -import java.lang.annotation.Annotation; -import java.lang.reflect.*; -import java.util.*; -import java.util.concurrent.TimeUnit; - /** * @author 陈添明 */ @@ -279,7 +308,7 @@ private synchronized OkHttpClient getOkHttpClient(Class retrofitClientInterfa // Add the interceptor defined by the annotation on the interface List interceptors = new ArrayList<>(findInterceptorByAnnotation(retrofitClientInterfaceClass)); // add global interceptor - Collection globalInterceptors = retrofitConfigBean.getGlobalInterceptors(); + List globalInterceptors = retrofitConfigBean.getGlobalInterceptors(); if (!CollectionUtils.isEmpty(globalInterceptors)) { interceptors.addAll(globalInterceptors); } @@ -310,7 +339,7 @@ private synchronized OkHttpClient getOkHttpClient(Class retrofitClientInterfa okHttpClientBuilder.addNetworkInterceptor(loggingInterceptor); } - Collection networkInterceptors = retrofitConfigBean.getNetworkInterceptors(); + List networkInterceptors = retrofitConfigBean.getNetworkInterceptors(); if (!CollectionUtils.isEmpty(networkInterceptors)) { for (NetworkInterceptor networkInterceptor : networkInterceptors) { okHttpClientBuilder.addNetworkInterceptor(networkInterceptor); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BaseGlobalInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BaseGlobalInterceptor.java deleted file mode 100644 index 86dcaa0..0000000 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BaseGlobalInterceptor.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.lianjiatech.retrofit.spring.boot.interceptor; - -import okhttp3.Response; - -import java.io.IOException; - -/** - * okhttp全局拦截器 - * 使用抽象类,方便后面进行功能升级 - * - * @author 陈添明 - */ -public abstract class BaseGlobalInterceptor implements PrototypeInterceptor { - - - @Override - public final Response intercept(Chain chain) throws IOException { - return doIntercept(chain); - } - - /** - * do intercept - * - * @param chain interceptor chain - * @return http Response - * @throws IOException IOException - */ - protected abstract Response doIntercept(Chain chain) throws IOException; -} diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalAndNetworkInterceptorFinder.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalAndNetworkInterceptorFinder.java new file mode 100644 index 0000000..75c8306 --- /dev/null +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalAndNetworkInterceptorFinder.java @@ -0,0 +1,33 @@ +package com.github.lianjiatech.retrofit.spring.boot.interceptor; + +import java.util.Collections; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @author 陈添明 + * @since 2022/3/30 11:01 下午 + */ +public class GlobalAndNetworkInterceptorFinder { + + @Autowired(required = false) + private List globalInterceptors; + + @Autowired(required = false) + private List networkInterceptors; + + public List getGlobalInterceptors() { + if (globalInterceptors == null) { + return Collections.emptyList(); + } + return globalInterceptors; + } + + public List getNetworkInterceptors() { + if (networkInterceptors == null) { + return Collections.emptyList(); + } + return networkInterceptors; + } +} diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalInterceptor.java new file mode 100644 index 0000000..45ebe87 --- /dev/null +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalInterceptor.java @@ -0,0 +1,11 @@ +package com.github.lianjiatech.retrofit.spring.boot.interceptor; + +import okhttp3.Interceptor; + +/** + * + * @author 陈添明 + */ +public interface GlobalInterceptor extends Interceptor { + +} diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/ApplicationContextUtils.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/ApplicationContextUtils.java index 74f5e0f..4ab99df 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/ApplicationContextUtils.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/ApplicationContextUtils.java @@ -3,9 +3,6 @@ import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; -import java.util.Collection; -import java.util.Map; - /** * @author 陈添明 */ @@ -24,14 +21,4 @@ public static U getBean(ApplicationContext context, Class clz) { return null; } } - - public static Collection getBeans(ApplicationContext context, Class clz) { - try { - Map beanMap = context.getBeansOfType(clz); - return beanMap.values(); - } catch (BeansException e) { - // do nothing - } - return null; - } } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java index 4c2167b..8af32c7 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java @@ -1,5 +1,25 @@ package com.github.lianjiatech.retrofit.spring.boot.test; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -8,33 +28,15 @@ import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi; import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi2; import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi3; + import okhttp3.Request; import okhttp3.ResponseBody; 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.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutionException; - /** * @author 陈添明 */ @@ -77,6 +79,7 @@ public void after() throws IOException { @Test public void testGetPersonBody() throws Exception { + // mock Person mockPerson = new Person().setId(1L) .setName("test") diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java index 98cf13c..2813b25 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SourceGlobalInterceptor.java @@ -1,22 +1,26 @@ 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 java.io.IOException; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import java.io.IOException; +import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor; +import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService; + +import okhttp3.Request; +import okhttp3.Response; @Component -public class SourceGlobalInterceptor extends BaseGlobalInterceptor { +@Order(2) +public class SourceGlobalInterceptor implements GlobalInterceptor { @Autowired private TestService testService; @Override - public Response doIntercept(Chain chain) throws IOException { + public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request newReq = request.newBuilder() .addHeader("source", "test") diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/TestPriorityGlobalInterceptor.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/TestPriorityGlobalInterceptor.java new file mode 100644 index 0000000..0d68aa0 --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/TestPriorityGlobalInterceptor.java @@ -0,0 +1,24 @@ +package com.github.lianjiatech.retrofit.spring.boot.test.interceptor; + +import java.io.IOException; + +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor; + +import okhttp3.Response; + +/** + * @author 陈添明 + * @since 2022/3/9 7:39 下午 + */ +@Component +@Order(1) +public class TestPriorityGlobalInterceptor implements GlobalInterceptor { + + @Override + public Response intercept(Chain chain) throws IOException { + return chain.proceed(chain.request()); + } +}