根据 URI 规范 RFC 3986 中 URL 的定义,路径片段中可以可以包含键值对。规范中没对对应的术语。一般 “URL 路径参数” 可以被应用,尽管更加独特的 “矩阵 URI” 也经常被使用并且相当有名。在 Spring MVC 它被成为矩阵变量

    矩阵变量可以出现在任何路径片段中,每一个矩阵变量都用分号(;)隔开。比如 “/cars;color=red;year=2012”。多个值可以用逗号隔开,比如 “color=red,green,blue”,或者分开写 “color=red;color=green;color=blue”。

    如果你希望一个 URL 包含矩阵变量,那么请求映射模式必须用 URI 模板来表示这些矩阵变量。这样的话,不管矩阵变量顺序如何,都能够保证请求可以正确的匹配。

Springboot 默认是无法使用矩阵变量绑定参数的。需要覆盖WebMvcConfigurer中的configurePathMatch方法。

@Configuration
public class WebConfig implements WebMvcConfigurer { @Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}

基于XML的配置

<mvc:annotation-driven enable-matrix-variables="true" />

编写矩阵变量控制器

package com.techmap.examples.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/matrix")
public class MatrixController
{ /**
* 使用矩阵变量
*/
@GetMapping("/owners/{ownerId}/pets/{petId}")
public String findPet(
@PathVariable String ownerId,
@PathVariable String petId,
@MatrixVariable(name = "q", pathVar = "ownerId") int q1,
@MatrixVariable(name = "q", pathVar = "petId") int q2)
{
System.out.println("--> ownerId : " + ownerId);
System.out.println("--> petId : " + petId);
System.out.println("--> q1 : " + q1);
System.out.println("--> q2 : " + q2); return "/examples/targets/test1";
} /**
* 矩阵变量可以设置默认值
*/
@GetMapping("/pets/{petId}")
public String findPet(
@MatrixVariable(required = false, defaultValue = "1") int q)
{
System.out.println("--> Default value of q : " + q); return "/examples/targets/test2";
}
}

Springboot使用MatrixVariable 注解的更多相关文章

  1. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  2. SpringBoot 中常用注解

    本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

  3. SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍

    SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍 本篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@Requ ...

  4. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  5. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  6. SpringBoot使用Mybatis注解进行一对多和多对多查询(2)

    SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...

  7. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  8. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  9. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...

随机推荐

  1. ubuntu平台下,字符集的转换命令iconv

    iconv命令格式 iconv -f 源字符集(要转换文件的字符集) -t 目标字符集  file iconv -f gb18030 -t utf-8 file 默认情况下,不改变原文件,输出到屏幕. ...

  2. 基于 Blazor 打造一款实时字幕

    早先在录制视频的时候一直使用的是 obs-auto-subtitle 作为实时字幕展示功能.不过这个是以 OBS 插件的形式存在,不管是语言和功能上都有一定的限制.故而使用 Blazor server ...

  3. Idea快捷键 累积大全

    分类 Editing 这个 Searching/Replcae Navigation Atl +1                                           打开和关闭左侧p ...

  4. vue 使用 lang="scss" 报错

    npm install sass-loader@7.3.1 --save-dev npm install node-sass --save npm install style-loader --sav ...

  5. shiro反序列化550、721

    shiro550反序列化 获取docker镜像 docker pull medicean/vulapps:s_shiro_1 重启docker systemctl restart docker 启动d ...

  6. 创建函数,传递一个数字n,返回斐波那契数列的第n的值。

    斐波那契数列 第1项和第2项的值是1,从第3项开始,每项的值是前两项相加的和 1   1   2    3    5    8    13    21...... 法1: function fn(n) ...

  7. 使用C#winform编写渗透测试工具--Web指纹识别

    使用C#winform编写渗透测试工具--web指纹识别 本篇文章主要介绍使用C#winform编写渗透测试工具--Web指纹识别.在渗透测试中,web指纹识别是信息收集关键的一步,通常是使用各种工具 ...

  8. Windows内核开发-5-(2)-内核模式调试

    Windows内核开发-5-(2)-内核模式调试 普通用户模式的调试,采取的是给进程添加一个线程来挂起断点,作为一个调试器的线程在进程中使用.照这样来类推,对操作系统调试相当于添加一个进程来限制操作系 ...

  9. 离线webpack创建vue 项目

    参考地址: https://blog.csdn.net/feinifi/article/details/104578546 画重点: // 需要带上参数--offline表示离线初始化. --offl ...

  10. pwnable.kr之simple Login

    pwnable.kr之simple Login 懒了几天,一边看malloc.c的源码,一边看华庭的PDF.今天佛系做题,到pwnable.kr上打开了simple Login这道题,但是这道题个人觉 ...