Useful utility classes for Spring MVC


Useful utility classes for Spring MVC


To use spring-mvc-utils simply add the following to your pom.xml file:


URI matching Handler Interceptors

To ensure your Spring MVC handler interceptors only run against specific URLs, you can decorate your handler interceptors with annotations to ensure they're only called when needed.

For example,:

@Includes({ "/include", "/include/*", "*.html" })
@Excludes({ "/exclude", "/exclude/*", "*.jsp" })
public class UriMatchingHandlerInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // TODO Auto-generated method stub
        return super.preHandle(request, response, handler);




The @Includes annotation above ensures that the list of patterns of URIs must be included; all files are included when the annotation is omitted. Similarly, the @Excludes annotation ensures the list of patterns of URIs must be excluded; no files are excluded when the annotation is omitted.

To enable this feature you must use the uri-matching-annotation-driven is used in your Spring configuration, like this:

<beans xmlns=""

    <mvc-util:uri-matching-annotation-driven />

    <!-- Override default handler mapping -->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
       <property name="interceptors">
                <ref bean="exampleInterceptor" />

    <bean id="exampleInterceptor" class="com.github.spring.mvc.util.handler.ExampleHandlerInterceptor" />



The spring-mvc-utils is an open source project licensed under the Apache License 2.0.