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
This commit is contained in:
rockinrimmer
2016-09-15 15:15:42 +01:00
committed by Spencer Gibb
parent 08aae5be3b
commit 5ba0cc5860
2 changed files with 22 additions and 0 deletions

View File

@@ -100,6 +100,10 @@ public class SingleReturnValueHandler implements AsyncHandlerMethodReturnValueHa
.map(new Func1<Object, ResponseEntity<?>>() {
@Override
public ResponseEntity<?> call(Object object) {
if (object instanceof ResponseEntity){
return (ResponseEntity) object;
}
return new ResponseEntity<Object>(object,
getHttpHeaders(responseEntity),
getHttpStatus(responseEntity));

View File

@@ -69,6 +69,11 @@ public class SingleReturnValueHandlerTest {
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")
public Single<Object> error() {
@@ -114,6 +119,19 @@ public class SingleReturnValueHandlerTest {
assertNotNull(response);
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) {
return String.format("http://localhost:%d%s", port, context);