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