不要抛出异常,请尝试以下操作:
@ExceptionHandler( value=ArticleNotFoundException.class )public ResponseEntity<String> handleArticleNotFound() { return new ResponseEntity<String>(HttpStatus.NOT_FOUND);}这基本上将返回一个Spring对象,该对象将由您的控制器转换为404。
如果要将不同的HTTP状态消息返回到前端,可以将其传递给其他HttpStatus。
如果您对使用注解不满意,只需使用@ResponseStatus注释该控制器方法,不要抛出异常。
基本上,如果您使用
@ExceptionHandler90%的方式注释方法,则Spring希望该方法使用该异常而不抛出另一个异常。通过抛出一个不同的异常,Spring认为未处理该异常并且您的异常处理程序失败,因此日志中的消息
编辑:
要使其返回特定页面,请尝试
return new ResponseEntity<String>(location/of/your/page.html, HttpStatus.NOT_FOUND);
编辑2:您应该能够做到这一点:
@ExceptionHandler( value=ArticleNotFoundException.class )public ResponseEntity<String> handleArticleNotFound(HttpServletResponse response) { response.sendRedirect(location/of/your/page); return new ResponseEntity<String>(HttpStatus.NOT_FOUND);}


