Package org.springframework.web.client

# Class RestClientResponseException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.springframework.core.NestedRuntimeException
org.springframework.web.client.RestClientException
org.springframework.web.client.RestClientResponseException

All Implemented Interfaces:
  `Serializable`

Direct Known Subclasses:
  `HttpStatusCodeException`, `UnknownHttpStatusCodeException`

public class RestClientResponseException extends RestClientException
Common base class for exceptions that contain actual HTTP response data.

Since:
  4.3
Author:
  Rossen Stoyanchev
See Also:
  - Serialized Form

## Constructor Summary

Constructors
Constructor
Description
`RestClientResponseException(String message, int statusCode, String statusText, HttpHeaders headers, byte[] responseBody, Charset responseCharset)`
Construct a new instance of with the given response data.
`RestClientResponseException(String message, HttpStatusCode statusCode, String statusText, HttpHeaders headers, byte[] responseBody, Charset responseCharset)`
Construct a new instance of with the given response data.

## Method Summary

Modifier and Type
Method
Description
`int`
`getRawStatusCode()`
Deprecated.
in favor of `getStatusCode()`, for removal in 7.0
`<E> E`
`getResponseBodyAs(Class<E> targetType)`
Convert the error response content to the specified type.
`<E> E`
`getResponseBodyAs(org.springframework.core.ParameterizedTypeReference<E> targetType)`
Variant of `getResponseBodyAs(Class)` with `ParameterizedTypeReference`.
`byte[]`
`getResponseBodyAsByteArray()`
Return the response body as a byte array.
`String`
`getResponseBodyAsString()`
Return the response body converted to String.
`String`
`getResponseBodyAsString(Charset fallbackCharset)`
Return the response body converted to String.
`HttpHeaders`
`getResponseHeaders()`
Return the HTTP response headers.
`HttpStatusCode`
`getStatusCode()`
Return the HTTP status code.
`String`
`getStatusText()`
Return the HTTP status text.
`void`
`setBodyConvertFunction(Function<org.springframework.core.ResolvableType,?> bodyConvertFunction)`
Provide a function to use to decode the response error content via `getResponseBodyAs(Class)`.

### Methods inherited from class org.springframework.core.NestedRuntimeException

`contains, getMostSpecificCause, getRootCause`

### Methods inherited from class java.lang.Throwable

`addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### RestClientResponseException

public RestClientResponseException(String message, int statusCode, String statusText, @Nullable HttpHeaders headers, @Nullable byte[] responseBody, @Nullable Charset responseCharset)
Construct a new instance of with the given response data.

Parameters:
  `statusCode` - the raw status code value
  `statusText` - the status text
  `headers` - the response headers (may be `null`)
  `responseBody` - the response body content (may be `null`)
  `responseCharset` - the response body charset (may be `null`)

### RestClientResponseException

public RestClientResponseException(String message, HttpStatusCode statusCode, String statusText, @Nullable HttpHeaders headers, @Nullable byte[] responseBody, @Nullable Charset responseCharset)
Construct a new instance of with the given response data.

Parameters:
  `statusCode` - the raw status code value
  `statusText` - the status text
  `headers` - the response headers (may be `null`)
  `responseBody` - the response body content (may be `null`)
  `responseCharset` - the response body charset (may be `null`)
Since:
  6.0

## Method Details

### getStatusCode

public HttpStatusCode getStatusCode()
Return the HTTP status code.

Since:
  6.0

### getRawStatusCode

@Deprecated(since="6.0") public int getRawStatusCode()
Deprecated.
in favor of `getStatusCode()`, for removal in 7.0
Return the raw HTTP status code value.

### getStatusText

public String getStatusText()
Return the HTTP status text.

### getResponseHeaders

@Nullable public HttpHeaders getResponseHeaders()
Return the HTTP response headers.

### getResponseBodyAsByteArray

public byte[] getResponseBodyAsByteArray()
Return the response body as a byte array.

### getResponseBodyAsString

public String getResponseBodyAsString()
Return the response body converted to String. The charset used is that of the response "Content-Type" or otherwise `"UTF-8"`.

### getResponseBodyAsString

public String getResponseBodyAsString(Charset fallbackCharset)
Return the response body converted to String. The charset used is that of the response "Content-Type" or otherwise the one given.

Parameters:
  `fallbackCharset` - the charset to use on if the response doesn't specify.
Since:
  5.1.11

### getResponseBodyAs

@Nullable public <E> E getResponseBodyAs(Class<E> targetType)
Convert the error response content to the specified type.

Type Parameters:
  `E` - the expected target type
Parameters:
  `targetType` - the type to convert to
Returns:
  the converted object, or `null` if there is no content
Since:
  6.0

### getResponseBodyAs

@Nullable public <E> E getResponseBodyAs(org.springframework.core.ParameterizedTypeReference<E> targetType)
Variant of `getResponseBodyAs(Class)` with `ParameterizedTypeReference`.

Since:
  6.0

### setBodyConvertFunction

public void setBodyConvertFunction(Function<org.springframework.core.ResolvableType,?> bodyConvertFunction)
Provide a function to use to decode the response error content via `getResponseBodyAs(Class)`.

Parameters:
  `bodyConvertFunction` - the function to use
Since:
  6.0