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 @@ @@ -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 { @@ -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 { @@ -81,13 +82,13 @@ public class FeignClientBuilder {
return this;
}
public Builder fallback(final Class<T> fallback) {
public Builder fallback(final Class<? extends T> fallback) {
FeignClientsRegistrar.validateFallback(fallback);
this.feignClientFactoryBean.setFallback(fallback);
return this;
}
public Builder fallbackFactory(final Class<T> fallbackFactory) {
public Builder fallbackFactory(final Class<? extends FallbackFactory<? extends T>> fallbackFactory) {
FeignClientsRegistrar.validateFallbackFactory(fallbackFactory);
this.feignClientFactoryBean.setFallbackFactory(fallbackFactory);
return this;

28
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientBuilderTests.java

@ -24,6 +24,7 @@ import java.util.Arrays; @@ -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 { @@ -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 { @@ -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 { @@ -159,4 +161,16 @@ public class FeignClientBuilderTests {
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