Allow charset input in WebClientResponseException
Closes gh-26866
This commit is contained in:
parent
443c34cc90
commit
582b94d50e
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.web.reactive.function.client;
|
package org.springframework.web.reactive.function.client;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -207,9 +206,7 @@ class DefaultClientResponse implements ClientResponse {
|
||||||
.onErrorReturn(IllegalStateException.class::isInstance, EMPTY)
|
.onErrorReturn(IllegalStateException.class::isInstance, EMPTY)
|
||||||
.map(bodyBytes -> {
|
.map(bodyBytes -> {
|
||||||
HttpRequest request = this.requestSupplier.get();
|
HttpRequest request = this.requestSupplier.get();
|
||||||
Charset charset = headers().contentType()
|
Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null);
|
||||||
.map(MimeType::getCharset)
|
|
||||||
.orElse(StandardCharsets.ISO_8859_1);
|
|
||||||
int statusCode = rawStatusCode();
|
int statusCode = rawStatusCode();
|
||||||
HttpStatus httpStatus = HttpStatus.resolve(statusCode);
|
HttpStatus httpStatus = HttpStatus.resolve(statusCode);
|
||||||
if (httpStatus != null) {
|
if (httpStatus != null) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -50,7 +50,7 @@ public class UnknownHttpStatusCodeException extends WebClientResponseException {
|
||||||
* @since 5.1.4
|
* @since 5.1.4
|
||||||
*/
|
*/
|
||||||
public UnknownHttpStatusCodeException(
|
public UnknownHttpStatusCodeException(
|
||||||
int statusCode, HttpHeaders headers, byte[] responseBody, Charset responseCharset,
|
int statusCode, HttpHeaders headers, byte[] responseBody, @Nullable Charset responseCharset,
|
||||||
@Nullable HttpRequest request) {
|
@Nullable HttpRequest request) {
|
||||||
|
|
||||||
super("Unknown status code [" + statusCode + "]", statusCode, "",
|
super("Unknown status code [" + statusCode + "]", statusCode, "",
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -43,6 +43,7 @@ public class WebClientResponseException extends WebClientException {
|
||||||
|
|
||||||
private final HttpHeaders headers;
|
private final HttpHeaders headers;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private final Charset responseCharset;
|
private final Charset responseCharset;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -97,7 +98,7 @@ public class WebClientResponseException extends WebClientException {
|
||||||
this.statusText = statusText;
|
this.statusText = statusText;
|
||||||
this.headers = (headers != null ? headers : HttpHeaders.EMPTY);
|
this.headers = (headers != null ? headers : HttpHeaders.EMPTY);
|
||||||
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
|
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
|
||||||
this.responseCharset = (charset != null ? charset : StandardCharsets.ISO_8859_1);
|
this.responseCharset = charset;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,10 +140,26 @@ public class WebClientResponseException extends WebClientException {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the response body as a string.
|
* Return the response content as a String using the charset of media type
|
||||||
|
* for the response, if available, or otherwise falling back on
|
||||||
|
* {@literal ISO-8859-1}. Use {@link #getResponseBodyAsString(Charset)} if
|
||||||
|
* you want to fall back on a different, default charset.
|
||||||
*/
|
*/
|
||||||
public String getResponseBodyAsString() {
|
public String getResponseBodyAsString() {
|
||||||
return new String(this.responseBody, this.responseCharset);
|
return getResponseBodyAsString(StandardCharsets.ISO_8859_1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variant of {@link #getResponseBodyAsString()} that allows specifying the
|
||||||
|
* charset to fall back on, if a charset is not available from the media
|
||||||
|
* type for the response.
|
||||||
|
* @param defaultCharset the charset to use if the {@literal Content-Type}
|
||||||
|
* of the response does not specify one.
|
||||||
|
* @since 5.3.7
|
||||||
|
*/
|
||||||
|
public String getResponseBodyAsString(Charset defaultCharset) {
|
||||||
|
return new String(this.responseBody,
|
||||||
|
(this.responseCharset != null ? this.responseCharset : defaultCharset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue