Springboot中URL的匹配规则

事情发生经过是我有次springboot项目中定义了一个遵循restful规则的接口,

@RequestMapping(value = "/xxxx/{email}", method = RequestMethod.GET)

public void xxxtest(@PathVariable String email) {

    System.out.println(email);

}


这时候访问的时候http://localhst:8080/xxxx/tets@qq.com

输出的email变成了qq。

请求url被“ . “截断了。最终得到的额email的值是qq,而不是qq.com


这里要说道一个类,PatternsRequestCondition类

里面有两个匹配规则,


useSuffixPatternMatch:设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;

这种模式下.后面加任何后缀,都会被匹配到。user.do,user.html等


useTrailingSlashMatch:设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;


private String getMatchingPattern(String pattern, String lookupPath) {

   if (pattern.equals(lookupPath)) {

      return pattern;


}

   if (this.useSuffixPatternMatch) {


if (!this.fileExtensions.isEmpty() && lookupPath.indexOf('.') != -1) {

         for (String extension : this.fileExtensions) {


if (this.pathMatcher.match(pattern + extension, lookupPath)) {

               return pattern + extension;


}

         }

      }

      else {

         boolean hasSuffix = pattern.indexOf('.') != -1;

         if

(!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {

            return pattern + ".*";


}

      }

   }

   if (this.pathMatcher.match(pattern, lookupPath)) {

      return pattern;


}

   if (this.useTrailingSlashMatch) {


if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {

         return pattern +"/";


}

   }

   return null;

}



默认情况下启动程序,这两个值都是默认true,可以通过在主程序入库

public class xxxApplication extends WebMvcConfigurationSupport {

    @Override

    protected void configurePathMatch(PathMatchConfigurer configurer) {

        configurer.setUseSuffixPatternMatch(false);


}


通过重写configurePathMatch方法,设置useSuffixPatternMatch,useTrailingSlashMatch的值来达到修改规则的目的

来自:Springboot中URL的匹配规则

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。