0

In Spring Boot 2.6.7, I want to set up all of my controllers' URLs to be case-insensitive.

I tried this but it does not work

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}

Can we configure it through application.properties file?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
AhmadRaza
  • 5
  • 6
  • 1
    Does this answer your question? [How can I have case insensitive URLS in Spring MVC with annotated mappings](https://stackoverflow.com/questions/4150039/how-can-i-have-case-insensitive-urls-in-spring-mvc-with-annotated-mappings) – halil ibrahim binol May 10 '22 at 11:37
  • Please provide enough code so others can better understand or reproduce the problem. – Community May 10 '22 at 11:41
  • You want case **in**sensitive but you call matcher.setCaseSensitive(**true**)? – k314159 May 10 '22 at 11:48
  • matcher.setCaseSensitive(true) typo mistake. I tried false also and also implement "WebMvcConfigurer" interface but these are not working – AhmadRaza May 10 '22 at 12:51

3 Answers3

3

After @Xiidref solution could you add below.

(If you are using spring-boot, just add this in your property and it will work)

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

in your application.properties file.

Easwaran
  • 18
  • 5
0

According to the documentation AntPathMatcher Documentation the boolean of the setCaseSensitive should be false to get a case insensitive behaviour.

So replace

matcher.setCaseSensitive(true);

By

matcher.setCaseSensitive(false);
Xiidref
  • 1,456
  • 8
  • 20
  • not work and this class "WebMvcConfigurerAdapter" is also deprecated in springboot 2.6.7 – AhmadRaza May 10 '22 at 11:58
  • implement "WebMvcConfigurer" interface instead of "WebMvcConfigurerAdapter" class. @AhmadRaza – Shahrukh May 10 '22 at 12:18
  • `` @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { AntPathMatcher matcher = new AntPathMatcher(); matcher.setCaseSensitive(false); configurer.setPathMatcher(matcher); } } `` @Shahrukh this also not work – AhmadRaza May 10 '22 at 12:41
0

Since SpringBoot 2.6.x (Spring 5.3.x), PathPatternParser becomes the default pattern implement instead of AntPathMatcher.

So since SpringBoot 2.6.x (Spring 5.3.x):

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        var paser = new PathPatternParser();
        paser.setCaseSensitive(false);
        configurer.setPatternParser(paser);
    }
}

In SpringBoot 2.5.x (Spring 5.2.x) and earlier:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}
Keillion
  • 19
  • 4