<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.3</version><relativePath/></parent><groupId>com.namastecode</groupId><artifactId>restclient-retry</artifactId><version>0.0.1-SNAPSHOT</version><name>restclient-retry</name><description>Demo project for RestClient with Spring Retry</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId><version>2.0.5</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>6.1.4</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
package com.namastecode.retry;
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}
packagecom.namastecode.retry;importorg.springframework.retry.annotation.Backoff;importorg.springframework.retry.annotation.EnableRetry;importorg.springframework.retry.annotation.Retryable;importorg.springframework.web.client.RestClient;@EnableRetrypublicclassExampleService{privatefinalRestClientrestClient;publicExampleService(){this.restClient=RestClient.create();}@Retryable(maxAttempts=5,backoff=@Backoff(delay=1000))publicStringperformRestCall(){returnrestClient.get().uri("https://example.com").retrieve().body(String.class);}@Retryable(maxAttempts=3,retryFor={CustomException.class},backoff=@Backoff(delay=500,multiplier=2.0))publicStringperformRestCallWithCustomException(){Stringresponse=restClient.get().uri("https://example.com/api/data").retrieve().body(String.class);if(response==null||response.isEmpty()){thrownewCustomException("Response is empty");}returnresponse;}}
package com.namastecode.retry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestClientRetryApplication {
public static void main(String[] args) {
SpringApplication.run(RestClientRetryApplication.class, args);
}
}