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
Cherry-picked from: 280da61d5c
This commit is contained in:
Brian Clozel
2018-08-27 21:16:37 +02:00
parent 1e8cb5fc59
commit 6189e17d7c
2 changed files with 43 additions and 12 deletions

View File

@@ -47,6 +47,7 @@ import org.springframework.util.Assert;
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @author Brian Clozel
* @since 5.0
*/
public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
@@ -107,9 +108,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));
});
}
}