diff --git a/pom.xml b/pom.xml index 398728b..7f72f6b 100644 --- a/pom.xml +++ b/pom.xml @@ -46,9 +46,16 @@ 1.17.5 1.6.3 2.12.6.1 + 1.18.24 + + org.projectlombok + lombok + ${lombok.version} + provided + org.springframework.cloud spring-cloud-commons diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/DegradeProperty.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/DegradeProperty.java index 781ae48..21d8fea 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/DegradeProperty.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/DegradeProperty.java @@ -2,12 +2,14 @@ import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType; +import lombok.Data; + /** * @author 陈添明 */ +@Data public class DegradeProperty { - /** * 启用熔断降级 * enable degrade @@ -19,21 +21,4 @@ public class DegradeProperty { * degrade type */ private DegradeType degradeType = DegradeType.SENTINEL; - - - public boolean isEnable() { - return enable; - } - - public void setEnable(boolean enable) { - this.enable = enable; - } - - public DegradeType getDegradeType() { - return degradeType; - } - - public void setDegradeType(DegradeType degradeType) { - this.degradeType = degradeType; - } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/LogProperty.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/LogProperty.java index 0b38a54..9d3cd96 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/LogProperty.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/LogProperty.java @@ -5,9 +5,12 @@ import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogLevel; import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogStrategy; +import lombok.Data; + /** * @author 陈添明 */ +@Data public class LogProperty { /** @@ -16,7 +19,6 @@ public class LogProperty { */ private boolean enable = true; - /** * 全局日志打印级别,支持的日志级别参见{@link LogLevel} * Log printing level, see {@link LogLevel} for supported log levels @@ -29,42 +31,9 @@ public class LogProperty { */ private LogStrategy globalLogStrategy = LogStrategy.BASIC; - /** * 日志打印拦截器 * Log print Interceptor */ private Class loggingInterceptor = DefaultLoggingInterceptor.class; - - public boolean isEnable() { - return enable; - } - - public void setEnable(boolean enable) { - this.enable = enable; - } - - public LogLevel getGlobalLogLevel() { - return globalLogLevel; - } - - public void setGlobalLogLevel(LogLevel globalLogLevel) { - this.globalLogLevel = globalLogLevel; - } - - public LogStrategy getGlobalLogStrategy() { - return globalLogStrategy; - } - - public void setGlobalLogStrategy(LogStrategy globalLogStrategy) { - this.globalLogStrategy = globalLogStrategy; - } - - public Class getLoggingInterceptor() { - return loggingInterceptor; - } - - public void setLoggingInterceptor(Class loggingInterceptor) { - this.loggingInterceptor = loggingInterceptor; - } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/PoolConfig.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/PoolConfig.java index 41f9c8c..4786049 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/PoolConfig.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/PoolConfig.java @@ -1,39 +1,18 @@ package com.github.lianjiatech.retrofit.spring.boot.config; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + /** - * 连接池参数配置 - * Connection pool parameter configuration - * * @author 陈添明 */ +@Data +@NoArgsConstructor +@AllArgsConstructor public class PoolConfig { private int maxIdleConnections; - private long keepAliveSecond; - - public PoolConfig(int maxIdleConnections, long keepAliveSecond) { - this.maxIdleConnections = maxIdleConnections; - this.keepAliveSecond = keepAliveSecond; - } - - public PoolConfig() { - } - - public void setMaxIdleConnections(int maxIdleConnections) { - this.maxIdleConnections = maxIdleConnections; - } - - public void setKeepAliveSecond(long keepAliveSecond) { - this.keepAliveSecond = keepAliveSecond; - } - - public int getMaxIdleConnections() { - return maxIdleConnections; - } - - public long getKeepAliveSecond() { - return keepAliveSecond; - } } \ No newline at end of file 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 fe69f5f..36bd564 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,11 +1,10 @@ package com.github.lianjiatech.retrofit.spring.boot.config; +import java.util.HashMap; +import java.util.List; 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; import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; @@ -34,11 +33,13 @@ import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeInterceptor; import com.github.lianjiatech.retrofit.spring.boot.degrade.ResourceNameParser; import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegradeInterceptor; -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 com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils; +import lombok.extern.slf4j.Slf4j; import okhttp3.ConnectionPool; import retrofit2.CallAdapter; import retrofit2.Converter; @@ -50,10 +51,9 @@ @Configuration @EnableConfigurationProperties(RetrofitProperties.class) @AutoConfigureAfter({JacksonAutoConfiguration.class}) +@Slf4j public class RetrofitAutoConfiguration implements ApplicationContextAware { - private static final Logger logger = LoggerFactory.getLogger(RetrofitAutoConfiguration.class); - private final RetrofitProperties retrofitProperties; private ApplicationContext applicationContext; @@ -71,30 +71,27 @@ public static PrototypeInterceptorBdfProcessor prototypeInterceptorBdfProcessor( } } - @Bean - public GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder() { - return new GlobalAndNetworkInterceptorFinder(); - } - @Bean @ConditionalOnMissingBean public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) ResourceNameParser resourceNameParser, - @Autowired(required = false) DegradeInterceptor degradeInterceptor) + @Autowired(required = false) DegradeInterceptor degradeInterceptor, + @Autowired(required = false) List globalInterceptors, + @Autowired(required = false) List networkInterceptors) throws IllegalAccessException, InstantiationException { - RetrofitConfigBean retrofitConfigBean = - new RetrofitConfigBean(retrofitProperties, globalAndNetworkInterceptorFinder()); - // Initialize the connection pool - Map poolRegistry = new ConcurrentHashMap<>(4); - Map pool = retrofitProperties.getPool(); - if (pool != null) { - pool.forEach((poolName, poolConfig) -> { - long keepAliveSecond = poolConfig.getKeepAliveSecond(); - int maxIdleConnections = poolConfig.getMaxIdleConnections(); - ConnectionPool connectionPool = - new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS); - poolRegistry.put(poolName, connectionPool); - }); - } + RetrofitConfigBean retrofitConfigBean = new RetrofitConfigBean(retrofitProperties); + retrofitConfigBean.setGlobalInterceptors(globalInterceptors); + retrofitConfigBean.setNetworkInterceptors(networkInterceptors); + retrofitConfigBean.setResourceNameParser(resourceNameParser); + retrofitConfigBean.setDegradeInterceptor(degradeInterceptor); + + Map poolRegistry = new HashMap<>(4); + retrofitProperties.getPool().forEach((poolName, poolConfig) -> { + long keepAliveSecond = poolConfig.getKeepAliveSecond(); + int maxIdleConnections = poolConfig.getMaxIdleConnections(); + ConnectionPool connectionPool = + new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS); + poolRegistry.put(poolName, connectionPool); + }); retrofitConfigBean.setPoolRegistry(poolRegistry); // callAdapterFactory @@ -111,10 +108,7 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour RetryProperty retry = retrofitProperties.getRetry(); Class retryInterceptor = retry.getRetryInterceptor(); BaseRetryInterceptor retryInterceptorInstance = - ApplicationContextUtils.getBeanOrNull(applicationContext, retryInterceptor); - if (retryInterceptorInstance == null) { - retryInterceptorInstance = retryInterceptor.newInstance(); - } + ApplicationContextUtils.getBeanOrNew(applicationContext, retryInterceptor); BeanUtils.copyProperties(retry, retryInterceptorInstance); retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance); @@ -130,8 +124,7 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour new ServiceInstanceChooserInterceptor(serviceInstanceChooser); retrofitConfigBean.setServiceInstanceChooserInterceptor(serviceInstanceChooserInterceptor); - retrofitConfigBean.setResourceNameParser(resourceNameParser); - retrofitConfigBean.setDegradeInterceptor(degradeInterceptor); + return retrofitConfigBean; } @@ -176,7 +169,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws public static class RetrofitScannerRegistrarNotFoundConfiguration implements InitializingBean { @Override public void afterPropertiesSet() { - logger.debug("No {} found.", RetrofitFactoryBean.class.getName()); + log.debug("No {} found.", RetrofitFactoryBean.class.getName()); } } 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 d668244..348c3ba 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,16 +1,17 @@ package com.github.lianjiatech.retrofit.spring.boot.config; +import java.util.Collections; import java.util.List; import java.util.Map; import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeInterceptor; import com.github.lianjiatech.retrofit.spring.boot.degrade.ResourceNameParser; -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 lombok.Data; import okhttp3.ConnectionPool; import retrofit2.CallAdapter; import retrofit2.Converter; @@ -18,15 +19,16 @@ /** * @author 陈添明 */ +@Data public class RetrofitConfigBean { private final RetrofitProperties retrofitProperties; private Map poolRegistry; - private final List globalInterceptors; + private List globalInterceptors; - private final List networkInterceptors; + private List networkInterceptors; private BaseRetryInterceptor retryInterceptor; @@ -40,80 +42,21 @@ public class RetrofitConfigBean { private DegradeInterceptor degradeInterceptor; - public RetrofitProperties getRetrofitProperties() { - return retrofitProperties; - } - - - public RetrofitConfigBean(RetrofitProperties retrofitProperties, - GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder) { + public RetrofitConfigBean(RetrofitProperties retrofitProperties) { this.retrofitProperties = retrofitProperties; - this.globalInterceptors = globalAndNetworkInterceptorFinder.getGlobalInterceptors(); - this.networkInterceptors = globalAndNetworkInterceptorFinder.getNetworkInterceptors(); - } - - - public Map getPoolRegistry() { - return poolRegistry; - } - - public void setPoolRegistry(Map poolRegistry) { - this.poolRegistry = poolRegistry; } public List getGlobalInterceptors() { + if (globalInterceptors == null) { + return Collections.emptyList(); + } return globalInterceptors; } - public BaseRetryInterceptor getRetryInterceptor() { - return retryInterceptor; - } - - public void setRetryInterceptor(BaseRetryInterceptor retryInterceptor) { - this.retryInterceptor = retryInterceptor; - } - public List getNetworkInterceptors() { + if (networkInterceptors == null) { + return Collections.emptyList(); + } return networkInterceptors; } - - public ServiceInstanceChooserInterceptor getServiceInstanceChooserInterceptor() { - return serviceInstanceChooserInterceptor; - } - - public void setServiceInstanceChooserInterceptor(ServiceInstanceChooserInterceptor serviceInstanceChooserInterceptor) { - this.serviceInstanceChooserInterceptor = serviceInstanceChooserInterceptor; - } - - public Class[] getGlobalConverterFactoryClasses() { - return globalConverterFactoryClasses; - } - - public void setGlobalConverterFactoryClasses(Class[] globalConverterFactoryClasses) { - this.globalConverterFactoryClasses = globalConverterFactoryClasses; - } - - public Class[] getGlobalCallAdapterFactoryClasses() { - return globalCallAdapterFactoryClasses; - } - - public void setGlobalCallAdapterFactoryClasses(Class[] globalCallAdapterFactoryClasses) { - this.globalCallAdapterFactoryClasses = globalCallAdapterFactoryClasses; - } - - public ResourceNameParser getResourceNameParser() { - return resourceNameParser; - } - - public void setResourceNameParser(ResourceNameParser resourceNameParser) { - this.resourceNameParser = resourceNameParser; - } - - public DegradeInterceptor getDegradeInterceptor() { - return degradeInterceptor; - } - - public void setDegradeInterceptor(DegradeInterceptor degradeInterceptor) { - this.degradeInterceptor = degradeInterceptor; - } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java index cdbaa6c..79d7185 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java @@ -1,22 +1,25 @@ package com.github.lianjiatech.retrofit.spring.boot.config; +import java.util.LinkedHashMap; +import java.util.Map; -import com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory; -import com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory; -import com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; + +import com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory; +import com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory; +import com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory; + +import lombok.Data; import retrofit2.CallAdapter; import retrofit2.Converter; import retrofit2.converter.jackson.JacksonConverterFactory; -import java.util.LinkedHashMap; -import java.util.Map; - /** * @author 陈添明 */ @ConfigurationProperties(prefix = "retrofit") +@Data public class RetrofitProperties { private static final String DEFAULT_POOL = "default"; @@ -42,7 +45,6 @@ public class RetrofitProperties { @NestedConfigurationProperty private DegradeProperty degrade = new DegradeProperty(); - /** * 日志配置 * log config @@ -50,7 +52,6 @@ public class RetrofitProperties { @NestedConfigurationProperty private LogProperty log = new LogProperty(); - /** * Disable Void return type */ @@ -76,21 +77,22 @@ public class RetrofitProperties { */ private int globalCallTimeoutMs = 0; - /** * 全局转换器工厂,转换器实例优先从Spring容器获取,如果没有获取到,则反射创建。 * global converter factories, The converter instance is first obtained from the Spring container. If it is not obtained, it is created by reflection. */ @SuppressWarnings("unchecked") - private Class[] globalConverterFactories = (Class[]) new Class[]{BasicTypeConverterFactory.class, JacksonConverterFactory.class}; + private Class[] globalConverterFactories = (Class< + ? extends Converter.Factory>[])new Class[] {BasicTypeConverterFactory.class, JacksonConverterFactory.class}; /** * 全局调用适配器工厂,转换器实例优先从Spring容器获取,如果没有获取到,则反射创建。 * global call adapter factories, The callAdapter instance is first obtained from the Spring container. If it is not obtained, it is created by reflection. */ @SuppressWarnings("unchecked") - private Class[] globalCallAdapterFactories = (Class[]) new Class[]{BodyCallAdapterFactory.class, ResponseCallAdapterFactory.class}; - + private Class[] globalCallAdapterFactories = + (Class[])new Class[] {BodyCallAdapterFactory.class, + ResponseCallAdapterFactory.class}; public Map getPool() { if (!pool.isEmpty()) { @@ -99,88 +101,4 @@ public Map getPool() { pool.put(DEFAULT_POOL, new PoolConfig(5, 300)); return pool; } - - public void setPool(Map pool) { - this.pool = pool; - } - - public boolean isDisableVoidReturnType() { - return disableVoidReturnType; - } - - public void setDisableVoidReturnType(boolean disableVoidReturnType) { - this.disableVoidReturnType = disableVoidReturnType; - } - - public Class[] getGlobalConverterFactories() { - return globalConverterFactories; - } - - public void setGlobalConverterFactories(Class[] globalConverterFactories) { - this.globalConverterFactories = globalConverterFactories; - } - - public Class[] getGlobalCallAdapterFactories() { - return globalCallAdapterFactories; - } - - public void setGlobalCallAdapterFactories(Class[] globalCallAdapterFactories) { - this.globalCallAdapterFactories = globalCallAdapterFactories; - } - - public RetryProperty getRetry() { - return retry; - } - - public void setRetry(RetryProperty retry) { - this.retry = retry; - } - - public DegradeProperty getDegrade() { - return degrade; - } - - public void setDegrade(DegradeProperty degrade) { - this.degrade = degrade; - } - - public LogProperty getLog() { - return log; - } - - public void setLog(LogProperty log) { - this.log = log; - } - - public int getGlobalConnectTimeoutMs() { - return globalConnectTimeoutMs; - } - - public void setGlobalConnectTimeoutMs(int globalConnectTimeoutMs) { - this.globalConnectTimeoutMs = globalConnectTimeoutMs; - } - - public int getGlobalReadTimeoutMs() { - return globalReadTimeoutMs; - } - - public void setGlobalReadTimeoutMs(int globalReadTimeoutMs) { - this.globalReadTimeoutMs = globalReadTimeoutMs; - } - - public int getGlobalWriteTimeoutMs() { - return globalWriteTimeoutMs; - } - - public void setGlobalWriteTimeoutMs(int globalWriteTimeoutMs) { - this.globalWriteTimeoutMs = globalWriteTimeoutMs; - } - - public int getGlobalCallTimeoutMs() { - return globalCallTimeoutMs; - } - - public void setGlobalCallTimeoutMs(int globalCallTimeoutMs) { - this.globalCallTimeoutMs = globalCallTimeoutMs; - } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetryProperty.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetryProperty.java index fe16889..4f7d337 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetryProperty.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetryProperty.java @@ -4,9 +4,12 @@ import com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor; import com.github.lianjiatech.retrofit.spring.boot.retry.RetryRule; +import lombok.Data; + /** * @author 陈添明 */ +@Data public class RetryProperty { /** @@ -35,50 +38,8 @@ public class RetryProperty { private RetryRule[] globalRetryRules = {RetryRule.RESPONSE_STATUS_NOT_2XX, RetryRule.OCCUR_IO_EXCEPTION}; - /** * retry interceptor */ private Class retryInterceptor = DefaultRetryInterceptor.class; - - - public boolean isEnableGlobalRetry() { - return enableGlobalRetry; - } - - public void setEnableGlobalRetry(boolean enableGlobalRetry) { - this.enableGlobalRetry = enableGlobalRetry; - } - - public int getGlobalMaxRetries() { - return globalMaxRetries; - } - - public void setGlobalMaxRetries(int globalMaxRetries) { - this.globalMaxRetries = globalMaxRetries; - } - - public int getGlobalIntervalMs() { - return globalIntervalMs; - } - - public void setGlobalIntervalMs(int globalIntervalMs) { - this.globalIntervalMs = globalIntervalMs; - } - - public RetryRule[] getGlobalRetryRules() { - return globalRetryRules; - } - - public void setGlobalRetryRules(RetryRule[] globalRetryRules) { - this.globalRetryRules = globalRetryRules; - } - - public Class getRetryInterceptor() { - return retryInterceptor; - } - - public void setRetryInterceptor(Class retryInterceptor) { - this.retryInterceptor = retryInterceptor; - } } 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..e432288 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 @@ -1,8 +1,8 @@ package com.github.lianjiatech.retrofit.spring.boot.core; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.List; + import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanFactory; @@ -14,7 +14,7 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; -import java.util.List; +import lombok.extern.slf4j.Slf4j; /** * This will just scan the same base package as Spring Boot does. If you want more power, you can explicitly use @@ -23,10 +23,9 @@ * * @author 陈添明 */ +@Slf4j public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware { - private static final Logger logger = LoggerFactory.getLogger(AutoConfiguredRetrofitScannerRegistrar.class); - private BeanFactory beanFactory; private ResourceLoader resourceLoader; @@ -38,19 +37,18 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } - @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { if (!AutoConfigurationPackages.has(this.beanFactory)) { - logger.debug("Could not determine auto-configuration package, automatic retrofit scanning disabled."); + log.debug("Could not determine auto-configuration package, automatic retrofit scanning disabled."); return; } - logger.debug("Searching for retrofits annotated with @RetrofitClient"); + log.debug("Searching for retrofits annotated with @RetrofitClient"); List packages = AutoConfigurationPackages.get(this.beanFactory); - if (logger.isDebugEnabled()) { - packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg)); + if (log.isDebugEnabled()) { + packages.forEach(pkg -> log.debug("Using auto-configuration base package '{}'", pkg)); } // Scan the @RetrofitClient annotated interface under the specified path and register it to the BeanDefinitionRegistry @@ -64,13 +62,11 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B scanner.doScan(packageArr); } - @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } - @Override public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; 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..95889ba 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 @@ -1,8 +1,9 @@ package com.github.lianjiatech.retrofit.spring.boot.core; -import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.Arrays; +import java.util.Objects; +import java.util.Set; + import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -11,19 +12,18 @@ import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.util.ClassUtils; -import java.util.Arrays; -import java.util.Objects; -import java.util.Set; +import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; + +import lombok.extern.slf4j.Slf4j; /** * @author 陈添明 */ +@Slf4j public class ClassPathRetrofitClientScanner extends ClassPathBeanDefinitionScanner { private final ClassLoader classLoader; - private final static Logger logger = LoggerFactory.getLogger(ClassPathRetrofitClientScanner.class); - public ClassPathRetrofitClientScanner(BeanDefinitionRegistry registry, ClassLoader classLoader) { super(registry, false); this.classLoader = classLoader; @@ -34,12 +34,12 @@ public void registerFilters() { this.addIncludeFilter(annotationTypeFilter); } - @Override protected Set doScan(String... basePackages) { Set beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { - logger.warn("No RetrofitClient was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration."); + log.warn("No RetrofitClient was found in '" + Arrays.toString(basePackages) + + "' package. Please check your configuration."); } else { processBeanDefinitions(beanDefinitions); } @@ -56,22 +56,22 @@ protected boolean isCandidateComponent( classLoader); return !target.isAnnotation(); } catch (Exception ex) { - logger.error("load class exception:", ex); + log.error("load class exception:", ex); } } return false; } - private void processBeanDefinitions(Set beanDefinitions) { GenericBeanDefinition definition; for (BeanDefinitionHolder holder : beanDefinitions) { - definition = (GenericBeanDefinition) holder.getBeanDefinition(); - if (logger.isDebugEnabled()) { - logger.debug("Creating RetrofitClientBean with name '" + holder.getBeanName() + definition = (GenericBeanDefinition)holder.getBeanDefinition(); + if (log.isDebugEnabled()) { + log.debug("Creating RetrofitClientBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' Interface"); } - definition.getConstructorArgumentValues().addGenericArgumentValue(Objects.requireNonNull(definition.getBeanClassName())); + definition.getConstructorArgumentValues() + .addGenericArgumentValue(Objects.requireNonNull(definition.getBeanClassName())); // beanClass全部设置为RetrofitFactoryBean definition.setBeanClass(RetrofitFactoryBean.class); } 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 bfaf7ec..53f3219 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 @@ -11,11 +11,8 @@ import java.util.HashMap; import java.util.List; 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.BeansException; import org.springframework.beans.factory.FactoryBean; import org.springframework.context.ApplicationContext; @@ -68,10 +65,8 @@ */ public class RetrofitFactoryBean implements FactoryBean, EnvironmentAware, ApplicationContextAware { - private final static Logger logger = LoggerFactory.getLogger(RetrofitFactoryBean.class); - private static final Map, CallAdapter.Factory> CALL_ADAPTER_FACTORIES_CACHE = - new ConcurrentHashMap<>(4); + new HashMap<>(4); private Class retrofitInterface; @@ -84,7 +79,7 @@ public class RetrofitFactoryBean implements FactoryBean, EnvironmentAware, private ApplicationContext applicationContext; private static final Map, Converter.Factory> CONVERTER_FACTORIES_CACHE = - new ConcurrentHashMap<>(4); + new HashMap<>(4); public RetrofitFactoryBean(Class retrofitInterface) { this.retrofitInterface = retrofitInterface; @@ -281,10 +276,7 @@ private void addDefineOnInterfaceInterceptor(OkHttpClient.Builder okHttpClientBu private void addErrorDecoderInterceptor(OkHttpClient.Builder okHttpClientBuilder, RetrofitClient retrofitClient) throws InstantiationException, IllegalAccessException { Class errorDecoderClass = retrofitClient.errorDecoder(); - ErrorDecoder decoder = ApplicationContextUtils.getBeanOrNull(applicationContext, errorDecoderClass); - if (decoder == null) { - decoder = errorDecoderClass.newInstance(); - } + ErrorDecoder decoder = ApplicationContextUtils.getBeanOrNew(applicationContext, errorDecoderClass); ErrorDecoderInterceptor decoderInterceptor = ErrorDecoderInterceptor.create(decoder); okHttpClientBuilder.addInterceptor(decoderInterceptor); } @@ -454,10 +446,7 @@ private List getCallAdapterFactories( for (Class callAdapterFactoryClass : combineCallAdapterFactoryClasses) { CallAdapter.Factory callAdapterFactory = CALL_ADAPTER_FACTORIES_CACHE.get(callAdapterFactoryClass); if (callAdapterFactory == null) { - callAdapterFactory = ApplicationContextUtils.getBeanOrNull(applicationContext, callAdapterFactoryClass); - if (callAdapterFactory == null) { - callAdapterFactory = callAdapterFactoryClass.newInstance(); - } + callAdapterFactory = ApplicationContextUtils.getBeanOrNew(applicationContext, callAdapterFactoryClass); CALL_ADAPTER_FACTORIES_CACHE.put(callAdapterFactoryClass, callAdapterFactory); } callAdapterFactories.add(callAdapterFactory); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/HttpMethodPath.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/HttpMethodPath.java index 4ffa3f4..4a45a91 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/HttpMethodPath.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/HttpMethodPath.java @@ -1,8 +1,11 @@ package com.github.lianjiatech.retrofit.spring.boot.degrade; +import lombok.Data; + /** * @author 陈添明 */ +@Data public class HttpMethodPath { /** * request method. such as GET, POST, PUT etc. @@ -18,12 +21,4 @@ public HttpMethodPath(String method, String path) { this.method = method; this.path = path; } - - public String getMethod() { - return method; - } - - public String getPath() { - return path; - } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelDegradeInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelDegradeInterceptor.java index 791396b..ed8d4a5 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelDegradeInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelDegradeInterceptor.java @@ -23,7 +23,7 @@ */ public class SentinelDegradeInterceptor implements DegradeInterceptor { - private ResourceNameParser resourceNameParser; + private final ResourceNameParser resourceNameParser; public SentinelDegradeInterceptor(ResourceNameParser resourceNameParser) { this.resourceNameParser = resourceNameParser; diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BasePathMatchInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BasePathMatchInterceptor.java index 5a8b518..715b297 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BasePathMatchInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/BasePathMatchInterceptor.java @@ -1,17 +1,20 @@ package com.github.lianjiatech.retrofit.spring.boot.interceptor; -import okhttp3.Request; -import okhttp3.Response; +import java.io.IOException; + import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; -import java.io.IOException; +import lombok.Data; +import okhttp3.Request; +import okhttp3.Response; /** * 路径匹配拦截器, 如果使用spring-bean方式,使用原型模式 * * @author 陈添明 */ +@Data public abstract class BasePathMatchInterceptor implements PrototypeInterceptor { private String[] include; @@ -20,15 +23,6 @@ public abstract class BasePathMatchInterceptor implements PrototypeInterceptor { private PathMatcher pathMatcher = new AntPathMatcher(); - - public void setInclude(String[] include) { - this.include = include; - } - - public void setExclude(String[] exclude) { - this.exclude = exclude; - } - @Override public final Response intercept(Chain chain) throws IOException { Request request = chain.request(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/DefaultLoggingInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/DefaultLoggingInterceptor.java index b97e9d4..4cbb829 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/DefaultLoggingInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/DefaultLoggingInterceptor.java @@ -1,19 +1,17 @@ package com.github.lianjiatech.retrofit.spring.boot.interceptor; +import java.io.IOException; + +import lombok.extern.slf4j.Slf4j; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; /** * @author 陈添明 */ +@Slf4j public class DefaultLoggingInterceptor extends BaseLoggingInterceptor { - private final static Logger logger = LoggerFactory.getLogger(DefaultLoggingInterceptor.class); - private final HttpLoggingInterceptor httpLoggingInterceptor; public DefaultLoggingInterceptor(LogLevel logLevel, LogStrategy logStrategy) { @@ -33,13 +31,13 @@ public Response intercept(Chain chain) throws IOException { public HttpLoggingInterceptor.Logger httpLoggingInterceptorLogger(LogLevel level) { if (level == LogLevel.DEBUG) { - return logger::debug; + return log::debug; } else if (level == LogLevel.ERROR) { - return logger::error; + return log::error; } else if (level == LogLevel.INFO) { - return logger::info; + return log::info; } else if (level == LogLevel.WARN) { - return logger::warn; + return log::warn; } throw new UnsupportedOperationException("We don't support this log level currently."); } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/ErrorDecoderInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/ErrorDecoderInterceptor.java index b6dab04..2587922 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/ErrorDecoderInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/ErrorDecoderInterceptor.java @@ -1,14 +1,15 @@ package com.github.lianjiatech.retrofit.spring.boot.interceptor; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder; + import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - /** * @author 陈添明 */ @@ -16,7 +17,7 @@ public class ErrorDecoderInterceptor implements Interceptor { private final ErrorDecoder errorDecoder; - private static Map cache = new HashMap<>(4); + private static final Map CACHE = new HashMap<>(4); private ErrorDecoderInterceptor(ErrorDecoder errorDecoder) { this.errorDecoder = errorDecoder; @@ -44,20 +45,19 @@ public Response intercept(Chain chain) throws IOException { throw errorDecoder.ioExceptionDecode(request, e); } catch (Exception e) { if (decoded && e instanceof RuntimeException) { - throw (RuntimeException) e; + throw (RuntimeException)e; } throw errorDecoder.exceptionDecode(request, e); } } - public static ErrorDecoderInterceptor create(ErrorDecoder errorDecoder) { - ErrorDecoderInterceptor interceptor = cache.get(errorDecoder); + ErrorDecoderInterceptor interceptor = CACHE.get(errorDecoder); if (interceptor != null) { return interceptor; } interceptor = new ErrorDecoderInterceptor(errorDecoder); - cache.put(errorDecoder, interceptor); + CACHE.put(errorDecoder, interceptor); return interceptor; } } 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 deleted file mode 100644 index 75c8306..0000000 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/interceptor/GlobalAndNetworkInterceptorFinder.java +++ /dev/null @@ -1,33 +0,0 @@ -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/retry/BaseRetryInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java index 7677de6..a0db314 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java @@ -5,6 +5,7 @@ import com.github.lianjiatech.retrofit.spring.boot.util.AnnotationExtendUtils; +import lombok.Data; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; @@ -16,6 +17,7 @@ * * @author 陈添明 */ +@Data public abstract class BaseRetryInterceptor implements Interceptor { private boolean enableGlobalRetry; @@ -74,9 +76,7 @@ private boolean needRetry(Retry retry) { } /** - * process a retryable request - * The access level here is set to protected, which can facilitate business personalized expansion - * 这里访问级别设置为protected,可方便业务个性化扩展 + * 重试拦截 * * @param maxRetries 最大重试次数。Maximum number of retries * @param intervalMs 重试时间间隔。Retry interval @@ -85,21 +85,4 @@ private boolean needRetry(Retry retry) { * @return 请求响应。Response */ protected abstract Response retryIntercept(int maxRetries, int intervalMs, RetryRule[] retryRules, Chain chain); - - - public void setEnableGlobalRetry(boolean enableGlobalRetry) { - this.enableGlobalRetry = enableGlobalRetry; - } - - public void setGlobalMaxRetries(int globalMaxRetries) { - this.globalMaxRetries = globalMaxRetries; - } - - public void setGlobalIntervalMs(int globalIntervalMs) { - this.globalIntervalMs = globalIntervalMs; - } - - public void setGlobalRetryRules(RetryRule[] globalRetryRules) { - this.globalRetryRules = globalRetryRules; - } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/DefaultRetryInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/DefaultRetryInterceptor.java index f99df58..9ac72a1 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/DefaultRetryInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/DefaultRetryInterceptor.java @@ -1,25 +1,23 @@ package com.github.lianjiatech.retrofit.spring.boot.retry; -import okhttp3.Request; -import okhttp3.Response; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; +import okhttp3.Request; +import okhttp3.Response; + /** * @author 陈添明 */ +@Slf4j public class DefaultRetryInterceptor extends BaseRetryInterceptor { - private final static Logger logger = LoggerFactory.getLogger(DefaultRetryInterceptor.class); - @Override protected Response retryIntercept(int maxRetries, int intervalMs, RetryRule[] retryRules, Chain chain) { - HashSet retryRuleSet = (HashSet) Arrays.stream(retryRules).collect(Collectors.toSet()); + HashSet retryRuleSet = (HashSet)Arrays.stream(retryRules).collect(Collectors.toSet()); RetryStrategy retryStrategy = new RetryStrategy(maxRetries, intervalMs); while (true) { try { @@ -35,7 +33,7 @@ protected Response retryIntercept(int maxRetries, int intervalMs, RetryRule[] re } // 执行重试 retryStrategy.retry(); - logger.debug("The response fails, retry is performed! The response code is " + response.code()); + log.debug("The response fails, retry is performed! The response code is " + response.code()); response.close(); } } catch (Exception e) { diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/RetryStrategy.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/RetryStrategy.java index c8fe882..ae7c5e0 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/RetryStrategy.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/RetryStrategy.java @@ -5,7 +5,7 @@ */ class RetryStrategy { private int maxRetries; - private int intervalMs; + private final int intervalMs; public RetryStrategy(int maxRetries, int intervalMs) { this.maxRetries = maxRetries; diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AnnotationExtendUtils.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AnnotationExtendUtils.java index 2162d9f..cbfc9cd 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AnnotationExtendUtils.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AnnotationExtendUtils.java @@ -5,10 +5,13 @@ import org.springframework.lang.Nullable; +import lombok.experimental.UtilityClass; + /** * @author 陈添明 * @since 2022/4/30 3:02 下午 */ +@UtilityClass public class AnnotationExtendUtils { /** 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 7af1396..082adae 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 @@ -5,15 +5,14 @@ import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; +import lombok.experimental.UtilityClass; + /** * @author 陈添明 */ +@UtilityClass public final class ApplicationContextUtils { - private ApplicationContextUtils() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - public static T getBeanOrNull(ApplicationContext context, Class clz) { try { return context.getBean(clz); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/BeanExtendUtils.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/BeanExtendUtils.java index 8f8e546..9ddb0ea 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/BeanExtendUtils.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/BeanExtendUtils.java @@ -1,20 +1,19 @@ package com.github.lianjiatech.retrofit.spring.boot.util; -import org.springframework.beans.BeanUtils; - import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.Map; +import org.springframework.beans.BeanUtils; + +import lombok.experimental.UtilityClass; + /** * @author 陈添明 */ +@UtilityClass public final class BeanExtendUtils { - private BeanExtendUtils() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - /** * 使用map填充bean实例的属性值 * Use map to fill in the attribute values of the bean instance diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/RetrofitUtils.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/RetrofitUtils.java index 59f508a..26aa06e 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/RetrofitUtils.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/RetrofitUtils.java @@ -10,6 +10,7 @@ import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; import com.github.lianjiatech.retrofit.spring.boot.exception.ReadResponseBodyException; +import lombok.experimental.UtilityClass; import okhttp3.Headers; import okhttp3.MediaType; import okhttp3.Response; @@ -21,6 +22,7 @@ /** * @author 陈添明 */ +@UtilityClass public final class RetrofitUtils { private static final Charset UTF8 = StandardCharsets.UTF_8; @@ -31,10 +33,6 @@ public final class RetrofitUtils { private static final String SUFFIX = "/"; public static final String HTTP_PREFIX = "http://"; - private RetrofitUtils() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - /** * read ResponseBody as String * diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/ErrorDecoderTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/ErrorDecoderTest.java index 24e8dce..d67648b 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/ErrorDecoderTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/ErrorDecoderTest.java @@ -1,5 +1,15 @@ package com.github.lianjiatech.retrofit.spring.boot.test; +import java.io.IOException; + +import org.junit.After; +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 com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -7,21 +17,9 @@ 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.ErrorDecoderTestApi; + import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; -import org.junit.After; -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 java.io.IOException; -import java.util.HashMap; -import java.util.Map; /** * @author 陈添明 @@ -30,9 +28,6 @@ @RunWith(SpringRunner.class) public class ErrorDecoderTest { - - private static final Logger logger = LoggerFactory.getLogger(RetrofitExceptionTest.class); - @Autowired private ErrorDecoderTestApi errorDecoderTestApi; diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitConvertFactoriesTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitConvertFactoriesTest.java index 43fb874..ea7bcfb 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitConvertFactoriesTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitConvertFactoriesTest.java @@ -1,24 +1,24 @@ package com.github.lianjiatech.retrofit.spring.boot.test; -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.ConvertFactoriesTestApi; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; +import java.io.IOException; + 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 java.io.IOException; +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.ConvertFactoriesTestApi; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; /** * @author 陈添明 @@ -27,9 +27,6 @@ @RunWith(SpringRunner.class) public class RetrofitConvertFactoriesTest { - - private static final Logger logger = LoggerFactory.getLogger(RetrofitExceptionTest.class); - @Autowired private ConvertFactoriesTestApi convertFactoriesTestApi; 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 09b5701..910a52a 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 @@ -1,26 +1,26 @@ 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.HttpApi; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import org.junit.After; 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 java.io.IOException; -import java.util.HashMap; -import java.util.Map; +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.HttpApi; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; /** * @author 陈添明 @@ -29,9 +29,6 @@ @RunWith(SpringRunner.class) public class RetrofitExceptionTest { - - private static final Logger logger = LoggerFactory.getLogger(RetrofitExceptionTest.class); - @Autowired private HttpApi httpApi; 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 8af32c7..1f6de73 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 @@ -14,8 +14,6 @@ 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; @@ -25,10 +23,13 @@ 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.DownloadApi; 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 com.github.lianjiatech.retrofit.spring.boot.test.http.InterceptApi; +import lombok.extern.slf4j.Slf4j; import okhttp3.Request; import okhttp3.ResponseBody; import okhttp3.mockwebserver.MockResponse; @@ -42,11 +43,10 @@ */ @SpringBootTest(classes = RetrofitTestApplication.class) @RunWith(SpringRunner.class) +@Slf4j public class RetrofitStarterTest { - private static final Logger logger = LoggerFactory.getLogger(RetrofitStarterTest.class); - @Autowired private HttpApi httpApi; @@ -56,6 +56,12 @@ public class RetrofitStarterTest { @Autowired private HttpApi3 httpApi3; + @Autowired + private InterceptApi interceptApi; + + @Autowired + private DownloadApi downloadApi; + private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setSerializationInclusion(JsonInclude.Include.NON_NULL); @@ -181,7 +187,7 @@ public void onResponse(Call> call, Response> respo @Override public void onFailure(Call> call, Throwable t) { Request request = call.request(); - logger.error("请求执行失败! request = {}", request, t); + log.error("请求执行失败! request = {}", request, t); countDownLatch.countDown(); } }); diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 70495cb..6c6cb68 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -55,8 +55,6 @@ retrofit: enable: true # 熔断降级实现方式 degrade-type: sentinel - # 熔断资源名称解析器 - resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser # 全局连接超时时间 global-connect-timeout-ms: 5000 # 全局读取超时时间 diff --git a/src/test/resources/default-config.yml b/src/test/resources/default-config.yml index 6788d84..2f436de 100644 --- a/src/test/resources/default-config.yml +++ b/src/test/resources/default-config.yml @@ -54,8 +54,6 @@ retrofit: enable: false # 熔断降级实现方式 degrade-type: sentinel - # 熔断资源名称解析器 - resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser # 全局连接超时时间 global-connect-timeout-ms: 10000 # 全局读取超时时间