From 9e0d8cae172e1c6a9a2a35011e1a90de435f7f23 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 29 Feb 2016 10:55:04 -0700 Subject: [PATCH] Support HttpEntity not just ResponseEntity fixes gh-859 --- .../feign/support/ResponseEntityDecoder.java | 22 ++++++---- .../netflix/feign/valid/FeignClientTests.java | 40 ++++++++++++++----- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/support/ResponseEntityDecoder.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/support/ResponseEntityDecoder.java index c0140e84..ba4149b0 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/support/ResponseEntityDecoder.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/support/ResponseEntityDecoder.java @@ -5,6 +5,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.LinkedList; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; @@ -33,13 +34,13 @@ public class ResponseEntityDecoder implements Decoder { public Object decode(final Response response, Type type) throws IOException, FeignException { - if (isParameterizeResponseEntity(type)) { + if (isParameterizeHttpEntity(type)) { type = ((ParameterizedType) type).getActualTypeArguments()[0]; Object decodedObject = decoder.decode(response, type); return createResponse(decodedObject, response); } - else if (isResponseEntity(type)) { + else if (isHttpEntity(type)) { return createResponse(null, response); } else { @@ -47,14 +48,19 @@ public class ResponseEntityDecoder implements Decoder { } } - private boolean isParameterizeResponseEntity(Type type) { - return type instanceof ParameterizedType - && ((ParameterizedType) type).getRawType().equals(ResponseEntity.class); + private boolean isParameterizeHttpEntity(Type type) { + if (type instanceof ParameterizedType) { + return isHttpEntity(((ParameterizedType) type).getRawType()); + } + return false; } - private boolean isResponseEntity(Type type) { - - return type instanceof Class && type.equals(ResponseEntity.class); + private boolean isHttpEntity(Type type) { + if (type instanceof Class) { + Class c = (Class) type; + return HttpEntity.class.isAssignableFrom(c); + } + return false; } @SuppressWarnings("unchecked") diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientTests.java index d7cd5af3..7113a0c1 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientTests.java @@ -54,6 +54,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.StaticServerList; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -87,6 +88,11 @@ import rx.Observable; @DirtiesContext public class FeignClientTests { + public static final String HELLO_WORLD_1 = "hello world 1"; + public static final String OI_TERRA_2 = "oi terra 2"; + public static final String MYHEADER1 = "myheader1"; + public static final String MYHEADER2 = "myheader2"; + @Value("${local.server.port}") private int port = 0; @@ -130,6 +136,9 @@ public class FeignClientTests { @RequestMapping(method = RequestMethod.HEAD, value = "/head") ResponseEntity head(); + + @RequestMapping(method = RequestMethod.GET, value = "/hello") + HttpEntity getHelloEntity(); } public static class TestClientConfig { @@ -139,7 +148,7 @@ public class FeignClientTests { return new RequestInterceptor() { @Override public void apply(RequestTemplate template) { - template.header("myheader1", "myheader1value"); + template.header(MYHEADER1, "myheader1value"); } }; } @@ -149,7 +158,7 @@ public class FeignClientTests { return new RequestInterceptor() { @Override public void apply(RequestTemplate template) { - template.header("myheader2", "myheader2value"); + template.header(MYHEADER2, "myheader2value"); } }; } @@ -220,7 +229,7 @@ public class FeignClientTests { @RequestMapping(method = RequestMethod.GET, value = "/hello") public Hello getHello() { - return new Hello("hello world 1"); + return new Hello(HELLO_WORLD_1); } @RequestMapping(method = RequestMethod.GET, value = "/hellos") @@ -232,14 +241,14 @@ public class FeignClientTests { @RequestMapping(method = RequestMethod.GET, value = "/hellostrings") public List getHelloStrings() { ArrayList hellos = new ArrayList<>(); - hellos.add("hello world 1"); - hellos.add("oi terra 2"); + hellos.add(HELLO_WORLD_1); + hellos.add(OI_TERRA_2); return hellos; } @RequestMapping(method = RequestMethod.GET, value = "/helloheaders") - public List getHelloHeaders(@RequestHeader("myheader1") String myheader1, - @RequestHeader("myheader2") String myheader2) { + public List getHelloHeaders(@RequestHeader(MYHEADER1) String myheader1, + @RequestHeader(MYHEADER2) String myheader2) { ArrayList headers = new ArrayList<>(); headers.add(myheader1); headers.add(myheader2); @@ -281,8 +290,8 @@ public class FeignClientTests { private static ArrayList getHelloList() { ArrayList hellos = new ArrayList<>(); - hellos.add(new Hello("hello world 1")); - hellos.add(new Hello("oi terra 2")); + hellos.add(new Hello(HELLO_WORLD_1)); + hellos.add(new Hello(OI_TERRA_2)); return hellos; } @@ -299,7 +308,7 @@ public class FeignClientTests { public void testSimpleType() { Hello hello = this.testClient.getHello(); assertNotNull("hello was null", hello); - assertEquals("first hello didn't match", new Hello("hello world 1"), hello); + assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello); } @Test @@ -332,7 +341,7 @@ public class FeignClientTests { assertNotNull("testClientServiceId was null", this.testClientServiceId); final Hello hello = this.testClientServiceId.getHello(); assertNotNull("The hello response was null", hello); - assertEquals("first hello didn't match", new Hello("hello world 1"), hello); + assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello); } @Test @@ -367,6 +376,15 @@ public class FeignClientTests { assertEquals("status code was wrong", HttpStatus.OK, response.getStatusCode()); } + @Test + public void testHttpEntity() { + HttpEntity entity = testClient.getHelloEntity(); + assertNotNull("entity was null", entity); + Hello hello = entity.getBody(); + assertNotNull("hello was null", hello); + assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello); + } + @Test public void testDecodeNotFound() { ResponseEntity response = decodingTestClient.notFound();