Yi EungJun opened SPR-13286 and commented
I want @RequestMapping
annotations to match paths in case-insensitive manner so that @RequestMapping("/path/to/{name}")
matches not only /path/to/me
but also /path/To/me
, /PATH/to/me
, and so on.
There is a known workaround to do that as follows:
public class CaseInsensitivePathMatcher extends AntPathMatcher {
@Override
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);
}
}
But it makes the values of URI template variables lowercase. I want to keep the case information.
Referenced from: commits https://github.com/spring-projects/spring-framework/commit/291550a4843edbecd371e6f040c3b154dac6189f
Comment From: spring-projects-issues
Yi EungJun commented
I'm trying to fix this myself. At the first glance, this issue can be solved by making AntPathMatcher matches patterns in case-insensitive manner as follows:
diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
index 3132b1c..5bee271 100644
--- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
+++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
@@ -583,7 +583,7 @@ public class AntPathMatcher implements PathMatcher {
end = m.end();
}
patternBuilder.append(quote(pattern, end, pattern.length()));
- this.pattern = Pattern.compile(patternBuilder.toString());
+ this.pattern = Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE);
}
private String quote(String s, int start, int end) {
But I am afraid that it causes a lot of side-effects because RequestMapping is not the only client which uses AntPathMatcher. I guess the side effects may be avoided by adding a flag for case-insensitive matching to AntPathmathcer and RequestMapping.
Is there anything more should be considered?
Comment From: spring-projects-issues
Juergen Hoeller commented
I've introduced a public setCaseInsensitive
method on AntPathMatcher
, allowing you to configure this externally. The default is still false
, so there's no backwards compatibility issue. Would this make sense for your purposes?
Juergen
Comment From: spring-projects-issues
Yi EungJun commented
It sounds great! I think it would make sense to me.
Is it possible to configure that AntPathMatcher matches in case-insensitive manner only for a specific controller? And can I try the your new AntPathMatcher?
Comment From: spring-projects-issues
Juergen Hoeller commented
I've turned this around into a setCaseSensitive
method, defaulting to true
, which can be switched to false
for case-insensitive matching. This will be available in the next 4.2.0.BUILD-SNAPSHOT
in about an hour.
I'm afraid the only way to customize the PathMatcher
is at the HandlerMapping
level, e.g. through the <mvc:annotation-driven path-matcher="..."/>
attribute.
Juergen
Comment From: spring-projects-issues
Yi EungJun commented
I tested it and it works very well for me!
Thanks very much, Juergen.
Here is my configuration:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
AntPathMatcher matcher = new AntPathMatcher();
matcher.setCaseSensitive(false);
configurer.setPathMatcher(matcher);
}
}
Comment From: kcsurapaneni
is there any way we can accomplish the same using applications.(properties/yaml)
file?
Comment From: bclozel
@kcsurapaneni that's really a question for the Spring Boot project. It doesn't look like it's possible at the moment.