Spring MVC中的500错误处理

时间:2017-08-10 12:54:17

标签: java spring jsp spring-mvc error-handling

我用hybris开发了我的项目。我想在 500Error.jsp 中找到一些模板 但我无法处理500错误。接下来我无法获得jsp文件。

我收到了这个错误:

SEVERE: Servlet.service() for servlet DispatcherServlet threw exception
java.lang.NullPointerException

Aug 10, 2017 3:14:07 PM org.apache.catalina.core.StandardHostValve custom
SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/serverError]
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
web.xml文件中的

<error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/serverError</location>
</error-page>

方法的Controller.java中

@RequestMapping(value = "serverError", method = RequestMethod.GET)
    public String renderErrorPage(final Model model, HttpServletRequest httpRequest) throws CMSItemNotFoundException {

4 个答案:

答案 0 :(得分:2)

您需要使用@ControllerAdvice,如下例所示:

@ControllerAdvice
public class ControllerExceptionHandler {

  private static Logger log = LoggerFactory.getLogger(ControllerExceptionHandler.class);

  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  @ExceptionHandler(Exception.class)
  public void notFoundHandler() {
    log.debug("Item not found. HTTP 500 returned.");
  }

}

答案 1 :(得分:1)

你可以在你的错误控制器中放一个断点,它会按预期停止吗? 如果没有,您可以尝试创建一个异常处理程序,然后将消息转发给您的控制器。

@ExceptionHandler(Exception.class)
public String handleException(final Exception e) {

    return "forward:/serverError";
}

答案 2 :(得分:0)

尝试将前导斜杠符号添加到控制器中的RequestMapping值:

@RequestMapping(value = "/serverError", method = RequestMethod.GET)
public String renderErrorPage(final Model model, HttpServletRequest httpRequest) throws CMSItemNotFoundException {

答案 3 :(得分:0)

@ java.nazif请在web.xml中添加错误代码。

<error-page>
    <error-code>500</error-code>
    <location>/jsps/500Error.jsp</location>
</error-page>