Spring AI utility library providing retry mechanisms for AI API interactions with comprehensive error handling and exception classification
Complete API documentation for Spring AI Retry.
Utility class providing pre-configured retry templates and error handlers.
public abstract class RetryUtilsPackage: org.springframework.ai.retry
RetryUtils is an abstract utility class containing only static members. It cannot be instantiated and serves as a namespace for retry-related constants and configurations.
Class Characteristics:
public static final RetryTemplate DEFAULT_RETRY_TEMPLATEProduction-ready retry template with exponential backoff strategy.
Type: org.springframework.retry.support.RetryTemplate
Configuration:
TransientAiException, ResourceAccessExceptionThread Safety: Thread-safe, can be shared across threads
public static final RetryTemplate SHORT_RETRY_TEMPLATEFast-failing retry template optimized for testing.
Type: org.springframework.retry.support.RetryTemplate
Configuration:
TransientAiException, ResourceAccessExceptionThread Safety: Thread-safe, can be shared across threads
public static final ResponseErrorHandler DEFAULT_RESPONSE_ERROR_HANDLERHTTP error handler that categorizes errors into transient and non-transient.
Type: org.springframework.web.client.ResponseErrorHandler
Behavior:
NonTransientAiException (not retried)TransientAiException (retried)Thread Safety: Stateless and thread-safe
Exception for transient failures that may succeed on retry.
public class TransientAiException extends RuntimeExceptionPackage: org.springframework.ai.retry
Extends: java.lang.RuntimeException
public TransientAiException(String message)Creates a new transient exception with the specified message.
Parameters:
message - The error message describing the transient failurepublic TransientAiException(String message, Throwable cause)Creates a new transient exception with the specified message and cause.
Parameters:
message - The error message describing the transient failurecause - The underlying causepublic String getMessage()Returns the detail message string.
Returns: The detail message
public Throwable getCause()Returns the cause of this exception or null.
Returns: The cause or null
public StackTraceElement[] getStackTrace()Returns an array of stack trace elements.
Returns: Stack trace array
Exception for permanent failures that should not be retried.
public class NonTransientAiException extends RuntimeExceptionPackage: org.springframework.ai.retry
Extends: java.lang.RuntimeException
public NonTransientAiException(String message)Creates a new non-transient exception with the specified message.
Parameters:
message - The error message describing the permanent failurepublic NonTransientAiException(String message, Throwable cause)Creates a new non-transient exception with the specified message and cause.
Parameters:
message - The error message describing the permanent failurecause - The underlying causepublic String getMessage()Returns the detail message string.
Returns: The detail message
public Throwable getCause()Returns the cause of this exception or null.
Returns: The cause or null
public StackTraceElement[] getStackTrace()Returns an array of stack trace elements.
Returns: Stack trace array
HTTP error handler interface and implementation.
public interface ResponseErrorHandlerPackage: org.springframework.web.client
boolean hasError(ClientHttpResponse response) throws IOExceptionChecks if the HTTP response has an error status code.
Parameters:
response - The HTTP response to checkReturns: true if status code is 4xx or 5xx, false otherwise
Throws: IOException if an I/O error occurs
void handleError(ClientHttpResponse response) throws IOExceptionHandles HTTP errors by reading the response body and throwing appropriate exception.
Parameters:
response - The HTTP response to handleThrows:
NonTransientAiException - For 4xx client errorsTransientAiException - For 5xx server errorsIOException - If an I/O error occursvoid handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOExceptionHandles HTTP errors with additional URL and method context.
Parameters:
url - The request URImethod - The HTTP method usedresponse - The HTTP response to handleThrows:
NonTransientAiException - For 4xx client errorsTransientAiException - For 5xx server errorsIOException - If an I/O error occurspublic class RetryTemplate implements RetryOperationsPackage: org.springframework.retry.support
public <T, E extends Throwable> T execute(RetryCallback<T, E> callback) throws EExecute with retry logic.
Type Parameters:
T - Return typeE - Exception typeParameters:
callback - The retry callbackReturns: Result from callback
Throws: E if all retries exhausted
public <T, E extends Throwable> T execute(RetryCallback<T, E> callback, RecoveryCallback<T> recoveryCallback) throws EExecute with retry logic and recovery callback.
Type Parameters:
T - Return typeE - Exception typeParameters:
callback - The retry callbackrecoveryCallback - Called after all retries exhaustedReturns: Result from callback or recovery
public void registerListener(RetryListener listener)Register a listener for retry events.
Parameters:
listener - The retry listenerpublic RetryPolicy getRetryPolicy()Get the retry policy.
Returns: The retry policy
public void setRetryPolicy(RetryPolicy retryPolicy)Set the retry policy.
Parameters:
retryPolicy - The retry policypublic void setBackOffPolicy(BackOffPolicy backOffPolicy)Set the backoff policy.
Parameters:
backOffPolicy - The backoff policy@FunctionalInterface
public interface RetryCallback<T, E extends Throwable>Package: org.springframework.retry
T doWithRetry(RetryContext context) throws EPerform operation with retry context.
Parameters:
context - The retry contextReturns: Operation result
Throws: E on failure
@FunctionalInterface
public interface RecoveryCallback<T>Package: org.springframework.retry
T recover(RetryContext context) throws ExceptionPerform recovery operation.
Parameters:
context - The retry contextReturns: Recovery result
Throws: Exception on failure
public interface RetryContextPackage: org.springframework.retry
int getRetryCount()Get the current retry count (0-based).
Returns: Current retry count
Throwable getLastThrowable()Get the last exception that caused retry.
Returns: Last exception or null
String getName()Get the name of this context.
Returns: Context name
boolean hasAttribute(String key)Check if context has attribute.
Parameters:
key - Attribute keyReturns: true if attribute exists
Object getAttribute(String key)Get attribute value.
Parameters:
key - Attribute keyReturns: Attribute value or null
public interface ClientHttpResponse extends HttpInputMessage, CloseablePackage: org.springframework.http.client
HttpStatusCode getStatusCode() throws IOExceptionGet the HTTP status code.
Returns: Status code
Throws: IOException on error
String getStatusText() throws IOExceptionGet the HTTP status text.
Returns: Status text
Throws: IOException on error
HttpHeaders getHeaders()Get the response headers.
Returns: HTTP headers
InputStream getBody() throws IOExceptionGet the response body as input stream.
Returns: Body input stream
Throws: IOException on error
void close()Close this response.
public class ResourceAccessException extends RestClientExceptionPackage: org.springframework.web.client
Exception for I/O errors during HTTP communication.
public ResourceAccessException(String msg)Create with message.
Parameters:
msg - Error messagepublic ResourceAccessException(String msg, Throwable cause)Create with message and cause.
Parameters:
msg - Error messagecause - Underlying causepublic class WebClientRequestException extends WebClientExceptionPackage: org.springframework.web.reactive.function.client
Only available when spring-webflux is on the classpath. Automatically added to retry list by auto-configuration.
public WebClientRequestException(Throwable cause, HttpMethod method, URI uri, HttpHeaders headers)Create with cause, method, URI, and headers.
Parameters:
cause - Underlying causemethod - HTTP methoduri - Request URIheaders - Request headerspublic HttpMethod getMethod()Get HTTP method.
Returns: HTTP method
public URI getUri()Get request URI.
Returns: Request URI
public HttpHeaders getHeaders()Get request headers.
Returns: Request headers
Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-ai--spring-ai-retry@1.1.0