Commit 9e0d8cae by Spencer Gibb

Support HttpEntity not just ResponseEntity

fixes gh-859
parent b6fef047
...@@ -5,6 +5,7 @@ import java.lang.reflect.ParameterizedType; ...@@ -5,6 +5,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.LinkedList; import java.util.LinkedList;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
...@@ -33,13 +34,13 @@ public class ResponseEntityDecoder implements Decoder { ...@@ -33,13 +34,13 @@ public class ResponseEntityDecoder implements Decoder {
public Object decode(final Response response, Type type) throws IOException, public Object decode(final Response response, Type type) throws IOException,
FeignException { FeignException {
if (isParameterizeResponseEntity(type)) { if (isParameterizeHttpEntity(type)) {
type = ((ParameterizedType) type).getActualTypeArguments()[0]; type = ((ParameterizedType) type).getActualTypeArguments()[0];
Object decodedObject = decoder.decode(response, type); Object decodedObject = decoder.decode(response, type);
return createResponse(decodedObject, response); return createResponse(decodedObject, response);
} }
else if (isResponseEntity(type)) { else if (isHttpEntity(type)) {
return createResponse(null, response); return createResponse(null, response);
} }
else { else {
...@@ -47,14 +48,19 @@ public class ResponseEntityDecoder implements Decoder { ...@@ -47,14 +48,19 @@ public class ResponseEntityDecoder implements Decoder {
} }
} }
private boolean isParameterizeResponseEntity(Type type) { private boolean isParameterizeHttpEntity(Type type) {
return type instanceof ParameterizedType if (type instanceof ParameterizedType) {
&& ((ParameterizedType) type).getRawType().equals(ResponseEntity.class); return isHttpEntity(((ParameterizedType) type).getRawType());
}
return false;
} }
private boolean isResponseEntity(Type type) { private boolean isHttpEntity(Type type) {
if (type instanceof Class) {
return type instanceof Class && type.equals(ResponseEntity.class); Class c = (Class) type;
return HttpEntity.class.isAssignableFrom(c);
}
return false;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
...@@ -54,6 +54,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClients; ...@@ -54,6 +54,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.StaticServerList; import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
...@@ -87,6 +88,11 @@ import rx.Observable; ...@@ -87,6 +88,11 @@ import rx.Observable;
@DirtiesContext @DirtiesContext
public class FeignClientTests { 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}") @Value("${local.server.port}")
private int port = 0; private int port = 0;
...@@ -130,6 +136,9 @@ public class FeignClientTests { ...@@ -130,6 +136,9 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.HEAD, value = "/head") @RequestMapping(method = RequestMethod.HEAD, value = "/head")
ResponseEntity head(); ResponseEntity head();
@RequestMapping(method = RequestMethod.GET, value = "/hello")
HttpEntity<Hello> getHelloEntity();
} }
public static class TestClientConfig { public static class TestClientConfig {
...@@ -139,7 +148,7 @@ public class FeignClientTests { ...@@ -139,7 +148,7 @@ public class FeignClientTests {
return new RequestInterceptor() { return new RequestInterceptor() {
@Override @Override
public void apply(RequestTemplate template) { public void apply(RequestTemplate template) {
template.header("myheader1", "myheader1value"); template.header(MYHEADER1, "myheader1value");
} }
}; };
} }
...@@ -149,7 +158,7 @@ public class FeignClientTests { ...@@ -149,7 +158,7 @@ public class FeignClientTests {
return new RequestInterceptor() { return new RequestInterceptor() {
@Override @Override
public void apply(RequestTemplate template) { public void apply(RequestTemplate template) {
template.header("myheader2", "myheader2value"); template.header(MYHEADER2, "myheader2value");
} }
}; };
} }
...@@ -220,7 +229,7 @@ public class FeignClientTests { ...@@ -220,7 +229,7 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.GET, value = "/hello") @RequestMapping(method = RequestMethod.GET, value = "/hello")
public Hello getHello() { public Hello getHello() {
return new Hello("hello world 1"); return new Hello(HELLO_WORLD_1);
} }
@RequestMapping(method = RequestMethod.GET, value = "/hellos") @RequestMapping(method = RequestMethod.GET, value = "/hellos")
...@@ -232,14 +241,14 @@ public class FeignClientTests { ...@@ -232,14 +241,14 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.GET, value = "/hellostrings") @RequestMapping(method = RequestMethod.GET, value = "/hellostrings")
public List<String> getHelloStrings() { public List<String> getHelloStrings() {
ArrayList<String> hellos = new ArrayList<>(); ArrayList<String> hellos = new ArrayList<>();
hellos.add("hello world 1"); hellos.add(HELLO_WORLD_1);
hellos.add("oi terra 2"); hellos.add(OI_TERRA_2);
return hellos; return hellos;
} }
@RequestMapping(method = RequestMethod.GET, value = "/helloheaders") @RequestMapping(method = RequestMethod.GET, value = "/helloheaders")
public List<String> getHelloHeaders(@RequestHeader("myheader1") String myheader1, public List<String> getHelloHeaders(@RequestHeader(MYHEADER1) String myheader1,
@RequestHeader("myheader2") String myheader2) { @RequestHeader(MYHEADER2) String myheader2) {
ArrayList<String> headers = new ArrayList<>(); ArrayList<String> headers = new ArrayList<>();
headers.add(myheader1); headers.add(myheader1);
headers.add(myheader2); headers.add(myheader2);
...@@ -281,8 +290,8 @@ public class FeignClientTests { ...@@ -281,8 +290,8 @@ public class FeignClientTests {
private static ArrayList<Hello> getHelloList() { private static ArrayList<Hello> getHelloList() {
ArrayList<Hello> hellos = new ArrayList<>(); ArrayList<Hello> hellos = new ArrayList<>();
hellos.add(new Hello("hello world 1")); hellos.add(new Hello(HELLO_WORLD_1));
hellos.add(new Hello("oi terra 2")); hellos.add(new Hello(OI_TERRA_2));
return hellos; return hellos;
} }
...@@ -299,7 +308,7 @@ public class FeignClientTests { ...@@ -299,7 +308,7 @@ public class FeignClientTests {
public void testSimpleType() { public void testSimpleType() {
Hello hello = this.testClient.getHello(); Hello hello = this.testClient.getHello();
assertNotNull("hello was null", hello); 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 @Test
...@@ -332,7 +341,7 @@ public class FeignClientTests { ...@@ -332,7 +341,7 @@ public class FeignClientTests {
assertNotNull("testClientServiceId was null", this.testClientServiceId); assertNotNull("testClientServiceId was null", this.testClientServiceId);
final Hello hello = this.testClientServiceId.getHello(); final Hello hello = this.testClientServiceId.getHello();
assertNotNull("The hello response was null", hello); 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 @Test
...@@ -368,6 +377,15 @@ public class FeignClientTests { ...@@ -368,6 +377,15 @@ public class FeignClientTests {
} }
@Test @Test
public void testHttpEntity() {
HttpEntity<Hello> 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() { public void testDecodeNotFound() {
ResponseEntity response = decodingTestClient.notFound(); ResponseEntity response = decodingTestClient.notFound();
assertNotNull("response was null", response); assertNotNull("response was null", response);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment