79135792

Date: 2024-10-29 03:58:39
Score: 0.5
Natty:
Report link

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());
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @ExceptionHandler
  • Low reputation (1):
Posted by: dheeraj reddy