Java Spring 3 控制器异常处理程序实现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2633451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:07:09  来源:igfitidea点击:

Spring 3 controller exception handler implementation problems

javaspring-mvcexception-handling

提问by

I was hoping to implement a single "ExceptionController" to handle exceptions that are thrown in execution of my other controllers' methods. I hadn't specified any HandlerExceptionResolver in my application context so according to the API documentationthe AnnotationMethodHandlerExceptionResolver should be started. I verified it as such in the source. So why doesn't the following work?

我希望实现一个“ExceptionController”来处理在执行其他控制器的方法时抛出的异常。我没有在我的应用程序上下文中指定任何 HandlerExceptionResolver 所以根据API 文档AnnotationMethodHandlerExceptionResolver 应该启动。我在来源中验证了它。那么为什么以下不起作用?

@Controller
public class ExceptionController {

  @ExceptionHandler(NullPointerException.class)
  public ModelAndView handleNullPointerException(NullPointerException ex) {
    // Do some stuff
    log.error(logging stuff)
    return myModelAndView;
  }
}

@Controller
public class AnotherController {

  @RequestMapping(value="/nullpointerpath")
  public String throwNullPointer() {
    throw new NullPointerException();
  }
}

I see in the debug logs that the three default exception handlers are asked for handling of the exception, but nothing is done and I see "DispatcherServlet - Could not complete request". Followed by the user being displayed the stacktrace and a 500 Internal error.

我在调试日志中看到三个默认异常处理程序被要求处理异常,但什么也没做,我看到“DispatcherServlet - 无法完成请求”。随后是向用户显示堆栈跟踪和 500 内部错误。

采纳答案by dteoh

Make sure your Exception handler is returning a view that exists/maps to a handler.

确保您的异常处理程序返回一个存在/映射到处理程序的视图。

回答by duffymo

I don't think this is a good design. Controllers in Spring handle HTTP requests and map to URLs. I don't think "exception" fits into either bin. It feels like a misuse of Spring to me.

我不认为这是一个好的设计。Spring 中的控制器处理 HTTP 请求并映射到 URL。我不认为“例外”适合任何一个垃圾箱。对我来说,这感觉像是对 Spring 的误用。

An exception is not an HTTP request. You don't map an exception to a URL. Therefore I'd conclude that controllers aren't intended to be treated as exception handlers.

异常不是 HTTP 请求。您不会将异常映射到 URL。因此,我得出的结论是,控制器不应被视为异常处理程序。

Controllers area part of the Spring API, but your design isn't using them as intended, so that's why it's not working. Re-think your design.

控制器Spring API 的一部分,但您的设计并未按预期使用它们,所以这就是它不起作用的原因。重新思考你的设计。

回答by benimben61

You should write your exceptionhandler to the same class with which you want to handle, like the following.

您应该将您的异常处理程序写入您要处理的同一个类,如下所示。

@Controller
public class AnotherController {
    @ExceptionHandler(NullPointerException.class)
    public ModelAndView handleNullPointerException(NullPointerException ex) {
        // Do some stuff.
        log.error(logging stuff)
        return myModelAndView;
    }

    @RequestMapping(value="/nullpointerpath")
    public String throwNullPointer() {
        throw new NullPointerException();
    }
}