SpringBoot2.x系列教程26--整合SpringMVC之路径匹配规则

本章节,我会给大家讲解Spring Boot中定制URL匹配规则的方法。

一.URL路径匹配

1.概述

在Spring Boot1.5的版本中,假如我们定义了一个’/show‘接口,默认情况下,我们可以按照/show来访问页面,也可以按照/show.do这样带有“.do”后缀的接口来访问资源。

但是到了Spring Boot2.x之后,我们发现再使用.do的扩展名就无法访问资源了。

也就是说,现在的Spring Boot在默认情况下,禁用了后缀匹配模式!

但是我们在开发Web应用程序时,并不总是使用一些默认的配置。有时,我们要创建包含字符 “.” 的RESTful风格的URL;有时候我们也希望识别斜杠的存在。这些需求,Spring都为我们提供了接口供开发人员按照需求定制。

“.” 字符在Spring中作为分隔符定义格式,例如/projects/spring-boot.json中的 “点” ,另外我们也可能想识别路径尾部的斜杠,如“/home/”中的 “/” 等。

2.案例说明

我们在SpringBoot1.x版本中,可以创建如下控制器接口:

@RestController
@RequestMapping("/")
public class HelloController {
 
    @RequestMapping("hello")
    public String showHello() {
    return "Hello,一一哥Sun!";
    }
}

在Spring Boot1.x版本的控制器中,我们可以使用下面的规则来进行访问:

/hello
/hello.*

但是如果我们将工程升级到SpringBoot2.x后,默认情况下我们只能使用/hello访问,那么怎样才能使用1.x的访问规则呢?

接下来我就带大家在SpringBoot 2.x版本中实现一下,进行自定义的URL路径匹配。

二.自定义URL路径匹配规则

1. 实现方式有2种:

  • 1.代码配置类实现方式;
  • 2.配置文件实现方式。

2.创建一个web项目(略)

三.以代码配置类来实现

1. 实现方式

在这种实现方式里,我们又可以有两种常用的方式实现:

  • 继承抽象类WebMvcConfigurerAdapter,重写configurePathMatch方法;
  • 继承WebMvcConfigurationSupport,重写configurePathMatch方法。

上述两种方式的核心都是:

configurePathMatch(PathMatchConfigurer configurer)函数,
让开发人员可以根据需求定制URL路径的匹配规则。

setUseSuffixPatternMatch(boolean useSuffixPatternMatch)
设置是否是后缀模式匹配,true即匹配。

核心开发步骤就是两步:

(1).启动类 extends WebMvcConfigurationSupport;

(2).重写configurePathMatch方法。

2. 具体实现过程

2.1 创建WebMvcConfigurationSupport配置类。

package com.yyg.boot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/21
 */
@Configuration
public class DefaultMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void configurePathMatch(PathMatchConfigurer configurer) {
        //setUseSuffixPatternMatch:设置是否遵循后缀匹配模式,如“/user”是否匹配/user.*,为true时就匹配;
        configurer.setUseSuffixPatternMatch(true)
                //setUseTrailingSlashMatch,设置是否自动后缀留级匹配模式,如“/user”是否匹配“/user/”,为true是即匹配
                .setUseTrailingSlashMatch(true);
    }

}

2.2 创建Controller接口

package com.yyg.boot.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description URL路径访问规则+内容协商管理
 * @Author 一一哥Sun
 * @Date Created in 2020/3/21
 */
@Controller
public class WelcomeController {

    /**
     * produces="application/json;charset=UTF-8":解决继承WebMvcConfigurationSupport类时中文乱码的问题.
     */
    @ResponseBody
    @GetMapping(value="/show",produces="application/json;charset=UTF-8")
    public String showMsg() {
        return "听一一哥讲解URL路径访问规则...";
    }

}

2.3 启动测试

在浏览器中输入地址:
http://localhost:8080/show.do
可以看到,此时即使我们带有".do"后缀,也可以访问页面,在此之前是不可以的。

在浏览器中输入地址:
http://localhost:8080/show/
可以看到,此时即使我们带有"/show/"后缀,也可以访问页面。

3. 总结

抽象类WebMvcConfigurerAdapter与WebMvcConfigurationSupport都可以配置MVC, WebMvcConfigurerAdapter有的功能,WebMvcConfigurationSupport都有,因此建议使用WebMvcConfigurationSupport。

四.以配置文件实现方式

以上代码配置类的实现方式,其实是有点复杂的,我们也可以把该实现方式,在properties或yml配置文件中实现,这种方式特别的简单。

我们可以先把前面案例中的配置类中的@Configuration先注释掉,使得之前的配置代码失效,我们接下来创建一个application.properties配置文件。

1. 创建application.properties文件

#设置是否遵循后缀匹配模式,如“/user”是否匹配/user.*,为true时就匹配;
spring.mvc.pathmatch.use-suffix-pattern=true
#设置是否自动后缀留级匹配模式,如“/user”是否匹配“/user/”,为true是即匹配
spring.mvc.pathmatch.use-registered-suffix-pattern=false

2. 重启项目测试

在浏览器中输入地址:
http://localhost:8080/show.dooo
可以看到,此时即使我们带有".dooo"后缀,也可以访问页面。

在浏览器中输入地址:
http://localhost:8080/show/
可以看到,此时即使我们带有"/show/"后缀,也可以访问页面。

猜你喜欢

转载自blog.csdn.net/qfchenjunbo/article/details/108220850