Browse Source

Fix FeignClientBuilder fallback (#197)

* fix FeignClientBuilder fallback

* Fix FeignClientBuilder fallback

* Fix FeignClientBuilder fallback

* Update FeignClientBuilderTests.java

* Update FeignClientBuilderTests.java

* Update FeignClientBuilderTests.java
pull/227/head
李家奉 5 years ago committed by Olga Maciaszek-Sharma
parent
commit
bc4bfb5b7a
  1. 9
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientBuilder.java
  2. 28
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientBuilderTests.java

9
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientBuilder.java

@ -16,6 +16,8 @@
package org.springframework.cloud.openfeign; package org.springframework.cloud.openfeign;
import feign.hystrix.FallbackFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
/** /**
@ -57,8 +59,7 @@ public class FeignClientBuilder {
this.feignClientFactoryBean.setContextId(FeignClientsRegistrar.getName(name)); this.feignClientFactoryBean.setContextId(FeignClientsRegistrar.getName(name));
// preset default values - these values resemble the default values on the // preset default values - these values resemble the default values on the
// FeignClient annotation // FeignClient annotation
this.url("").path("").decode404(false).fallback(void.class) this.url("").path("").decode404(false);
.fallbackFactory(void.class);
} }
public Builder url(final String url) { public Builder url(final String url) {
@ -81,13 +82,13 @@ public class FeignClientBuilder {
return this; return this;
} }
public Builder fallback(final Class<T> fallback) { public Builder fallback(final Class<? extends T> fallback) {
FeignClientsRegistrar.validateFallback(fallback); FeignClientsRegistrar.validateFallback(fallback);
this.feignClientFactoryBean.setFallback(fallback); this.feignClientFactoryBean.setFallback(fallback);
return this; return this;
} }
public Builder fallbackFactory(final Class<T> fallbackFactory) { public Builder fallbackFactory(final Class<? extends FallbackFactory<? extends T>> fallbackFactory) {
FeignClientsRegistrar.validateFallbackFactory(fallbackFactory); FeignClientsRegistrar.validateFallbackFactory(fallbackFactory);
this.feignClientFactoryBean.setFallbackFactory(fallbackFactory); this.feignClientFactoryBean.setFallbackFactory(fallbackFactory);
return this; return this;

28
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.Collections;
import java.util.List; import java.util.List;
import feign.hystrix.FallbackFactory;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@ -101,11 +102,11 @@ public class FeignClientBuilderTests {
public void forType_preinitializedBuilder() { public void forType_preinitializedBuilder() {
// when: // when:
final FeignClientBuilder.Builder builder = this.feignClientBuilder final FeignClientBuilder.Builder builder = this.feignClientBuilder
.forType(FeignClientBuilderTests.class, "TestClient"); .forType(TestFeignClient.class, "TestClient");
// then: // then:
assertFactoryBeanField(builder, "applicationContext", this.applicationContext); assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class); assertFactoryBeanField(builder, "type", TestFeignClient.class);
assertFactoryBeanField(builder, "name", "TestClient"); assertFactoryBeanField(builder, "name", "TestClient");
assertFactoryBeanField(builder, "contextId", "TestClient"); assertFactoryBeanField(builder, "contextId", "TestClient");
@ -126,21 +127,22 @@ public class FeignClientBuilderTests {
public void forType_allFieldsSetOnBuilder() { public void forType_allFieldsSetOnBuilder() {
// when: // when:
final FeignClientBuilder.Builder builder = this.feignClientBuilder final FeignClientBuilder.Builder builder = this.feignClientBuilder
.forType(FeignClientBuilderTests.class, "TestClient").decode404(true) .forType(TestFeignClient.class, "TestClient").decode404(true)
.fallback(Object.class).fallbackFactory(Object.class).path("Path/") .fallback(TestFeignClientFallback.class)
.fallbackFactory(TestFeignClientFallbackFactory.class).path("Path/")
.url("Url/"); .url("Url/");
// then: // then:
assertFactoryBeanField(builder, "applicationContext", this.applicationContext); assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class); assertFactoryBeanField(builder, "type", TestFeignClient.class);
assertFactoryBeanField(builder, "name", "TestClient"); assertFactoryBeanField(builder, "name", "TestClient");
// and: // and:
assertFactoryBeanField(builder, "url", "http://Url/"); assertFactoryBeanField(builder, "url", "http://Url/");
assertFactoryBeanField(builder, "path", "/Path"); assertFactoryBeanField(builder, "path", "/Path");
assertFactoryBeanField(builder, "decode404", true); assertFactoryBeanField(builder, "decode404", true);
assertFactoryBeanField(builder, "fallback", Object.class); assertFactoryBeanField(builder, "fallback", TestFeignClientFallback.class);
assertFactoryBeanField(builder, "fallbackFactory", Object.class); assertFactoryBeanField(builder, "fallbackFactory", TestFeignClientFallbackFactory.class);
} }
@Test @Test
@ -159,4 +161,16 @@ public class FeignClientBuilderTests {
builder.build(); builder.build();
} }
private interface TestFeignClient {
}
private class TestFeignClientFallback implements TestFeignClient {
}
private class TestFeignClientFallbackFactory implements FallbackFactory<TestFeignClient> {
@Override
public TestFeignClientFallback create(Throwable throwable) {
return new TestFeignClientFallback();
}
}
} }

Loading…
Cancel
Save