diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/BodyCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/BodyCallAdapterFactory.java index 8c361dd..81d1e4a 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/BodyCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/BodyCallAdapterFactory.java @@ -38,7 +38,7 @@ * @author 陈添明 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class BodyCallAdapterFactory extends CallAdapter.Factory { +public final class BodyCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final BodyCallAdapterFactory INSTANCE = new BodyCallAdapterFactory(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/InternalCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/InternalCallAdapterFactory.java new file mode 100644 index 0000000..c4249ea --- /dev/null +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/InternalCallAdapterFactory.java @@ -0,0 +1,9 @@ +package com.github.lianjiatech.retrofit.spring.boot.core; + +/** + * 组件内置CallAdapterFactory,标记接口。 + * @author 陈添明 + * @since 2022/9/12 8:08 下午 + */ +public interface InternalCallAdapterFactory { +} diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ResponseCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ResponseCallAdapterFactory.java index ec9702e..78b27d6 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ResponseCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ResponseCallAdapterFactory.java @@ -37,7 +37,7 @@ * @author 陈添明 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class ResponseCallAdapterFactory extends CallAdapter.Factory { +public final class ResponseCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final ResponseCallAdapterFactory INSTANCE = new ResponseCallAdapterFactory(); 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 b08ba14..482269d 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 @@ -38,6 +38,8 @@ import okhttp3.Interceptor; import okhttp3.OkHttpClient; +import retrofit2.CallAdapter; +import retrofit2.Converter; import retrofit2.Retrofit; /** @@ -165,14 +167,26 @@ private Retrofit createRetrofit() { .validateEagerly(retrofitClient.validateEagerly()) .client(client); - combineAndCreate(retrofitClient.callAdapterFactories(), retrofitConfigBean.getGlobalCallAdapterFactoryClasses()) - .forEach(retrofitBuilder::addCallAdapterFactory); + // 添加配置或者指定的CallAdapterFactory + List> callAdapterFactories = new ArrayList<>(2); + callAdapterFactories.addAll(Arrays.asList(retrofitClient.callAdapterFactories())); + callAdapterFactories.addAll(Arrays.asList(retrofitConfigBean.getGlobalCallAdapterFactoryClasses())); + callAdapterFactories.stream() + // 过滤掉内置的CallAdapterFactory,因为后续会指定add + .filter(adapterFactoryClass -> !InternalCallAdapterFactory.class.isAssignableFrom(adapterFactoryClass)) + .forEach(adapterFactoryClass -> retrofitBuilder + .addCallAdapterFactory(AppContextUtils.getBeanOrNew(applicationContext, adapterFactoryClass))); + addReactiveCallAdapterFactory(retrofitBuilder); retrofitBuilder.addCallAdapterFactory(ResponseCallAdapterFactory.INSTANCE); retrofitBuilder.addCallAdapterFactory(BodyCallAdapterFactory.INSTANCE); - combineAndCreate(retrofitClient.converterFactories(), retrofitConfigBean.getGlobalConverterFactoryClasses()) - .forEach(retrofitBuilder::addConverterFactory); + // 添加配置或者指定的ConverterFactory + List> converterFactories = new ArrayList<>(4); + converterFactories.addAll(Arrays.asList(retrofitClient.converterFactories())); + converterFactories.addAll(Arrays.asList(retrofitConfigBean.getGlobalConverterFactoryClasses())); + converterFactories.forEach(converterFactoryClass -> retrofitBuilder + .addConverterFactory(AppContextUtils.getBeanOrNew(applicationContext, converterFactoryClass))); return retrofitBuilder.build(); } @@ -218,21 +232,6 @@ private boolean reactor3ClassExist() { } } - private List combineAndCreate(Class[] clz, Class[] globalClz) { - if (clz.length == 0 && globalClz.length == 0) { - return Collections.emptyList(); - } - List> combineClz = new ArrayList<>(clz.length + globalClz.length); - combineClz.addAll(Arrays.asList(clz)); - combineClz.addAll(Arrays.asList(globalClz)); - - List result = new ArrayList<>(combineClz.size()); - for (Class aClass : combineClz) { - result.add(AppContextUtils.getBeanOrNew(applicationContext, aClass)); - } - return result; - } - @Override public void setEnvironment(Environment environment) { this.environment = environment; diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/MonoCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/MonoCallAdapterFactory.java index 1d691ff..cc405fe 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/MonoCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/MonoCallAdapterFactory.java @@ -4,6 +4,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory; import lombok.AccessLevel; import lombok.NoArgsConstructor; import reactor.core.publisher.Mono; @@ -20,7 +21,7 @@ * @since 2022/6/9 8:53 下午 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class MonoCallAdapterFactory extends CallAdapter.Factory { +public class MonoCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final MonoCallAdapterFactory INSTANCE = new MonoCallAdapterFactory(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2CompletableCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2CompletableCallAdapterFactory.java index ffeefc3..11bf7e7 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2CompletableCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2CompletableCallAdapterFactory.java @@ -3,6 +3,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory; import io.reactivex.Completable; import io.reactivex.CompletableEmitter; import io.reactivex.annotations.NonNull; @@ -20,7 +21,7 @@ * @since 2022/6/10 8:08 上午 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class Rxjava2CompletableCallAdapterFactory extends CallAdapter.Factory { +public class Rxjava2CompletableCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final Rxjava2CompletableCallAdapterFactory INSTANCE = new Rxjava2CompletableCallAdapterFactory(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2SingleCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2SingleCallAdapterFactory.java index b159932..7c7c49e 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2SingleCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava2SingleCallAdapterFactory.java @@ -4,6 +4,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory; import io.reactivex.Single; import io.reactivex.SingleEmitter; import io.reactivex.annotations.NonNull; @@ -21,7 +22,7 @@ * @since 2022/6/10 8:08 上午 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class Rxjava2SingleCallAdapterFactory extends CallAdapter.Factory { +public class Rxjava2SingleCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final Rxjava2SingleCallAdapterFactory INSTANCE = new Rxjava2SingleCallAdapterFactory(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3CompletableCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3CompletableCallAdapterFactory.java index d29b0a7..b161da9 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3CompletableCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3CompletableCallAdapterFactory.java @@ -3,6 +3,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory; import io.reactivex.rxjava3.annotations.NonNull; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.CompletableEmitter; @@ -20,7 +21,7 @@ * @since 2022/6/10 8:08 上午 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class Rxjava3CompletableCallAdapterFactory extends CallAdapter.Factory { +public class Rxjava3CompletableCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final Rxjava3CompletableCallAdapterFactory INSTANCE = new Rxjava3CompletableCallAdapterFactory(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3SingleCallAdapterFactory.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3SingleCallAdapterFactory.java index 8555816..7bfe17b 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3SingleCallAdapterFactory.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/reactive/Rxjava3SingleCallAdapterFactory.java @@ -4,6 +4,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory; import io.reactivex.rxjava3.annotations.NonNull; import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.core.SingleEmitter; @@ -21,7 +22,7 @@ * @since 2022/6/10 8:08 上午 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class Rxjava3SingleCallAdapterFactory extends CallAdapter.Factory { +public class Rxjava3SingleCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory { public static final Rxjava3SingleCallAdapterFactory INSTANCE = new Rxjava3SingleCallAdapterFactory(); diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AppContextUtils.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AppContextUtils.java index 098aae6..c950485 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AppContextUtils.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/util/AppContextUtils.java @@ -24,7 +24,7 @@ public static T getBeanOrNew(ApplicationContext context, Class clz) { } catch (Exception e1) { try { log.warn("Failed to get bean from applicationContext!", e1); - return clz.newInstance(); + return clz.getDeclaredConstructor().newInstance(); } catch (Exception e2) { log.warn("Failed to create instance by reflection.", e2); try { diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index ec4bc4e..bdd7512 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -6,7 +6,7 @@ retrofit: - retrofit2.converter.jackson.JacksonConverterFactory # 全局调用适配器工厂(组件扩展的调用适配器工厂已经内置,这里请勿重复配置) global-call-adapter-factories: - + - com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory # 全局日志打印配置 global-log: # 启用全局日志打印