Don’t go with @ExceptionHandler. It would become problematic for other custom expectations in future.
For your problem I would create
Create a Custom Exception:
public class CustomRestClientException extends RestClientException {
private final String requestUrl;
public CustomRestClientException(String message, Throwable cause, String requestUrl) {
super(message, cause);
this.requestUrl = requestUrl;
}
public String getRequestUrl() {
return requestUrl;
} }
Add an Interceptor:
public class RestClientInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
try {
return execution.execute(request, body);
} catch (RestClientException e) {
throw new CustomRestClientException("External Reqyest Exception", e, request.getURI().toString());
}
}}
Configure Your RestTemplate:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new RestClientInterceptor());