I have a Spring Boot Java application. There is a service class that throws a "401 Unauthorized" HttpClientErrorException since the access token used in the application has expired. I want to handle this exception globally for which I have used the @ControllerAdvice annotation. The error is:
Caused by: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
The class is:
@Slf4j
@EnableWebMvc
@ControllerAdvice(basePackages = Service.class)
public class HttpClientErrorHandler{
@ExceptionHandler(HttpClientErrorException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public String errorHandle(HttpClientErrorException e) {
log.error("log HttpClientErrorException: ", e);
return "HttpClientErrorException_message";
}
}
Since the exception was caused in the service class, I have mentioned it specifically in the basePackages. The entire configuration for the program is specified in the application.yml file. I have not used the xml configuration. I don't understand why the @ControllerAdvice annotation is not working. The program still throws the exception. Can someone explain?