Unverified Commit 1511167f by rockinrimmer Committed by Spencer Gibb

Check if object is already a ResponseEntity, if so return instead of … (#1343)

* Check if object is already a ResponseEntity, if so return instead of wrapping in another ResponseEnitiy. Which caused headers and status code from original ResponseEnitiy to be lost and for body to be output with whole ResponseEntity instead of just the body * added test
parent a1bcaba5
...@@ -100,6 +100,10 @@ public class SingleReturnValueHandler implements AsyncHandlerMethodReturnValueHa ...@@ -100,6 +100,10 @@ public class SingleReturnValueHandler implements AsyncHandlerMethodReturnValueHa
.map(new Func1<Object, ResponseEntity<?>>() { .map(new Func1<Object, ResponseEntity<?>>() {
@Override @Override
public ResponseEntity<?> call(Object object) { public ResponseEntity<?> call(Object object) {
if (object instanceof ResponseEntity){
return (ResponseEntity) object;
}
return new ResponseEntity<Object>(object, return new ResponseEntity<Object>(object,
getHttpHeaders(responseEntity), getHttpHeaders(responseEntity),
getHttpStatus(responseEntity)); getHttpStatus(responseEntity));
......
...@@ -69,7 +69,13 @@ public class SingleReturnValueHandlerTest { ...@@ -69,7 +69,13 @@ public class SingleReturnValueHandlerTest {
@RequestMapping(method = RequestMethod.GET, value = "/singleWithResponse") @RequestMapping(method = RequestMethod.GET, value = "/singleWithResponse")
public ResponseEntity<Single<String>> singleWithResponse() { public ResponseEntity<Single<String>> singleWithResponse() {
return new ResponseEntity<>(Single.just("single value"), HttpStatus.NOT_FOUND); return new ResponseEntity<>(Single.just("single value"),
HttpStatus.NOT_FOUND);
}
@RequestMapping(method = RequestMethod.GET, value = "/singleCreatedWithResponse")
public Single<ResponseEntity<String>> singleOuterWithResponse() {
return Single.just(new ResponseEntity<>("single value", HttpStatus.CREATED));
} }
@RequestMapping(method = RequestMethod.GET, value = "/throw") @RequestMapping(method = RequestMethod.GET, value = "/throw")
...@@ -114,6 +120,19 @@ public class SingleReturnValueHandlerTest { ...@@ -114,6 +120,19 @@ public class SingleReturnValueHandlerTest {
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
} }
@Test
public void shouldRetrieveSingleValueWithCreatedCode() {
// when
ResponseEntity<String> response = restTemplate.getForEntity(path("/singleCreatedWithResponse"),
String.class);
// then
assertNotNull(response);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("single value", response.getBody());
}
private String path(String context) { private String path(String context) {
return String.format("http://localhost:%d%s", port, context); return String.format("http://localhost:%d%s", port, context);
} }
......
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