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:
@@ -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());
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user