springMVC的注解@PathVariable是什么?详情及用法解析
在路由中定义变量规则后,通常我们需要在处理方法(也就是@RequestMapping注解的方法)中获取这个URL变量的具体值,并根据这个值(例如用户名)做相应的操作,Spring MVC提供的@PathVariable可以帮助我们:
@GetMapping("/users/{username}")
public String userProfile(@PathVariable String username) {
return String.format("user %s", username);
}
在上述例子中,当@Controller处理HTTP请求时,userProfile的参数username会被自动设置为URL中的对应变量username(同名赋值)的值,例如当HTTP请求为:/users/tianmaying,URL变量username的值tianmaying会被赋给函数参数username,函数的返回值自然也就是:String.format("user %s", username);,即user tianmaying。
在默认情况下,Spring会对
@PathVariable注解的变量进行自动赋值,当然你也可以指定@PathVariable使用哪一个URL中的变量:@GetMapping("/users/{username}")
public String userProfile(@PathVariable("username") String user) {
return String.format("user %s", user);
}这时,由于注解
String user的@PathVariable的参数值为username,所以仍然会将URL变量username的值赋给变量user
@RequestParam
@RequestParam和@PathVariable的区别就在于请求时当前参数是在url路由上还是在请求的body上,例如有下面一段代码:
@RequestMapping(value="", method=RequestMethod.POST)
public String postUser(@RequestParam(value="phoneNum", required=true) String phoneNum ) String userName) {
userService.create(phoneNum, userName);
return "success";
}
这个接口的请求url这样写:http://xxxxx?phoneNum=xxxxxx,也就是说被@RequestParam修饰的参数最后通过key=value的形式放在http请求的Body传过来,对比下上面的@PathVariable就很容易看出两者的区别了。
@RequestBody直接以String接收前端传过来的json数据
=================================
参考资料:
获取URL变量
@RequestParam @RequestBody @PathVariable的作用
end
springMVC的注解@PathVariable是什么?详情及用法解析的更多相关文章
- springMVC的注解详解
springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...
- 一 : springmvc常用注解
springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- springmvc常用注解与类型转换
springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...
- SpringMVC异常处理注解@ExceptionHandler@ControllerAdvice@ResponseStatus
参考: http://blog.csdn.net/w372426096/article/details/78429132 http://blog.csdn.net/w372426096/article ...
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...
- SpringMVC 常用注解 详解
SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping 路径映射 2.@Requ ...
随机推荐
- 资源的合并与压缩-html压缩
资源的合并:减少http请求数量 资源的压缩:减少请求资源的大小 html压缩 html代码压缩就是压缩这些在文本文件中有意义,但是在html中不显示的字符,包括空格,制表符,换行符等,还有一些其他意 ...
- Vue-router(1)之component标签
1. 使用 <component>标签实现组件切换 <component> 是Vue提供的标签语法:有一个is属性,is的作用就是显示指定的组件 <template> ...
- transform—切割轮播图
效果演示: 1.结构分析 第一步:在一个div里面有显示图片的ul标签(1个)和左右切换的a标签(2个): 第二步:ul标签中有5个li标签,li标签浮动,每个li标签的宽度占ul宽度的五分之一,高度 ...
- 【每日Scrum】第六天冲刺
一.计划会议内容 数据库仍然有问题,决定先绕过数据库,进行软件内容设计与界面ui美化. 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图
- LeetCode No.166,167,168
No.166 FractionToDecimal 分数到小数 题目 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数 ...
- IO读写
1.read & write read: 把数据从内核缓冲区复制到进程缓冲区. write: 把数据从进程缓冲区复制到内核缓冲区. 上层程序的IO操作.不是物理设备级别的读写,而是缓存的复制. ...
- MySQL--索引和外键
来自:http://www.jb51.net/article/32149.htm 1.添加PRIMARY KEY(主键索引) ALTER TABLE `table_name` ADD PRIMARY ...
- //使用PDO连接mysql数据库
<?php //使用PDO连接mysql数据库 class pdo_con{ var $dsn = 'mysql:dbname=test; host:127.0.0.1'; va ...
- image compression with libjpeg
http://www.aaronmr.com/en/2010/03/test/ Working on the project I've seen in the need for compression ...
- 字符串中子序列出现次数(dp)
躲藏 链接:https://ac.nowcoder.com/acm/problem/15669来源:牛客网 题目描述 XHRlyb和她的小伙伴Cwbc在玩捉迷藏游戏. Cwbc藏在多个不区分大小写的字 ...