我正在尝试在我的 Spring Boot 应用程序中为所有类型的 RestClientResponseException 创建自己的自定义响应
控制器类抛出的自定义异常:
throw new HealthCheckRestException(ex.getResponseBodyAsString());
我的 ExceptionHandler 类是这样的:
@Order(Ordered.HIGHEST_PRECEDENCE)
@EnableWebMvc
@ControllerAdvice
public class AvailExceptionHandler {
private static final Logger logger = Logger.getLogger(AvailExceptionHandler.class);
@ExceptionHandler(value=HealthCheckRestException.class)
public AvailResponse handleHttpErrorResponse(final HealthCheckRestException ex, final HttpServletRequest request){
logger.error("RestClientException is thrown from HealthCheck API: with status :"
+ ex.getStatusText() + " and exception : "+ ex.getMessage());
return new AvailResponse(AvailStatus.ERROR);
}
}
我已经尝试了所有可能的情况,例如:
1) Trying @ExceptionHandler inside the controller class
2) Including @ControllerAdvice(basePackages = "com.org.availabillity.utilities") to scan for specific packages where the controller is defined.
3) Using @Order(Ordered.HIGHEST_PRECEDENCE) to set precedence
4) Using @RestControllerAdvice
抛出异常并调用带有@ExceptionHandler注解的方法后似乎没有任何拦截
现在卡住了一段时间,需要一些帮助。非常感谢您对此的帮助。
I am using spring-web-5.0.6.RELEASE