3.9 路径匹配和内容协商
Spring MVC中可以通过重载WebMvcConfigurer
接口的configurePathMatch
方法来设置路径匹配。Spring MVC为我们提供了PathMatchConfigurer
来进行路径匹配配置。
public void configurePathMatch(PathMatchConfigurer configurer) {
}
3.9.1 后缀匹配
使用PathMatchConfigurer.setUseSuffixPatternMatch(Boolean suffixPatternMatch)
设置是否使用后缀匹配。若设置为true
则路径/xx
和/xx.*
是等效的,Spring Boot下默认是false
。
configurer.setUseSuffixPatternMatch(true);
我们还可以在外部配置application.yml
快捷配置与同样的效果:
spring.mvc.pathmatch.use-suffix-pattern: true //Spring Boot默认是false
3.9.2 斜线匹配
使用PathMatchConfigurer.setUseTrailingSlashMatch(Boolean trailingSlashMatch)