在Spring @ExceptionHandler中如何将异常作为@ ResponseStatus-annotated异常重新抛出? [英] How to rethrow exception as @ResponseStatus-annotated exception in Spring @ExceptionHandler?

查看:1696
本文介绍了在Spring @ExceptionHandler中如何将异常作为@ ResponseStatus-annotated异常重新抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有例外,当我想要一个404页面时总是会抛出:

I have made exception, that I'm throwing always when I want a 404 page:

@ResponseStatus( value = HttpStatus.NOT_FOUND )
public class PageNotFoundException extends RuntimeException {

我想创建控制器范围 @ExceptionHandler 将重新抛出 ArticleNotFoundException (导致错误500)作为我的404异常:

I want to create controller-wide @ExceptionHandler that will re-throw ArticleNotFoundException(which causes error 500) as my 404 exception:

@ExceptionHandler( value=ArticleNotFoundException.class )
public void handleArticleNotFound() {
    throw new PageNotFoundException();
}

但它不起作用 - 我还有错误500 和Spring日志:

But it doesn't work - I still have error 500 and Spring logs:

ExceptionHandlerExceptionResolver - 无法调用@ExceptionHandler方法:...

请注意,我将代码翻译成html,因此响应不能为空或简单字符串,如 ResponseEntity web.xml 条目:

Note that I translate code to html so response cannot be empty or simple String like with ResponseEntity. web.xml entry:

<error-page>
    <location>/resources/error-pages/404.html</location>
    <error-code>404</error-code>
</error-page>

来自ANSWER COMMENTS的最终解决方案

它不是一个完整的重新抛出,但至少它使用 web.xml 错误页面映射,像我的 PageNotFoundException

It's not a full re-throw, but at least it uses web.xml error page mapping like my PageNotFoundException

    @ExceptionHandler( value = ArticleNotFoundException.class )
    public void handle( HttpServletResponse response) throws IOException {
        response.sendError( HttpServletResponse.SC_NOT_FOUND );

    }


推荐答案

抛出异常尝试这样:

@ExceptionHandler( value=ArticleNotFoundException.class )
public ResponseEntity<String> handleArticleNotFound() {
    return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}

这将基本上返回一个Spring对象,由控制器转换为404 。

This will basically return a Spring object that gets converted into a 404 by your controller.

如果您要将不同的HTTP状态消息返回到前端,您可以传递不同的HttpStatus。

You can pass it a different HttpStatus if you want to return different HTTP status messages to your front-end.

如果您在使用注释执行此操作时无效,请使用@ResponseStatus注释该控制器方法,并且不会引发异常。

If you are deadset on doing this with annotations, just annotate that controller method with @ResponseStatus and don't throw an exception.

基本上如果您注释方法使用 @ExceptionHandler 我有90%的确定,Spring希望该方法可以使用该异常,而不会抛出另一个异常。通过抛出一个不同的异常,Spring认为异常没有被处理,你的异常处理程序失败,因此日志中的消息

Basically if you annotate a method with @ExceptionHandler I'm 90% sure that Spring expects that method to consume that exception and not throw another one. By throwing a different exception, Spring thinks that the exception wasn't handled and that your exception handler failed, hence the message in your logs

编辑:

要使其返回到特定页面尝试

To get it to return to a specific page try

return new ResponseEntity<String>(location/of/your/page.html, HttpStatus.NOT_FOUND);

编辑2:
您应该可以这样做:

EDIT 2: You should be able to do this:

@ExceptionHandler( value=ArticleNotFoundException.class )
public ResponseEntity<String> handleArticleNotFound(HttpServletResponse response) {
    response.sendRedirect(location/of/your/page);
    return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}

这篇关于在Spring @ExceptionHandler中如何将异常作为@ ResponseStatus-annotated异常重新抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆