开发者

Can I have a catch all not matched requests using Spring DispatcherServlet

开发者 https://www.devze.com 2022-12-25 15:39 出处:网络
I am using theSpring MVC for a project, and I am using the Spri开发者_运维问答ng DispatcherServlet to map the request\'s coming into the application to the controllers written elsewhere. I am looking

I am using the Spring MVC for a project, and I am using the Spri开发者_运维问答ng DispatcherServlet to map the request's coming into the application to the controllers written elsewhere. I am looking for a way to have a default handler ( a catch all handler) if the request doesn't map to any of the exisiting controller-view maps. This currently shows a Resource not found exception, but I want to know if a catch all unmatched requests function is available in the Spring.


Every HandlerMapping strategy in Spring MVC has a defaultHandler property for just this purpose.

This is easy if your Spring config is already specifying a HandlerMapping object explicitly (e.g. a SimpleUrlHandlerMapping), but it's less obvious if you're relying on the defaults to supply a HandlerMapping for you.

For example, if you're using annotated controllers, then you're probably relaying on the default declaration of DefaultAnnotationHandlerMapping which Spring supplies automatically. However, you can provide your own bean to override the default:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="defaultHandler" ref="myDefaultHandler"/>
</bean>

This same pattern will work with any Handlermapping type, just substitute the class name.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号