【发布时间】:2014-05-29 10:40:30
【问题描述】:
我已经例外,当我想要一个 404 页面时,我总是抛出:
@ResponseStatus( value = HttpStatus.NOT_FOUND )
public class PageNotFoundException extends RuntimeException {
我想创建控制器范围的 @ExceptionHandler,它将重新抛出 ArticleNotFoundException(导致错误 500)作为我的 404 异常:
@ExceptionHandler( value=ArticleNotFoundException.class )
public void handleArticleNotFound() {
throw new PageNotFoundException();
}
但它不起作用 - 我仍然有 错误 500 和 Spring 日志:
ExceptionHandlerExceptionResolver - Failed to invoke @ExceptionHandler method: ...
请注意,我将代码转换为 html,因此响应不能为空或像 ResponseEntity 那样的简单字符串。 web.xml 入口:
<error-page>
<location>/resources/error-pages/404.html</location>
<error-code>404</error-code>
</error-page>
来自回答评论的最终解决方案
这不是一个完整的重新抛出,但至少它使用了web.xml错误页面映射,就像我的PageNotFoundException一样
@ExceptionHandler( value = ArticleNotFoundException.class )
public void handle( HttpServletResponse response) throws IOException {
response.sendError( HttpServletResponse.SC_NOT_FOUND );
}
【问题讨论】:
标签: java spring exception spring-mvc exception-handling