简体   繁体   English

Spring 引导:无法在 @ControllerAdvice 中捕获异常

[英]Spring Boot: Cannot catch exception in @ControllerAdvice

I have 2 exception handler classes annotated with @RestControllerAdvice and:我有 2 个使用@RestControllerAdvice注释的异常处理程序类,并且:

I use the first one as global exception handler to catch exceptions:我使用第一个作为全局异常处理程序来捕获异常:

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    protected ResponseEntity<Object> handleMethodArgumentNotValid(...) {
      // ...
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<Object> handleAllUncaughtException(Exception ex, WebRequest request) {
        // ...
    }

    // code omitted for clarity
}

and the second for validation exceptions (I creates custom validation):第二个用于验证异常(我创建自定义验证):

@RestControllerAdvice
public class ValidationExceptionHandler { // did not extend from extends ResponseEntityExceptionHandler

    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    protected ValidationErrorResponse onConstraintValidationException(ConstraintViolationException e) {
        // ...
    }
}

When I move onConstraintValidationException to GlobalExceptionHandler class, I catch validation exception and display corresponding message.当我将onConstraintValidationException移动到GlobalExceptionHandler class 时,我捕获了验证异常并显示相应的消息。 But when it is in the second ControllerAdvice class ( ValidationExceptionHandler ) as shown above, code does not hit onConstraintValidationException method.但是当它在第二个 ControllerAdvice class ( ValidationExceptionHandler ) 中时,代码不会命中onConstraintValidationException方法。

I also tried to extend the second class from ResponseEntityExceptionHandler , but does not make any sense.我还尝试从ResponseEntityExceptionHandler扩展第二个 class ,但没有任何意义。

So, what is the problem and how can I fix it?那么,问题是什么,我该如何解决?

In your case it could be question of priorities... The first exception handler has highest priority, and probably the handleAllUncaughtException is going to capture all the exceptions.在您的情况下,这可能是优先级问题......第一个异常处理程序具有最高优先级,并且可能 handleAllUncaughtException 将捕获所有异常。

For catching the ConstraintViolationException in the second handler (ValidationExceptionHandler) you should give to it the highest priority, like so:为了在第二个处理程序 (ValidationExceptionHandler) 中捕获 ConstraintViolationException,您应该给它最高优先级,如下所示:

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ValidationExceptionHandler { 

and to the first one lowest:和第一个最低的:

@RestControllerAdvice
@Order(Ordered.LOWEST_PRECEDENCE)
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

Can have a look at this discussion, it can be interesting to learn more about how it works and have some alternative solutions: Setting Precedence of Multiple @ControllerAdvice @ExceptionHandlers可以看看这个讨论,了解更多关于它是如何工作的并有一些替代解决方案可能会很有趣: 设置多个@ControllerAdvice @ExceptionHandlers 的优先级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM