Add support for feign OptionalDecoder

fixes gh-1982
This commit is contained in:
Spencer Gibb
2017-09-28 22:18:52 -04:00
parent 25337dd440
commit 91808bb042
7 changed files with 56 additions and 18 deletions

View File

@@ -114,6 +114,11 @@
<artifactId>feign-okhttp</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>

View File

@@ -46,6 +46,7 @@ import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.hystrix.HystrixFeign;
import feign.optionals.OptionalDecoder;
/**
* @author Dave Syer
@@ -69,7 +70,7 @@ public class FeignClientsConfiguration {
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
}
@Bean

View File

@@ -23,7 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Configuration;
@@ -36,6 +35,7 @@ import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.optionals.OptionalDecoder;
import feign.slf4j.Slf4jLogger;
/**
@@ -51,7 +51,7 @@ public class EnableFeignClientsTests {
@Test
public void decoderDefaultCorrect() {
ResponseEntityDecoder.class
OptionalDecoder.class
.cast(this.feignContext.getInstance("foo", Decoder.class));
}

View File

@@ -17,17 +17,12 @@
package org.springframework.cloud.netflix.feign;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
@@ -38,6 +33,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import feign.Contract;
import feign.Feign;
import feign.Logger;
@@ -51,6 +50,7 @@ import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.hystrix.HystrixFeign;
import feign.optionals.OptionalDecoder;
import feign.slf4j.Slf4jLogger;
/**
@@ -79,7 +79,7 @@ public class FeignClientOverrideDefaultsTests {
@Test
public void overrideDecoder() {
Decoder.Default.class.cast(this.context.getInstance("foo", Decoder.class));
ResponseEntityDecoder.class.cast(this.context.getInstance("bar", Decoder.class));
OptionalDecoder.class.cast(this.context.getInstance("bar", Decoder.class));
}
@Test

View File

@@ -16,14 +16,6 @@
package org.springframework.cloud.netflix.feign.valid;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@@ -34,6 +26,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -76,6 +69,15 @@ import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import feign.Client;
import feign.Feign;
import feign.Logger;
@@ -161,6 +163,9 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.GET, path = "/hello")
Hello getHello();
@RequestMapping(method = RequestMethod.GET, path = "/hello")
Optional<Hello> getOptionalHello();
@RequestMapping(method = RequestMethod.GET, path = "${feignClient.methodLevelRequestMappingPath}")
Hello getHelloUsingPropertyPlaceHolder();
@@ -240,6 +245,9 @@ public class FeignClientTests {
protected interface DecodingTestClient {
@RequestMapping(method = RequestMethod.GET, path = "/notFound")
ResponseEntity<String> notFound();
@RequestMapping(method = RequestMethod.GET, path = "/notFound")
Optional<String> optional();
}
@FeignClient(name = "localapp3", fallback = HystrixClientFallback.class)
@@ -518,6 +526,15 @@ public class FeignClientTests {
assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello);
}
@Test
public void testOptional() {
Optional<Hello> hello = this.testClient.getOptionalHello();
assertThat(hello)
.isNotNull()
.isPresent()
.contains(new Hello(HELLO_WORLD_1));
}
@Test
public void testGenericType() {
List<Hello> hellos = this.testClient.getHellos();
@@ -632,6 +649,12 @@ public class FeignClientTests {
assertNull("response body was not null", response.getBody());
}
@Test
public void testOptionalNotFound() {
Optional<String> s = decodingTestClient.optional();
assertThat(s).isNotPresent();
}
@Test
public void testConvertingExpander() {
assertEquals(Arg.A.toString(), testClient.getToString(Arg.A));

View File

@@ -16,7 +16,7 @@
<properties>
<archaius.version>0.7.5</archaius.version>
<eureka.version>1.8.2</eureka.version>
<feign.version>9.5.0</feign.version>
<feign.version>9.5.1</feign.version>
<hystrix.version>1.5.12</hystrix.version>
<ribbon.version>2.2.2</ribbon.version>
<servo.version>0.10.1</servo.version>
@@ -281,6 +281,11 @@
<artifactId>feign-hystrix</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>

View File

@@ -46,6 +46,10 @@
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>