Introduce RestClient.ResponseSpec#requiredBody

Closes gh-36173
This commit is contained in:
Sébastien Deleuze
2026-01-19 18:15:34 +01:00
parent 9b95482557
commit 693ec8219f
3 changed files with 193 additions and 2 deletions
@@ -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.