当前位置: 首页 > 知识库问答 >
问题:

Spring:RestController和Controller的不同异常处理

温翔宇
2023-03-14
  • RestControllerExceptionHandler-对于使用@RestController
  • 注释的任何控制器,它都应以json形式返回错误响应
  • ControllerExceptionHandler-应将任何其他控制器的错误消息打印到屏幕(用@controller注释)

问题是,当我声明这两个异常处理程序时,spring总是使用ControllerExceptionHandler,而从不使用RestControllerExceptionHandler来处理异常。

如何使这起作用?顺便说一句:我尝试使用@order注释,但这似乎不起作用。

以下是我的异常处理程序:

// should handle all exception for classes annotated with         
@ControllerAdvice(annotations = RestController.class)
public class RestControllerExceptionHandler {


  @ExceptionHandler(Exception.class)
  public ResponseEntity<ErrorResponse> handleUnexpectedException(Exception e) {

    // below object should be serialized to json
    ErrorResponse errorResponse = new ErrorResponse("asdasd"); 

    return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
  }

// should handle exceptions for all the other controllers
@ControllerAdvice(annotations = Controller.class)
public class ControllerExceptionHandler {


  @ExceptionHandler(Exception.class)
  public ResponseEntity<String> handleUnexpectedException(Exception e) {
    return new ResponseEntity<String>("Unexpected exception, HttpStatus.INTERNAL_SERVER_ERROR);
  }


}
}

当我删除ControllerExceptionHandler时,spring才正确调用RestControllerExceptionHandler(仅适用于用@RestController批注的类)....但是,当我添加ControllerExceptionHandler时,所有操作都将通过ControllerExceptionHandler进行。为什么?

共有1个答案

仲孙经赋
2023-03-14

经过一些更深入的调查,似乎字母顺序很重要:/。

当我将我的RestControllerExceptionHandler重命名为ArestControllerExceptionHandler(按字母顺序在ControllerExceptionHandler)时,所有工作都按预期进行!ARestControllerExceptionHandler正确处理来自RestControllers的异常,ControllerExceptionHandler处理来自其他控制器的异常。

我在spring中为此创建了一个bug:https://jira.spring.io/browse/spr-15432

---编辑:

我收到了SPR-15432的答案,其中建议可以使用@order(org.springframework.core.annotation.order)注释或通过实现有序接口来解决这种情况。

这以前对我并不起作用,但似乎我导入了错误的@order注释。(来自log4j2而不是spring)。修好这个后,它就能工作了。固定版本如下:

// should handle all exception for classes annotated with         
@ControllerAdvice(annotations = RestController.class)
@Order(1) // NOTE: order 1 here
public class RestControllerExceptionHandler {


  @ExceptionHandler(Exception.class)
  public ResponseEntity<ErrorResponse> handleUnexpectedException(Exception e) {

    // below object should be serialized to json
    ErrorResponse errorResponse = new ErrorResponse("asdasd"); 

    return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
  }
}
// should handle exceptions for all the other controllers
@ControllerAdvice(annotations = Controller.class)
@Order(2)  // NOTE: order 2 here
public class ControllerExceptionHandler {


  @ExceptionHandler(Exception.class)
  public ResponseEntity<String> handleUnexpectedException(Exception e) {
    return new ResponseEntity<String>("Unexpected exception, HttpStatus.INTERNAL_SERVER_ERROR);
  }
}
 类似资料:
  • 是否可以处理特定url模式的404状态?我想显示登录页面,如果有人去'localhost:8080/login/other'但没有请求映射'login/other'。Controller类如下所示: 我无法添加“/login/**”,因为它与我静态内容匹配,而对js或css的任何请求都与endpoint匹配。HTML示例:

  • 我正在使用Spring Boot 1.5.9开发我的应用程序。我需要实现jwt身份验证,我使用了jjwt库。以下代码来自我的自定义身份验证安全筛选器,它继承自。在这里,我试图从令牌解析用户名,当自动解析用户名时,jwt会验证用户名,并检查令牌的过期时间。我调试了它,它可以工作,所以接下来我想向客户端应用发送正确的消息,说明身份验证失败的原因。我想抛出一个ExpiredJwtException,并使

  • 我有例外: 其他自定义异常从它继承: 处理它们。现在看起来就像是在那里: ExceptionHandler正确处理所有自定义异常,排除 在验证jwm-token时,此异常只在一种情况下抛出。 这样,Console中有3个stacktraces日志: 仅前端重装 请告诉我,怎么了。 附注。完整StackTrace: null 2019-07-14 17:04:38.791错误24060---[nio

  • 我使用的是改型2.0.1。我想处理所有类型的网络错误和异常(如没有网络连接,超时错误,服务器找不到等)我已经找到这个链接。但是其中一些方法在V1.8.0中被否决了。在2.0.1中如何做到这一点?

  • 本文向大家介绍.NET中的异常和异常处理用法分析,包括了.NET中的异常和异常处理用法分析的使用技巧和注意事项,需要的朋友参考一下 本文较为详细的分析了.NET中的异常和异常处理用法。分享给大家供大家参考。具体分析如下: .NET中的异常(Exception) .net中的中异常的父类是Exception,大多数异常一般继承自Exception。 可以通过编写一个继承自Exception的类的方式

  • 您需要修复网页抛出的异常和在JavaScript中调试错误。 如果你能得到页面异常和JavaScript错误的细节,在实际工作中非常有用 。当页面抛出异常或脚本产生错误时,Console(控制台)面板会提供明确的可靠信息,以帮助您找到并解决问题。 在Console(控制台)面板中,您可以跟踪异常及跟踪导致异常的执行路径,显式地或隐式地捕获它们(或忽略它们),甚至可以设置错误处理程序来自动收集和处理