简体   繁体   English

如何处理Filters中抛出的异常?

[英]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.

相关问题 如何管理 Spring 中过滤器抛出的异常? - How to manage exceptions thrown in filters in Spring? 如何处理自定义的HandlerMethodArgumentResolver引发的异常? - How to handle exceptions thrown from customized HandlerMethodArgumentResolver? 如何处理Freemarker中从Java抛出的异常? - How to handle exceptions thrown from Java in Freemarker? 如何处理Struts 2用户定义的AJAX结果中引发的异常? - How to handle exceptions thrown in a Struts 2 user-defined AJAX result? 如何在没有尝试捕获的情况下处理流 map 中引发的异常? - How to handle exceptions thrown in streams map without try catch? 如何处理Spring Security AuthenticationProviders抛出的运行时异常? - How to handle run time exceptions thrown by Spring Security AuthenticationProviders? 如何处理RxJava中观察者的onNext引发的异常? - How to handle exceptions thrown by observer's onNext in RxJava? 如何处理Struts2中默认拦截器引发的异常? - How to handle exceptions thrown by default interceptors in struts2? 如何处理Project Reactor中处理器订阅中抛出的异常 - How to handle exceptions thrown in subscriptions to processors in Project Reactor 如何处理在Spring MVC中呈现视图时抛出的异常? - How to handle exceptions thrown while rendering a view in Spring MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM