From f22d1eab231f06c4f964de72fe1633318d540a9f Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 29 Aug 2025 15:12:14 +0300 Subject: [PATCH] Add createException to ConvertibleClientHttpResponse Closes gh-35391 --- .../springframework/web/client/DefaultRestClient.java | 5 +++++ .../org/springframework/web/client/RestClient.java | 9 +++++++++ .../org/springframework/web/client/StatusHandler.java | 11 ++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java index d5d1fe3876d..600bafe9aa0 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java @@ -961,6 +961,11 @@ final class DefaultRestClient implements RestClient { Class bodyClass = bodyClass(type); return readWithMessageConverters(this.delegate, () -> {}, type, bodyClass, this.hints); } + + @Override + public RestClientResponseException createException() throws IOException { + return StatusHandler.createException(this, DefaultRestClient.this.messageConverters); + } } } diff --git a/spring-web/src/main/java/org/springframework/web/client/RestClient.java b/spring-web/src/main/java/org/springframework/web/client/RestClient.java index bb7e6180b2a..61652c6cf29 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestClient.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestClient.java @@ -878,6 +878,15 @@ public interface RestClient { * @return the body, or {@code null} if no response body was available */ @Nullable T bodyTo(ParameterizedTypeReference bodyType); + + /** + * Create a {@link RestClientResponseException} of the appropriate + * subtype depending on the response status code. The exception contains + * the status, headers, and body of the response. + * @throws IOException in case of a response failure (e.g. to obtain the status) + * @since 7.0 + */ + RestClientResponseException createException() throws IOException; } } diff --git a/spring-web/src/main/java/org/springframework/web/client/StatusHandler.java b/spring-web/src/main/java/org/springframework/web/client/StatusHandler.java index 96a7a5eb3d8..d943d980937 100644 --- a/spring-web/src/main/java/org/springframework/web/client/StatusHandler.java +++ b/spring-web/src/main/java/org/springframework/web/client/StatusHandler.java @@ -108,7 +108,16 @@ final class StatusHandler { }); } - private static RestClientResponseException createException( + /** + * Create a {@link RestClientResponseException} of the appropriate + * subtype depending on the response status code. + * @param response the response + * @param converters the converters to use to decode the body + * @return the created exception + * @throws IOException in case of a response failure (e.g. to obtain the status) + * @since 7.0 + */ + public static RestClientResponseException createException( ClientHttpResponse response, List> converters) throws IOException { HttpStatusCode statusCode = response.getStatusCode();