diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java index bf7f68d8b8..8a38d77d4b 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java @@ -130,7 +130,7 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple @Nullable MimeType mimeType, @Nullable Map hints) { return DataBufferUtils.join(input, this.maxInMemorySize) - .map(dataBuffer -> decode(dataBuffer, elementType, mimeType, hints)); + .flatMap(dataBuffer -> Mono.justOrEmpty(decode(dataBuffer, elementType, mimeType, hints))); } @Override diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index cb44ad3cd6..2cbe2f2e35 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -29,6 +29,7 @@ import java.nio.file.Files; import java.time.Duration; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.function.Consumer; import java.util.stream.Stream; @@ -1080,6 +1081,24 @@ class WebClientIntegrationTests { .verify(Duration.ofSeconds(5)); } + @ParameterizedWebClientTest + void nullJsonResponseShouldBeReadAsEmpty(ClientHttpConnector connector) { + startServer(connector); + + prepareResponse(response -> response + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody("null")); + + Mono result = this.webClient.get() + .uri("/null") + .retrieve() + .bodyToMono(Map.class); + + StepVerifier.create(result) + .verifyComplete(); + } + private void prepareResponse(Consumer consumer) { MockResponse response = new MockResponse();