please look at retry spring boot example
@EnableRetry(fallback = RetryApiFallBack.class, extensions = @RetrofitInterceptorParam(sort = 10))
@RetrofitBuilder(baseUrl = "${app.hello.url}",
addCallAdapterFactory = {SimpleBodyCallAdapterFactoryBuilder.class, ReactorCallAdapterFactoryBuilder.class},
addConverterFactory = GsonConvertFactoryBuilder.class
)
public interface HelloApi {
@Retry(resourceName = "error400", fallbackMethod = "error400")
@GET("backend/v1/error/400")
HelloBean error400();
@Retry(resourceName = "error404")
@GET("backend/v1/error/404")
HelloBean error404();
@Retry(resourceName = "helloMono400", fallbackMethod = "helloMono400")
@GET("backend/v1/error/400")
Mono<HelloBean> helloMono400();
}@RestController
public class Hello2Controller {
@Autowired
private HelloApi helloApi;
@Autowired
private View error;
@GetMapping("/v2/hello/{message}")
public Mono<HelloBean> hello(@PathVariable String message) throws IOException {
return Mono.fromCallable(helloApi::error400);
}
@GetMapping("/v3/hello/{message}")
public Mono<HelloBean> helloMono400(@PathVariable String message) throws IOException {
return helloApi.helloMono400().onErrorResume(error -> {
log.info("error400 fallback {}", error.getMessage());
HelloBean helloBean = new HelloBean();
helloBean.setMessage("mono error400 fallback");
return Mono.just(helloBean);
});
}
}when you use async call like Rxjava or Reactor, you can not use fallback function.
- use
Mono.fromCallable(helloApi::error400), thehelloApi.error400()is sync call, then you can use fallback function. - use
helloApi.helloMono400().onErrorResume(error -> {}), thehelloApi.helloMono400()is async call, then you can not use fallback function. the fallback function will be executed on onErrorResume function.