From 878e3a89d5ab3315125800474a0e8d4370099dde Mon Sep 17 00:00:00 2001 From: chentianming Date: Thu, 5 Aug 2021 18:42:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9C=AA=E9=85=8D=E7=BD=AEbaseUrl=E5=BF=BD?= =?UTF-8?q?=E8=A7=86=E8=AF=A5=E6=8E=A5=E5=8F=A3?= 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 +- ...utoConfiguredRetrofitScannerRegistrar.java | 12 +++++++-- .../core/ClassPathRetrofitClientScanner.java | 26 +++++++++++++++++-- .../core/RetrofitClientScannerRegistrar.java | 13 ++++++++-- .../boot/test/http/IllegalBaseUrlTestApi.java | 19 ++++++++++++++ 7 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java diff --git a/README.md b/README.md index e43efa3..d9c5504 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.12 + 2.2.13 ``` @@ -63,7 +63,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.12 + 2.2.13 com.squareup.okhttp3 diff --git a/README_EN.md b/README_EN.md index f87ad85..bb183ae 100644 --- a/README_EN.md +++ b/README_EN.md @@ -43,7 +43,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.12 + 2.2.13 ``` @@ -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.12 + 2.2.13 com.squareup.okhttp3 diff --git a/pom.xml b/pom.xml index 38aa1b7..90da40a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.12 + 2.2.13 retrofit-spring-boot-starter retrofit-spring-boot-starter diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java index 2fb72d0..00902d4 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java @@ -9,8 +9,10 @@ import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.boot.autoconfigure.AutoConfigurationPackages; +import org.springframework.context.EnvironmentAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; @@ -23,7 +25,7 @@ * * @author 陈添明 */ -public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware { +public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { private static final Logger logger = LoggerFactory.getLogger(AutoConfiguredRetrofitScannerRegistrar.class); @@ -32,6 +34,7 @@ public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, private ResourceLoader resourceLoader; private ClassLoader classLoader; + private Environment environment; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { @@ -54,7 +57,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B } // Scan the @RetrofitClient annotated interface under the specified path and register it to the BeanDefinitionRegistry - ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader); + ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader, environment); if (resourceLoader != null) { scanner.setResourceLoader(resourceLoader); } @@ -75,4 +78,9 @@ public void setResourceLoader(ResourceLoader resourceLoader) { public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java index 7e4300c..487cae2 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java @@ -8,8 +8,10 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; +import org.springframework.core.env.Environment; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; import java.util.Arrays; import java.util.Objects; @@ -23,10 +25,12 @@ public class ClassPathRetrofitClientScanner extends ClassPathBeanDefinitionScann private final ClassLoader classLoader; private final static Logger logger = LoggerFactory.getLogger(ClassPathRetrofitClientScanner.class); + private final Environment environment; - public ClassPathRetrofitClientScanner(BeanDefinitionRegistry registry, ClassLoader classLoader) { + public ClassPathRetrofitClientScanner(BeanDefinitionRegistry registry, ClassLoader classLoader, Environment environment) { super(registry, false); this.classLoader = classLoader; + this.environment = environment; } public void registerFilters() { @@ -54,7 +58,8 @@ protected boolean isCandidateComponent( Class target = ClassUtils.forName( beanDefinition.getMetadata().getClassName(), classLoader); - return !target.isAnnotation(); + + return !target.isAnnotation() && legalBaseUrl(target); } catch (Exception ex) { logger.error("load class exception:", ex); } @@ -62,6 +67,23 @@ protected boolean isCandidateComponent( return false; } + private boolean legalBaseUrl(Class target) { + final RetrofitClient retrofitClient = target.getAnnotation(RetrofitClient.class); + final String baseUrl = retrofitClient.baseUrl(); + if (StringUtils.isEmpty(baseUrl)) { + logger.warn("No config baseUrl! interface={}", target); + return false; + } + + try { + environment.resolveRequiredPlaceholders(baseUrl); + } catch (Exception e) { + logger.warn("No config baseUrl! interface={}", target); + return false; + } + return true; + } + private void processBeanDefinitions(Set beanDefinitions) { GenericBeanDefinition definition; diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java index bc846ed..4ac3d41 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java @@ -3,9 +3,11 @@ import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitScan; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.EnvironmentAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.Assert; @@ -19,12 +21,14 @@ /** * @author 陈添明 */ -public class RetrofitClientScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware { +public class RetrofitClientScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { private ResourceLoader resourceLoader; private ClassLoader classLoader; + private Environment environment; + @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { AnnotationAttributes attributes = AnnotationAttributes @@ -33,7 +37,7 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR return; } // Scan the @RetrofitClient annotated interface under the specified path and register it to the BeanDefinitionRegistry - ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader); + ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader, environment); if (resourceLoader != null) { scanner.setResourceLoader(resourceLoader); } @@ -78,4 +82,9 @@ public void setResourceLoader(ResourceLoader resourceLoader) { public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java new file mode 100644 index 0000000..6b187bd --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java @@ -0,0 +1,19 @@ +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.converter.gson.GsonConverterFactory; +import retrofit2.converter.jaxb.JaxbConverterFactory; +import retrofit2.http.GET; +import retrofit2.http.Query; + +/** + * @author 陈添明 + */ +@RetrofitClient(baseUrl = "${not.baseUrl}", converterFactories = {GsonConverterFactory.class, JaxbConverterFactory.class}) +public interface IllegalBaseUrlTestApi { + + @GET("person") + Result getPerson(@Query("id") Long id); +} From d43a90ae98d2a0eced3c5a349b0d5a7c2e2a56ed Mon Sep 17 00:00:00 2001 From: chentianming Date: Fri, 6 Aug 2021 11:47:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9C=AA=E9=85=8D=E7=BD=AEbaseUrl=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E6=9C=AA=E9=85=8D=E7=BD=AEserviceId=E5=BF=BD=E8=A7=86?= =?UTF-8?q?=E8=AF=A5=E6=8E=A5=E5=8F=A3?= 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 +- .../core/ClassPathRetrofitClientScanner.java | 19 ++++++++++++++++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d9c5504..9d33fd1 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.13 + 2.2.14 ``` @@ -63,7 +63,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.13 + 2.2.14 com.squareup.okhttp3 diff --git a/README_EN.md b/README_EN.md index bb183ae..6093711 100644 --- a/README_EN.md +++ b/README_EN.md @@ -43,7 +43,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.13 + 2.2.14 ``` @@ -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.13 + 2.2.14 com.squareup.okhttp3 diff --git a/pom.xml b/pom.xml index 90da40a..96212ec 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.13 + 2.2.14 retrofit-spring-boot-starter retrofit-spring-boot-starter diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java index 487cae2..f65cf7f 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java @@ -59,7 +59,7 @@ protected boolean isCandidateComponent( beanDefinition.getMetadata().getClassName(), classLoader); - return !target.isAnnotation() && legalBaseUrl(target); + return !target.isAnnotation() && (legalBaseUrl(target) || legalServiceId(target)); } catch (Exception ex) { logger.error("load class exception:", ex); } @@ -84,6 +84,23 @@ private boolean legalBaseUrl(Class target) { return true; } + private boolean legalServiceId(Class target) { + final RetrofitClient retrofitClient = target.getAnnotation(RetrofitClient.class); + final String serviceId = retrofitClient.serviceId(); + if (StringUtils.isEmpty(serviceId)) { + logger.warn("No config serviceId! interface={}", target); + return false; + } + + try { + environment.resolveRequiredPlaceholders(serviceId); + } catch (Exception e) { + logger.warn("No config serviceId! interface={}", target); + return false; + } + return true; + } + private void processBeanDefinitions(Set beanDefinitions) { GenericBeanDefinition definition;