Browse Source

Support empty body in Jackson2JsonDecoder.decodeToMono

Prior to this commit, extracting an HTTP response with an empty body
and no Content-Type header using the WebClient would:

* trigger the use of the Jackson2JsonDecoder
* throw a NoSuchElementException because of the use of `Flux.single()`

This commit changes this behavior to `Flux.singleOrEmpty()` to avoid
throwing exceptions for empty Flux instances.

Issue: SPR-14582
pull/1125/merge
Brian Clozel 10 years ago
parent
commit
e4575330c3
  1. 2
      spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonDecoder.java
  2. 12
      spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

2
spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonDecoder.java

@ -87,7 +87,7 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode @@ -87,7 +87,7 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
MimeType mimeType, Object... hints) {
JsonObjectDecoder objectDecoder = this.monoObjectDecoder;
return decodeInternal(objectDecoder, inputStream, elementType, mimeType, hints).single();
return decodeInternal(objectDecoder, inputStream, elementType, mimeType, hints).singleOrEmpty();
}
private Flux<Object> decodeInternal(JsonObjectDecoder objectDecoder, Publisher<DataBuffer> inputStream,

12
spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

@ -105,6 +105,18 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa @@ -105,6 +105,18 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
});
}
@Test
public void decodeEmptyBodyToMono() throws Exception {
Flux<DataBuffer> source = Flux.empty();
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Mono<Object> flux = new Jackson2JsonDecoder().decodeToMono(source, elementType, null);
TestSubscriber.subscribe(flux)
.assertNoError()
.assertComplete()
.assertValueCount(0);
}
void handle(List<Pojo> list) {
}

Loading…
Cancel
Save