Fix empty body writing in EncoderHttpMessageWriter

Prior to this commit, an bug introduced in SPR-16949 prevented
`Mono.empty` bodies from being written to the response.

This commit ensures that empty bodies still trigger the writing to the
response and does not hang the processing of the exchange.

Issue: SPR-17220
This commit is contained in:
Brian Clozel
2018-08-27 21:16:37 +02:00
parent 1dac0df38b
commit 280da61d5c
2 changed files with 43 additions and 12 deletions

View File

@@ -48,6 +48,7 @@ import org.springframework.util.Assert;
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @author Brian Clozel
* @since 5.0
* @param <T> the type of objects in the input stream
*/
@@ -119,9 +120,10 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
HttpHeaders headers = message.getHeaders();
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
return Mono.from(body)
.flatMap(dataBuffer -> {
headers.setContentLength(dataBuffer.readableByteCount());
return message.writeWith(Mono.just(dataBuffer));
.defaultIfEmpty(message.bufferFactory().wrap(new byte[0]))
.flatMap(buffer -> {
headers.setContentLength(buffer.readableByteCount());
return message.writeWith(Mono.just(buffer));
});
}
}