Spring boot @ControllerAdvice在Spring Boot中永远不会触发

Spring boot @ControllerAdvice在Spring Boot中永远不会触发,spring-boot,exceptionhandler,controller-advice,Spring Boot,Exceptionhandler,Controller Advice,我正在尝试为Spring Boot应用程序中的所有类型的RestClientResponseException创建自己的自定义响应 从控制器类引发的自定义异常: throw new HealthCheckRestException(ex.getResponseBodyAsString()); 我的ExceptionHandler类如下所示: @Order(Ordered.HIGHEST_PRECEDENCE) @EnableWebMvc @ControllerAdvice public cla

我正在尝试为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

试试这样的方法:

import org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
导入org.springframework.core.Ordered;
导入org.springframework.core.annotation.Order;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.ExceptionHandler;
导入org.springframework.web.bind.annotation.RestControllerAdvice;
导入org.springframework.web.context.request.WebRequest;
导入org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
导入java.util.Map;
@再控制建议
@顺序(有序。最高优先级)
公共最终类GlobalExceptionHandler扩展了ResponseEntityExceptionHandler{
私有静态最终记录器log=LoggerFactory.getLogger(GlobalExceptionHandler.class);
私有静态最终布尔值INCLUDE_STACKTRACE=false;
@ExceptionHandler(Exception.class)
公共响应属性句柄(最终WebRequest请求、最终可丢弃异常){
debug(“针对未处理的异常执行的回退处理程序:”,异常);
返回新的ResponseEntity(新的DefaultErrorAttributes().getErrorAttributes(请求,包括堆栈跟踪),
HttpStatus.INTERNAL_SERVER_ERROR);
}
//IllegalArgumentException不是“完全正确的”,所以用您自己的替换它
@ExceptionHandler(IllegalArgumentException.class)
公共响应句柄1(最终WebRequest请求,最终可丢弃异常){
debug(“针对未处理的异常执行的回退处理程序:”,异常);
返回新的ResponseEntity(新的DefaultErrorAttributes().getErrorAttributes(请求,包括堆栈跟踪),
HttpStatus.INTERNAL_SERVER_ERROR);
}
//TODO:继续为特定情况添加异常处理程序
}
GlobalExceptionHandler
只是一个可以放置在任何地方的Spring组件/bean,只要您配置了要查找的位置

@EnableWebMvc
应放在用
@Configuration
注释的类中。此外,如果您使用的是
springboot
,很可能您不需要它,因为它会被推断出来


下面是我的Handler类中的一个小示例

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler({QueryNotFoundException.class,ContentNotAllowedException.class})
public final ResponseEntity<ApiError> handleException(Exception ex, WebRequest request) {
    HttpHeaders headers = new HttpHeaders();

    if (ex instanceof QueryNotFoundException) {
        HttpStatus status = HttpStatus.NOT_FOUND;
        QueryNotFoundException qnfe = (QueryNotFoundException) ex;
        return handleQueryNotFoundException(qnfe, headers, status, request);
     } else if (ex instanceof ContentNotAllowedException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        ContentNotAllowedException cnae = (ContentNotAllowedException) ex;
        return handleContentNotAllowedException(cnae, headers, status, request);
    } else {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return handleExceptionInternal(ex, null, headers, status, request);
    }
@ControllerAdvice
公共类GlobalExceptionHandler{
@ExceptionHandler({QueryNotFoundException.class,ContentNotAllowedException.class})
公共最终响应处理异常(异常示例,WebRequest请求){
HttpHeaders=新的HttpHeaders();
if(例如QueryNotFoundException的实例){
HttpStatus status=HttpStatus.NOT_FOUND;
QueryNotFoundException qnfe=(QueryNotFoundException)ex;
返回handleQueryNotFoundException(qnfe、头、状态、请求);
}else if(例如ContentNotAllowedException的实例){
HttpStatus status=HttpStatus.BAD_请求;
ContentNotAllowedException cnae=(ContentNotAllowedException)ex;
返回handleContentNotAllowedException(cnae、标题、状态、请求);
}否则{
HttpStatus status=HttpStatus.INTERNAL\u SERVER\u错误;
返回handleExceptionInternal(例如,null、头、状态、请求);
}

您可以发布项目的包结构吗?