Browse Source

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
pull/1158/head
Brian Clozel 10 years ago
parent
commit
01bd8b9e01
  1. 4
      spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java
  2. 11
      spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java

4
spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java

@ -149,6 +149,10 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand @@ -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());

11
spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java

@ -65,6 +65,7 @@ import static org.junit.Assert.assertNull; @@ -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 { @@ -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");

Loading…
Cancel
Save