Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)

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);

Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)_第1张图片
我们还可以在外部配置application.yml快捷配置与同样的效果:

spring.mvc.pathmatch.use-suffix-pattern: true //Spring Boot默认是false
3.9.2 斜线匹配

使用PathMatchConfigurer.setUseTrailingSlashMatch(Boolean trailingSlashMatch)设置是否使用尾随斜线匹配。若设置为true,则路径/xx/xx/等效,Spring MVC下默认是开启的,需关闭设置为false

configurer.setUseTrailingSlashMatch(false);

截图演示的效果是默认情况下的效果:
Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)_第2张图片

3.9.3 路径前缀

可以通过PathMatchConfigurer.addPathPrefix(String prefix, Predicate> predicate)来设置路径前缀。

prefix设置路径的前缀,predicate设置匹配起效的控制器类型,本例为对@RestController有效:

configurer.addPathPrefix("/api", HandlerTypePredicate.forAnnotation(RestController.class));

Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)_第3张图片

3.9.4 内容协商

所谓“内容协商”指的是请求时使用不同的条件可获得不同的返回体的类型,如json,xml等。

在Spring Boot下我们可以在外部配置通过,实现根据“路径+.+扩展名”获得不同类型的返回体。

spring.mvc.contentnegotiation.favor-path-extension: true //偏好使用路径扩展名,如content.json
spring.mvc.pathmatch.use-registered-suffix-pattern: true //后缀只能是已注册扩展名,content.xx无效

Spring MVC已经为我们注册了媒体类型json;添加xml的支持jar到build.gradle

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-web'
   implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'
}

我们用控制器验证:

@GetMapping("/content")
public AnotherPerson content(@RequestBody AnotherPerson person){
    return person;
}

Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)_第4张图片

3.9.5 添加新的媒体类型

媒体类型的功能都是由HttpMessageConverter提供的,我们将我没钱前面注册的AnotherPersonHttpMessageConverter支持的媒体类型注册到内容协商。

我们可以通过重载WebMvcConfigurer接口的configureContentNegotiation方法,并使用Spring MVC提供的ContentNegotiationConfigurer来配置内容协商。

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("ap",
            new MediaType("application","another-person", Charset.defaultCharset()));
}

或者通过外部配置来配置:

spring.mvc.contentnegotiation.media-types.ap: application/another-person

spring.mvc.contentnegotiation.media-types后支持一个Map类型,ap是key,application/another-personMediaType类型的value。

我们访问http://localhost:8080/people/content.ap,会使用AnotherPersonHttpMessageConverter的处理来生成返回体。
Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)_第5张图片

新书推荐:

我的新书《从企业级开发到云原生微服务:Spring Boot 实战》已出版,内容涵盖了丰富Spring Boot开发的相关知识
购买地址:https://item.jd.com/12760084.html

Spring Boot 2.x实战45 - Spring Web MVC 17 - Web MVC配置(路径匹配和内容协商-PathMatchConfigurer)_第6张图片
主要包含目录有:

第一章 初识Spring Boot(快速领略Spring Boot的美丽)
第二章 开发必备工具(对常用开发工具进行介绍:包含IntelliJ IDEA、Gradle、Lombok、Docker等)
第三章 函数式编程
第四章 Spring 5.x基础(以Spring 5.2.x为基础)
第五章 深入Spring Boot(以Spring Boot 2.2.x为基础)
第六章 Spring Web MVC
第七章 数据访问(包含Spring Data JPA、Spring Data Elasticsearch和数据缓存)
第八章 安全控制(包含Spring Security和OAuth2)
第九章 响应式编程(包含Project Reactor、Spring WebFlux、Reactive NoSQL、R2DBC、Reactive Spring Security)
第十章 事件驱动(包含JMS、RabbitMQ、Kafka、Websocket、RSocket)
第11章 系统集成和屁股里(包含Spring Integration和Spring Batch)
第12章 Spring Cloud与微服务
第13章 Kubernetes与微服务(包含Kubernetes、Helm、Jenkins、Istio)
多谢大家支持。

你可能感兴趣的:(Spring,Boot2.x实战全集,Spring,Boot2.x实战,-,Spring,MVC,spring,boot,mvc,mvc,config,路径匹配和内容协商,PathMatch)