mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 14:09:25 +00:00
Introduce RestClient.ResponseSpec#requiredBody
Closes gh-36173
This commit is contained in:
@@ -813,19 +813,31 @@ final class DefaultRestClient implements RestClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("NullAway") // See https://github.com/uber/NullAway/issues/1290
|
||||
public <T> @Nullable T body(Class<T> bodyType) {
|
||||
return executeAndExtract((request, response) -> readBody(request, response, bodyType, bodyType, this.hints));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("NullAway") // See https://github.com/uber/NullAway/issues/1290
|
||||
public <T> T requiredBody(Class<T> bodyType) {
|
||||
T body = body(bodyType);
|
||||
Assert.state(body != null, "The body must not be null");
|
||||
return body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T body(ParameterizedTypeReference<T> bodyType) {
|
||||
Type type = bodyType.getType();
|
||||
Class<T> bodyClass = bodyClass(type);
|
||||
return executeAndExtract((request, response) -> readBody(request, response, type, bodyClass, this.hints));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T requiredBody(ParameterizedTypeReference<T> bodyType) {
|
||||
T body = body(bodyType);
|
||||
Assert.state(body != null, "The body must not be null");
|
||||
return body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ResponseEntity<T> toEntity(Class<T> bodyType) {
|
||||
return toEntityInternal(bodyType, bodyType);
|
||||
|
||||
@@ -1027,9 +1027,24 @@ public interface RestClient {
|
||||
* response with a status code of 4xx or 5xx. Use
|
||||
* {@link #onStatus(Predicate, ErrorHandler)} to customize error response
|
||||
* handling.
|
||||
* @see #requiredBody(Class)
|
||||
*/
|
||||
<T> @Nullable T body(Class<T> bodyType);
|
||||
|
||||
/**
|
||||
* Extract the body as an object of the given type.
|
||||
* @param bodyType the type of return value
|
||||
* @param <T> the body type
|
||||
* @return the body
|
||||
* @throws IllegalStateException if no response body was available
|
||||
* @throws RestClientResponseException by default when receiving a
|
||||
* response with a status code of 4xx or 5xx. Use
|
||||
* {@link #onStatus(Predicate, ErrorHandler)} to customize error response
|
||||
* handling.
|
||||
* @since 7.0.4
|
||||
*/
|
||||
<T> T requiredBody(Class<T> bodyType);
|
||||
|
||||
/**
|
||||
* Extract the body as an object of the given type.
|
||||
* @param bodyType the type of return value
|
||||
@@ -1039,9 +1054,24 @@ public interface RestClient {
|
||||
* response with a status code of 4xx or 5xx. Use
|
||||
* {@link #onStatus(Predicate, ErrorHandler)} to customize error response
|
||||
* handling.
|
||||
* @see #requiredBody(ParameterizedTypeReference)
|
||||
*/
|
||||
<T> @Nullable T body(ParameterizedTypeReference<T> bodyType);
|
||||
|
||||
/**
|
||||
* Extract the body as an object of the given type.
|
||||
* @param bodyType the type of return value
|
||||
* @param <T> the body type
|
||||
* @return the body
|
||||
* @throws IllegalStateException if no response body was available
|
||||
* @throws RestClientResponseException by default when receiving a
|
||||
* response with a status code of 4xx or 5xx. Use
|
||||
* {@link #onStatus(Predicate, ErrorHandler)} to customize error response
|
||||
* handling.
|
||||
* @since 7.0.4
|
||||
*/
|
||||
<T> T requiredBody(ParameterizedTypeReference<T> bodyType);
|
||||
|
||||
/**
|
||||
* Return a {@code ResponseEntity} with the body decoded to an Object of
|
||||
* the given type.
|
||||
|
||||
Reference in New Issue
Block a user