Skip to content

Commit 88ec452

Browse files
committed
Merge pull request #23764 from ask4gilles/23738
2 parents 1b43b09 + 2733511 commit 88ec452

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -33,7 +33,7 @@ public class RestClientResponseException extends RestClientException {
3333

3434
private static final long serialVersionUID = -8803556342728481792L;
3535

36-
private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
36+
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
3737

3838

3939
private final int rawStatusCode;
@@ -99,11 +99,22 @@ public byte[] getResponseBodyAsByteArray() {
9999
}
100100

101101
/**
102-
* Return the response body as a string.
102+
* Return the response body converted to String. The charset used is that
103+
* of the response "Content-Type" or otherwise {@code "UTF-8"}.
103104
*/
104105
public String getResponseBodyAsString() {
106+
return getResponseBodyAsString(DEFAULT_CHARSET);
107+
}
108+
109+
/**
110+
* Return the response body converted to String. The charset used is that
111+
* of the response "Content-Type" or otherwise the one given.
112+
* @param fallbackCharset the charset to use on if the response doesn't specify.
113+
* @since 5.1.11
114+
*/
115+
public String getResponseBodyAsString(Charset fallbackCharset) {
105116
if (this.responseCharset == null) {
106-
return new String(this.responseBody, DEFAULT_CHARSET);
117+
return new String(this.responseBody, fallbackCharset);
107118
}
108119
try {
109120
return new String(this.responseBody, this.responseCharset);

0 commit comments

Comments
 (0)