SpringBoot魔法堂:@MatrixVariable参数注解使用详解
前言
RFC3986定义URI的路径(Path)中可包含name-value片段,扩充了以往仅能通过查询字符串(Query String)设置可选参数的囧境。
假如现在需要设计一个用于“搜索某部门某些员工可选信息中的部分信息”的API,我们分别使用查询字符串和路径name-value方式来设计对比,看看具体效果:
- 查询字符串方式:
/api/v1/users/optional-info?dept=321&name=joh*&fields=hometown,birth
问题:其中的dept和name理应属于users路径,而fields则属于optional-info路径,但现在全部都要挤在查询字符串中。 - 路径name-value方式:
/api/v1/users/depts=321;name=joh*/optional-fields/fields=hometown,birth
可以看出路径name-value的方式逻辑上更在理些。
@MatrixVariable注解属性说明
在正式开始前我们先死记硬背一下注解的属性吧。
value和属性pathVar的别名;pathVar用于指定name-value参数所在的路径片段名称name用于指定name-value参数的参数名required是否为必填值,默认为falsedefaultValue设置默认值
其中pathVar和name到底是什么呢?请继续看后面的示例吧,准能秒懂!
启用@MatrixVariable
虽然从Spring 3.2就已经支持@MatrixVariable特性,但直至现在其依然为默认禁用的状态。我们需要手工配置开启才能使用。
@Configuration
public class SpringBootConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
参数仅有一个值的玩法
注意:多个name-value间以分号分隔,如name=joh*;dept=321。
/*
1. 获取单个路径片段中的参数
请求URI为 /Demo2/66;color=red;year=2020
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@PathVariable String id, @MatrixVariable String color, @MatrixVariable String year){}
/*
2. 获取单个路径片段中的参数
请求URI为 /Demo2/color=red;year=2020
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test2(@MatrixVariable String color, @MatrixVariable String year){}
/*
3. 获取不同路径片段中的参数
请求URI为 /Demo2/66;color=red;year=2020/pets/77;color=blue;year=2019
*/
@RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET)
public String test3(@PathVariable String id1, @PathVariable String id2,
@MatrixVariable(name="color", pathVar="id1") String color1, @MatrixVariable(name="year", pathVar="id1") String year1,
@MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){}
/*
4. 获取不同路径片段中的参数
请求URI为 /Demo2/color=red;year=2020/pets/77;color=blue;year=2019
*/
@RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET)
public String test4(@PathVariable String id2,
@MatrixVariable(name="color", pathVar="id1") String color1, @MatrixVariable(name="year", pathVar="id1") String year1,
@MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){}
/*
5. 通过Map获取所有或指定路径下的所有参数
*/
@RequestMapping(path="/Demo3/{id1}/pets/{id2}", method=RequestMethod.GET)
public String test5(@MatrixVariable Map<String, Object> all, @MatrixVariable(pathVar="id1") Map<String, Object> mapId1) {}
参数有多个值的玩法
若参数值不是单个,那么可以通过两种方式传递:
- 值之间通过逗号分隔,如
dept=321,123 - 重名name-value对,如
dept=321;dept=123
/*
请求为/Demo1/color=123,321
那么color值为123,321
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@MatrixVariable Integer[] color){}
/*
请求为/Demo1/color=123;color=321
那么color值为123,321
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@MatrixVariable Integer[] color){}
那些要注意的坑
在参数多值的情况下还有如下3个坑,请各位多加注意:
String参数类型可以接受通过逗号和通过重名name-value传递的所有值,而其它类型只能获取第一个值。
/*
请求为/Demo1/color=123,321
那么color值为123,321
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@MatrixVariable String color){}
/*
请求为/Demo1/color=123;color=321
那么color值为123,321
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@MatrixVariable String color){}
/*
请求为/Demo1/color=123;color=321
那么color值为123
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@MatrixVariable Integer color){}
Map<String, Object[]>只能获取参数中的第一个值而已。
/*
请求为/Demo1/color=123,321
那么color值为123
*/
@RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)
public String test1(@MatrixVariable Map<String, Integer[]> color){}
- 不同路径片段中出现名称相同的参数,那么必须通过pathVar标识所有相同参数所属路径,否则URI匹配失败。
// 以下handler仅标识第二个参数的pathVar,而没有标识第一个,那么也是会匹配失败的。
@RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET)
public String test2(@MatrixVariable String color, @MatrixVariable(name="color", pathVar="id2") String color2){}
总结
今天就写到这里吧,后续会有更多Spring Boot的分享,请大家多关注我哦!
转载请注明来自: https://www.cnblogs.com/fsjohnhuang/p/14284988.html —— 肥仔John
SpringBoot魔法堂:@MatrixVariable参数注解使用详解的更多相关文章
- JS魔法堂:LINK元素深入详解
一.前言 我们一般使用方式为 <link type="text/css" rel="stylesheet" href="text.css&quo ...
- .Net魔法堂:AssemblyInfo.cs文件详解
一.前言 .net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件.但我们实际上通过另一个形式操作该文件.那就是通过在鼠标右键点 ...
- Java 注解用法详解——@SuppressWarnings
转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解——@SuppressWarnings 一.前言 编码时我 ...
- SpringBoot系列(六)集成thymeleaf详解版
SpringBoot系列(六)集成thymeleaf详解版 1. thymeleaf简介 1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎. 2. Thymeleaf ...
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- Mybatis----传入参数parameterType类型详解
Mybatis----传入参数parameterType类型详解 前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType ...
- Spring 注解@Value详解
一.spring(基础10) 注解@Value详解[1] 一 配置方式 @value需要参数,这里参数可以是两种形式: [html] view plain copy @Value("#{co ...
- Java注解(Annotation)详解
转: Java注解(Annotation)详解 幻海流心 2018.05.23 15:20 字数 1775 阅读 380评论 0喜欢 1 Java注解(Annotation)详解 1.Annotati ...
- SpringBoot之Spring@Value属性注入使用详解
在使用Spring框架的项目中,@Value是使用比较频繁的注解之一,它的作用是将配置文件中key对应的值赋值给它标注的属性.在日常使用中我们常用的功能都比较简单,本篇文章系统的带大家来了解一下@Va ...
随机推荐
- 基于gin的golang web开发:实现用户登录
前文分别介绍过了Resty和gin-jwt两个包,Resty是一个HTTP和REST客户端,gin-jwt是一个实现了JWT的Gin中间件.本文将使用这两个包来实现一个简单的用户登录功能. 环境准备 ...
- STL—— 容器(vector)begin() 与 rbegin() , end() 与 rend()
1. Vector 迭代器首地址与尾地址 begin() 和 end() 在代码中可以将迭代器用作参数的位置可以使用 begin() 和 end() 获取地址,如下代码: 1 #include &l ...
- 服务启动Apache服务,错误Parent: child process exited with status 3 -- Aborting.解决
不能启动apache,或者使用wamp等集成包后,唯独apache服务启动后有停止,但是把东西搬到其他机器上却没事问题可能和网络有关,我查了很多资料首先找打apache的错误报告日志,发现现实诸多的调 ...
- Day4 dict和set
dict -- dictionary 一组key的集合,包含key与value的对应. Python内置的字典,在其他语言中称为map,使用key-value存储,具有极快的查找 ...
- centos 7 配置 mysql 5.7 主从复制
centos 7 配置 mysql 5.7 主从复制 主库:192.168.12.3 从库:192.168.12.2 1. 主库从库所在服务器关闭防火墙Systemctl stop firewalld ...
- emca配置EM
EM DC(Enterprise Manager Database Control)是 web 界面的数据库管理工具, 可用于配置 EM DC环境的工具包括: Oracle Universal In ...
- day109:MoFang:好友列表显示&添加好友页面初始化&添加好友后端接口
目录 1.好友列表 2.添加好友-前端 3.服务端提供添加好友的后端接口 1.好友列表 1.在用户中心页面添加好友列表点击入口 html/user.html,用户中心添加好友列表点击入口,代码: &l ...
- 测开之数据类型· 第3篇《列表推导式、字典推导式、2种方式创建生成器》
坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:oschina.云+社区.知乎等各大平台都有. 目录 一.列表推导式 二.字典推导式 三.2种方式创建生成器 1.生成器表达式 2.函数里面,通过 y ...
- C语言中++*x和*++x的区别
++跟*的优先级一样,如果两个同时出现,运算是从右往左(不是常规的从左往右),所以: ++*x即++(*x),先取x的值,然后让值自加1:(地址没变,指针指向的值变了.搞不懂的话自己用快递做例子) * ...
- eclipse中字体、XML字体设置
[修改编辑区中文字体大小]: Window > Preferences > General > Appearance > Colors and Fonts > Java ...