WebClientResponseException supports decoding empty content

Closes gh-31179
This commit is contained in:
rstoyanchev 2023-09-07 15:14:03 +01:00
parent 249f6f2da5
commit 7a516170ef
3 changed files with 46 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -50,6 +50,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyExtractors;
@ -233,6 +234,9 @@ class DefaultClientResponse implements ClientResponse {
private Function<ResolvableType, ?> initDecodeFunction(byte[] body, @Nullable MediaType contentType) {
return targetType -> {
if (ObjectUtils.isEmpty(body)) {
return null;
}
Decoder<?> decoder = null;
for (HttpMessageReader<?> reader : strategies().messageReaders()) {
if (reader.canRead(targetType, contentType)) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -233,22 +233,21 @@ public class WebClientResponseException extends WebClientException {
*/
@Nullable
public <E> E getResponseBodyAs(Class<E> targetType) {
return getResponseBodyAs(ResolvableType.forClass(targetType));
return decodeBody(ResolvableType.forClass(targetType));
}
/**
* Variant of {@link #getResponseBodyAs(Class)} with
* {@link ParameterizedTypeReference}.
* Variant of {@link #getResponseBodyAs(Class)} with {@link ParameterizedTypeReference}.
* @since 6.0
*/
@Nullable
public <E> E getResponseBodyAs(ParameterizedTypeReference<E> targetType) {
return getResponseBodyAs(ResolvableType.forType(targetType.getType()));
return decodeBody(ResolvableType.forType(targetType.getType()));
}
@SuppressWarnings("unchecked")
@Nullable
private <E> E getResponseBodyAs(ResolvableType targetType) {
private <E> E decodeBody(ResolvableType targetType) {
Assert.state(this.bodyDecodeFunction != null, "Decoder function not set");
return (E) this.bodyDecodeFunction.apply(targetType);
}

View File

@ -21,6 +21,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
import org.junit.jupiter.api.BeforeEach;
@ -45,6 +46,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -346,6 +348,40 @@ class DefaultClientResponseTests {
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
}
@Test
void createExceptionAndDecodeContent() {
byte[] bytes = "{\"name\":\"Jason\"}".getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(buffer));
given(mockExchangeStrategies.messageReaders()).willReturn(List.of(
new DecoderHttpMessageReader<>(new ByteArrayDecoder()),
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));
WebClientResponseException ex = defaultClientResponse.createException().block();
assertThat(ex.getResponseBodyAs(Map.class)).hasSize(1).containsEntry("name", "Jason");
}
@Test
void createExceptionAndDecodeWithoutContent() {
byte[] bytes = new byte[0];
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(buffer));
given(mockExchangeStrategies.messageReaders()).willReturn(List.of(
new DecoderHttpMessageReader<>(new ByteArrayDecoder()),
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));
WebClientResponseException ex = defaultClientResponse.createException().block();
assertThat(ex.getResponseBodyAs(Map.class)).isNull();
}
@Test
@SuppressWarnings("deprecation")
void createError() {