【问题标题】:Java SpringBoot Exception HandlingJava Spring Boot 异常处理
【发布时间】:2016-09-29 16:28:21
【问题描述】:

我正在编写一个 Spring Boot 应用程序。我现在正在实施异常处理。

我遇到了以下问题。

我的异常处理程序如下所示:

@ControllerAdvice
public class SpecialExceptionHandler {

@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseObject missingServletErrorHandler(HttpServletRequest req, MissingServletRequestParameterException exception) {

 //do something
return responseObject;
}}

我得到了一个看起来很通用的异常处理程序

@ControllerAdvice
public class GeneralExceptionHandler {    

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseObject defaultErrorHandler(HttpServletRequest req, Exception exception)  {
// do something
return responseObject;
}}

但我遇到了一个问题:我的应用程序总是运行到 GeneralExceptionHandler 而不是特殊处理程序,除非我将 GeneralExceptionHandler 类的名称更改为按字母顺序出现在特殊异常处理程序之后的名称(例如,将“GeneralExceptionHandler”更改为“zGeneralExceptionHandler” ')。

我该如何解决这个问题?

【问题讨论】:

    标签: java exception-handling spring-boot


    【解决方案1】:

    您可以尝试在您的 ControllerAdvices 上添加 @Order(N) 注释,以强制他们的注册顺序(其中 N 是定义顺序的 int)

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 2023-03-28
      • 2018-03-16
      • 2020-10-10
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多