春季过滤器异常处理 [英] exception handling for filter in spring

查看:135
本文介绍了春季过滤器异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用@ExceptionHandler在spring中处理异常。使用@ExceptionHandler注释的方法捕获控制器抛出的任何异常,并采取相应的操作。为了避免为每个控制器写@exceptionHandler,我使用@ControllerAdvice注释。



一切正常工作正常。



现在我有一个过滤器(是的,不是拦截器,以处理某些要求),这是使用DelegatingFilterProxy和ContextLoaderListener实现的。



当我从上面的过滤器中抛出相同的异常时,它没有捕捉到在控制器案例中完成的方式。



$ b

这里有什么问题?

解决方案

p>过滤器发生在控制器均匀解决之前,因此从过滤器抛出的异常不能被控制器建议捕获。



过滤器是servlet的一部分,而不是真正的MVC堆栈。


I am handling exceptions in spring using @ExceptionHandler. Any exception thrown by controller is caught using method annotated with @ExceptionHandler and action is taken accordingly. To avoid writing @exceptionHandler for every controller i am using @ControllerAdvice annotation.

Everything works fine as expected.

Now i have a filter(Yes, not interceptor, to handle certain requirement) which is implemented using DelegatingFilterProxy and ContextLoaderListener.

When i am throwing the same exception from above filter, its not caught the way it was done in controller case. It is directly thrown to user.

What's wrong in here?

解决方案

Filters happens before controllers are even resolved so exceptions thrown from filters can't be caught by a Controller Advice.

Filters are a part of the servlet and not really the MVC stack.

这篇关于春季过滤器异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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