Skip to content

Commit 582b94d

Browse files
committed
Allow charset input in WebClientResponseException
Closes gh-26866
1 parent 443c34c commit 582b94d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.reactive.function.client;
1818

1919
import java.nio.charset.Charset;
20-
import java.nio.charset.StandardCharsets;
2120
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.Map;
@@ -207,9 +206,7 @@ public Mono<WebClientResponseException> createException() {
207206
.onErrorReturn(IllegalStateException.class::isInstance, EMPTY)
208207
.map(bodyBytes -> {
209208
HttpRequest request = this.requestSupplier.get();
210-
Charset charset = headers().contentType()
211-
.map(MimeType::getCharset)
212-
.orElse(StandardCharsets.ISO_8859_1);
209+
Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null);
213210
int statusCode = rawStatusCode();
214211
HttpStatus httpStatus = HttpStatus.resolve(statusCode);
215212
if (httpStatus != null) {

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/UnknownHttpStatusCodeException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ public UnknownHttpStatusCodeException(
5050
* @since 5.1.4
5151
*/
5252
public UnknownHttpStatusCodeException(
53-
int statusCode, HttpHeaders headers, byte[] responseBody, Charset responseCharset,
53+
int statusCode, HttpHeaders headers, byte[] responseBody, @Nullable Charset responseCharset,
5454
@Nullable HttpRequest request) {
5555

5656
super("Unknown status code [" + statusCode + "]", statusCode, "",

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ public class WebClientResponseException extends WebClientException {
4343

4444
private final HttpHeaders headers;
4545

46+
@Nullable
4647
private final Charset responseCharset;
4748

4849
@Nullable
@@ -97,7 +98,7 @@ public WebClientResponseException(String message, int statusCode, String statusT
9798
this.statusText = statusText;
9899
this.headers = (headers != null ? headers : HttpHeaders.EMPTY);
99100
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
100-
this.responseCharset = (charset != null ? charset : StandardCharsets.ISO_8859_1);
101+
this.responseCharset = charset;
101102
this.request = request;
102103
}
103104

@@ -139,10 +140,26 @@ public byte[] getResponseBodyAsByteArray() {
139140
}
140141

141142
/**
142-
* Return the response body as a string.
143+
* Return the response content as a String using the charset of media type
144+
* for the response, if available, or otherwise falling back on
145+
* {@literal ISO-8859-1}. Use {@link #getResponseBodyAsString(Charset)} if
146+
* you want to fall back on a different, default charset.
143147
*/
144148
public String getResponseBodyAsString() {
145-
return new String(this.responseBody, this.responseCharset);
149+
return getResponseBodyAsString(StandardCharsets.ISO_8859_1);
150+
}
151+
152+
/**
153+
* Variant of {@link #getResponseBodyAsString()} that allows specifying the
154+
* charset to fall back on, if a charset is not available from the media
155+
* type for the response.
156+
* @param defaultCharset the charset to use if the {@literal Content-Type}
157+
* of the response does not specify one.
158+
* @since 5.3.7
159+
*/
160+
public String getResponseBodyAsString(Charset defaultCharset) {
161+
return new String(this.responseBody,
162+
(this.responseCharset != null ? this.responseCharset : defaultCharset));
146163
}
147164

148165
/**

0 commit comments

Comments
 (0)