@RequestAttribute

它只能使用在方法入参上,从request请求参数中获取对应的属性值。

//路径跳转
@GetMapping("/goto")
public String goToPage(HttpServletRequest request){ request.setAttribute("msg","成功了...");
return "forward:/success"; //转发到 /success请求
}
  @ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute(value = "msg",required = false) String msg,
HttpServletRequest request){
Map<String,Object> map = new HashMap<>();
//HttpServletRequest方式获取请求参数
Object msg1 = request.getAttribute("msg");
map.put("msg1",msg1);
//@RequestAttribute注解方式获取请求参数
map.put("msg",msg);
return map;
}

请求参数:http://localhost:8080/goto

结果:{"msg":"成功了...","msg1":"成功了..."}

@MatrixVariable

用于获取路径中参数的属性

假如现在需要设计一个用于“搜索某部门某些员工可选信息中的部分信息”的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的方式逻辑上更在理些。

使用方法

多个变量可以使用;分隔,例如:

/cars;color=red;year=2012

如果是一个变量的多个值那么可以使用,分隔

color=red,green,blu

或者可以使用重复的变量名:

color=red;color=green;color=blue

属性

  • value :属性pathVar的别名;
  • pathVar :用于指定name-value参数所在的路径片段名称
  • name :用于指定name-value参数的参数名
  • required :是否为必填值,默认为false
  • defaultValue :设置默认值

举例

/*
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) {}

@RequestAttribute与@MatrixVariable的更多相关文章

  1. Spring @RequestAttribute

    @RequestAttribute注解用法 @RequestAttribute用在方法入参上,作用:从request中取对应的值,至于request中是怎么存在该属性的,方式多种多样,拦截器中预存.M ...

  2. 从原理层面掌握@RequestAttribute、@SessionAttribute的使用【一起学Spring MVC】

    每篇一句 改我们就改得:取其精华,去其糟粕.否则木有意义 前言 如果说知道@SessionAttributes这个注解的人已经很少了,那么不需要统计我就可以确定的说:知道@RequestAttribu ...

  3. @MatrixVariable的使用

    @MatrixVariable的使用 博客分类: J2EE   在Spring3.2 后,一个@MatrixVariable出现了,这个注解的出现拓展了URL请求地址的功能. Matrix Varia ...

  4. SpringBoot魔法堂:@MatrixVariable参数注解使用详解

    前言 RFC3986定义URI的路径(Path)中可包含name-value片段,扩充了以往仅能通过查询字符串(Query String)设置可选参数的囧境. 假如现在需要设计一个用于"搜索 ...

  5. Springboot使用MatrixVariable 注解

        根据 URI 规范 RFC 3986 中 URL 的定义,路径片段中可以可以包含键值对.规范中没对对应的术语.一般 "URL 路径参数" 可以被应用,尽管更加独特的 &qu ...

  6. springboot开启矩阵传参MatrixVariable

    /** *   根据 URI 规范 RFC 3986 中 URL 的定义,路径片段中可以可以包含键值对. * 规范中没对对应的术语.一般 "URL 路径参数" 可以被应用,尽管更加 ...

  7. SpringBoot中请求参数 @MatrixVariable 矩阵变量

    一.矩阵变量请求格式 /users;id=1,uname=jack 二.SpringBoot开启矩阵请求 首先查看springboot源码关于矩阵部分的内容 在 WebMvcAutoConfigura ...

  8. Spring MVC 学习总结(三)——请求处理方法Action详解

    Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果. 一.Action参数类型 如果 ...

  9. Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解

    一.控制器定义 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求并将其转换为一个模型.在Spring MVC中一个控制器可以包含多个Action(动作. ...

随机推荐

  1. python并发与futures模块

    非并发程序(用于对比) 从网上下载20个国家的国旗图像: import os import time import sys import requests # 导入requests库 POP20_CC ...

  2. python实现查找图片相同的id及重复个数

    import os #os:操作系统相关的信息模块 import random #导入随机函数 #存放原始图片地址 data_base_dir = r"C:\Users\Administra ...

  3. linux mint17.3+vmware 12.1.1 流畅安装运行OSX EI capitan

    在linux mint17.3的vmware虚拟机中安装mac osx ei capitan系统 出于对苹果操作系统的好奇与喜爱,分别在宿主机操作系统为windows 7和linux mint17.3 ...

  4. 计算机python二级 第六套

    第一模块  基本操作 1. random.seed(100)   随机种子  就是100 2.import  random 3.https://www.runoob.com/python3/pytho ...

  5. 【Vue】淘气三千问之 data为什么是函数而不是对象?这河狸吗

    朋友,当你提出以上问题的时候建议你先去复习下原型链的知识 但是我好人做到底直接就讲了吧,我们先看一下下面的这段代码: function Component () { this.data = this. ...

  6. idea使用gitee的小坑

    1. 账号配置 账号配置登陆时提示 *** is not a valid login name: Email support only. 翻译:只能支持邮箱登录 解决方法:在gitee网站上查看自己配 ...

  7. (一):细说贝叶斯滤波:Bayes filters

    认知计算,还要从贝叶斯滤波的基本思想讲起,本文主要是对<Probabilistic Robotics>中贝叶斯滤波器部分的详细讲解. 这一部分,我们先回顾贝叶斯公式的数学基础,然后再来介绍 ...

  8. yolov5实战之二维码检测

    目录 1.前沿 2.二维码数据 3.训练配置 3.1数据集设置 3.2训练参数的配置 3.3网络结构设置 3.4训练 3.5结果示例 附录:数据集下载 1.前沿 之前总结过yolov5来做皮卡丘的检测 ...

  9. Miller-Rabin and Pollard-Rho

    实话实说,我自学(肝)了两天才学会这两个随机算法 记录: Miller-Rabin 她是一个素数判定的算法. 首先需要知道费马小定理 \[a^{p-1}\equiv1\pmod{p}\quad p\i ...

  10. 洛谷5024 保卫王国 (动态dp)

    qwq非正解. 但是能跑过. 1e5 log方还是很稳的啊 首先,考虑最普通的\(dp\) 令\(dp1[x][0]表示不选这个点,dp1[x][1]表示选这个点的最大最小花费\) 那么 \(dp1[ ...