Fix null body handling in ResponseEntityResultHandler

This commit fixes `ResponseEntityResultHandler` so that it only tries to
call `writeBody` if the `ResponseEntity` is not null. In case the
response entity body is null, the response is flushed right away and the
request is signaled as handled.

Issue: SPR-14663
This commit is contained in:
Brian Clozel
2016-09-02 23:01:16 +02:00
parent a10a8e56df
commit 01bd8b9e01
2 changed files with 15 additions and 0 deletions

View File

@@ -149,6 +149,10 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand
.filter(entry -> !responseHeaders.containsKey(entry.getKey()))
.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
}
if(httpEntity.getBody() == null) {
exchange.getResponse().setComplete();
return Mono.empty();
}
String etag = entityHeaders.getETag();
Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());

View File

@@ -65,6 +65,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.http.ResponseEntity.ok;
import static org.springframework.http.ResponseEntity.notFound;
/**
* Unit tests for {@link ResponseEntityResultHandler}. When adding a test also
@@ -168,6 +169,16 @@ public class ResponseEntityResultHandlerTests {
assertNull(this.response.getBody());
}
@Test
public void handleResponseEntityWithNullBody() throws Exception {
Object returnValue = Mono.just(notFound().build());
ResolvableType returnType = forClassWithGenerics(Mono.class, responseEntity(String.class));
HandlerResult result = handlerResult(returnValue, returnType);
this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode());
assertNull(this.response.getBody());
}
@Test
public void handleReturnTypes() throws Exception {
Object returnValue = ok("abc");