[英]How to handle exceptions thrown in Filters?
I am using Spring 4 and Tomcat. 我使用的是Spring 4和Tomcat。 The issue is sometimes I have to throw a (custom) RuntimeException in my filter (The control has not even reached the controller).
问题是有时我必须在我的过滤器中抛出(自定义)RuntimeException(控件甚至没有到达控制器)。 The issue is since I am not throwing an exception that tomcat understands, it gets converted to 500 (internal server error).
问题是因为我没有抛出tomcat理解的异常,它被转换为500(内部服务器错误)。 I believe a 403 Forbidden would be better than a 500 (For my custom exception).
我相信403 Forbidden会比500更好(对于我的自定义例外)。 I have looked at
@ExceptionHandler
and @ControllerAdvice
annotations. 我查看了
@ExceptionHandler
和@ControllerAdvice
注释。 But these work only if the control reaches the controller. 但这些只有在控制器到达控制器时才起作用。
As of now I am manually setting the status to 403 in the HTTPResponse
in my filter. 截至目前,我在我的过滤器中的
HTTPResponse
中手动将状态设置为403。 Is there a better way of handling this scenario? 有没有更好的方法来处理这种情况?
you should use something like this
你应该使用这样的东西
Setting an error handler in web.xml 在web.xml中设置错误处理程序
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/handleExceptionService</location>
</error-page>
So, when you reach your service, yo can do wathever you want with the error. 所以,当你到达你的服务时,你可以用错误来做你想要的。
Good Luck!!! 祝好运!!!
You can add in web.xml
<error-page>
<error-code>404</error-code>
<location>/error-404.html</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/access_denied.html</location>
</error-page>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.