Friday, 1 January 2016

Difference between "/*" and "/**" in Spring MVC paths


There are many places in Spring configuration where we need to provide path. It might be a mapping or a resource path. For example an interceptor mapping - 

        <mvc:mapping path="/secure/**" />
        <mvc:exclude-mapping path="/secure/help" /> 
        <bean class="" />

OR Servlet mapping in web.xml


or even your resource chain

<mvc:resources mapping="/css/**" location="/css/">
    <mvc:resource-chain resource-cache="true" auto-registration="true">
                <mvc:content-version-strategy patterns="/**"/>

In all these place you can use  "/*" or "/**". But the million dollar question is what does each mean and when do we use what?

Difference between "/*" and "/**" in Spring MVC paths

  • An asterisk ('*') matches zero or more characters, up to the occurrence of a '/' character (which serves as a path separator). A string, such as "/abcd/docs/index.html", would not match successfully against the pattern '/*/*.index.html'. The first asterisk matches up to the first path separator only, resulting in the "abcd" string. A successful matching pattern would be '/*/*/*.html'.
  • A string containing two asterisks ('**') matches zero or more characters. This could include the path separator '/'. In this case, "/abcd/docs/index.html" would successfully match the '/**/*.html' pattern. The double asterisk, including the path separator, would match the "abcd/docs" string.

So to sum up "/**" takes into account even the subpaths that may include the "/" i.e the path separator.


This is a path pattern that used in Apache ant, spring team implement it and use it throughout the framework.

The mapping matches URLs using the following rules:

  • ? matches one character
  • * matches zero or more characters
  • ** matches zero or more 'directories' in a path
Some examples:

  • com/t?st.jsp - matches com/test.jsp but also com/tast.jsp or com/txst.jsp
  • com/*.jsp - matches all .jsp files in the com directory
  • com/**/test.jsp - matches all test.jsp files underneath the com path
  • org/springframework/**/*.jsp - matches all .jsp files underneath the org/springframework path
  • org/**/servlet/bla.jsp - matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp


Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top