URL Matching with PathPattern in Spring MVC
The recent Spring Framework 5.3 M1 release announcement mentions "Spring MVC comes with PathPattern parsing for efficient URL matching". This post expands on that with more context and detail.
Overview
In Spring applications AntPathMatcher is used to identify classpath, file system, remote, and other resources in Spring configuration. It has also been used in Spring MVC to match URL paths. Over time the use of patterns in web applications grew in number and syntax with AntPathMatcher evolving to meet those needs but some pain points remain without a solution:
-
In web applications, patterns need to be matched many times per request and therefore any gains in performance and efficiency matter. However
Stringpattern matching limits what can be achieved. -
Choosing the most specific pattern among several that match a request has proven challenging over the years with no simple ways to make it more predictable without impacting other cases.
-
Matching a
Stringpath to aStringpattern makes it difficult to avoid URI encoding issues. For example should the incoming path be decoded first and then matched? That allows for patterns themselves to be declared without encoded characters, but what if the request path contains%2For%3Bwhich are/and;respectively? Once decoded those alter the structure of the path making it harder to match reliably. We could leave the request path encoded viaUrlPathHelper#urlDecode…