Improve handling of empty response with Mono<T>

Issue: SPR-17560
This commit is contained in:
Rossen Stoyanchev
2018-11-19 16:47:51 -05:00
parent 63275ae2b7
commit abf9ce8a34
3 changed files with 28 additions and 13 deletions

View File

@@ -119,7 +119,10 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
if (inputStream instanceof Mono) {
HttpHeaders headers = message.getHeaders();
return Mono.from(body)
.defaultIfEmpty(message.bufferFactory().wrap(new byte[0]))
.switchIfEmpty(Mono.defer(() -> {
headers.setContentLength(0);
return message.setComplete().then(Mono.empty());
}))
.flatMap(buffer -> {
headers.setContentLength(buffer.readableByteCount());
return message.writeWith(Mono.just(buffer));