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:
committed by
Spencer Gibb
parent
08aae5be3b
commit
5ba0cc5860
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user