From bc4bfb5b7a469a8faac7409ca50d2cbe3489d104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AE=B6=E5=A5=89?= <519982896@qq.com> Date: Thu, 26 Sep 2019 06:11:27 -0500 Subject: [PATCH] Fix FeignClientBuilder fallback (#197) * fix FeignClientBuilder fallback * Fix FeignClientBuilder fallback * Fix FeignClientBuilder fallback * Update FeignClientBuilderTests.java * Update FeignClientBuilderTests.java * Update FeignClientBuilderTests.java --- .../cloud/openfeign/FeignClientBuilder.java | 9 +++--- .../openfeign/FeignClientBuilderTests.java | 28 ++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientBuilder.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientBuilder.java index 0d18c556..18cd30a3 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientBuilder.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientBuilder.java @@ -16,6 +16,8 @@ package org.springframework.cloud.openfeign; +import feign.hystrix.FallbackFactory; + import org.springframework.context.ApplicationContext; /** @@ -57,8 +59,7 @@ public class FeignClientBuilder { this.feignClientFactoryBean.setContextId(FeignClientsRegistrar.getName(name)); // preset default values - these values resemble the default values on the // FeignClient annotation - this.url("").path("").decode404(false).fallback(void.class) - .fallbackFactory(void.class); + this.url("").path("").decode404(false); } public Builder url(final String url) { @@ -81,13 +82,13 @@ public class FeignClientBuilder { return this; } - public Builder fallback(final Class fallback) { + public Builder fallback(final Class fallback) { FeignClientsRegistrar.validateFallback(fallback); this.feignClientFactoryBean.setFallback(fallback); return this; } - public Builder fallbackFactory(final Class fallbackFactory) { + public Builder fallbackFactory(final Class> fallbackFactory) { FeignClientsRegistrar.validateFallbackFactory(fallbackFactory); this.feignClientFactoryBean.setFallbackFactory(fallbackFactory); return this; diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientBuilderTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientBuilderTests.java index 699c3a3a..1acb6379 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientBuilderTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientBuilderTests.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import feign.hystrix.FallbackFactory; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Rule; @@ -101,11 +102,11 @@ public class FeignClientBuilderTests { public void forType_preinitializedBuilder() { // when: final FeignClientBuilder.Builder builder = this.feignClientBuilder - .forType(FeignClientBuilderTests.class, "TestClient"); + .forType(TestFeignClient.class, "TestClient"); // then: assertFactoryBeanField(builder, "applicationContext", this.applicationContext); - assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class); + assertFactoryBeanField(builder, "type", TestFeignClient.class); assertFactoryBeanField(builder, "name", "TestClient"); assertFactoryBeanField(builder, "contextId", "TestClient"); @@ -126,21 +127,22 @@ public class FeignClientBuilderTests { public void forType_allFieldsSetOnBuilder() { // when: final FeignClientBuilder.Builder builder = this.feignClientBuilder - .forType(FeignClientBuilderTests.class, "TestClient").decode404(true) - .fallback(Object.class).fallbackFactory(Object.class).path("Path/") + .forType(TestFeignClient.class, "TestClient").decode404(true) + .fallback(TestFeignClientFallback.class) + .fallbackFactory(TestFeignClientFallbackFactory.class).path("Path/") .url("Url/"); // then: assertFactoryBeanField(builder, "applicationContext", this.applicationContext); - assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class); + assertFactoryBeanField(builder, "type", TestFeignClient.class); assertFactoryBeanField(builder, "name", "TestClient"); // and: assertFactoryBeanField(builder, "url", "http://Url/"); assertFactoryBeanField(builder, "path", "/Path"); assertFactoryBeanField(builder, "decode404", true); - assertFactoryBeanField(builder, "fallback", Object.class); - assertFactoryBeanField(builder, "fallbackFactory", Object.class); + assertFactoryBeanField(builder, "fallback", TestFeignClientFallback.class); + assertFactoryBeanField(builder, "fallbackFactory", TestFeignClientFallbackFactory.class); } @Test @@ -159,4 +161,16 @@ public class FeignClientBuilderTests { builder.build(); } + private interface TestFeignClient { + } + + private class TestFeignClientFallback implements TestFeignClient { + } + + private class TestFeignClientFallbackFactory implements FallbackFactory { + @Override + public TestFeignClientFallback create(Throwable throwable) { + return new TestFeignClientFallback(); + } + } }