当前位置:网站首页>@pathvariable 和 @Requestparam的详细区别
@pathvariable 和 @Requestparam的详细区别
2022-07-07 00:41:00 【码农研究僧】
前言
使用springboot项目的时候,经常看到这两个注解的在项目中的混用
对于springboot的基础知识点可看我之前的文章:
springboot从入门到精通(全)
今天详细分析下@pathvariable 和 @Requestparam(都归属spring注解)
- @PathVariable:/manongyanjiuseng/18
- @RequestParam:/manongyanjiuseng?age=18
区别在于一个是用?,一个使用/进行传输数据
这种格式类似:RESTFul从入门到精通超全解析(全)

1. 源码解析
查看@pathvariable注解的源码:
@Target({
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
}
查看@Requestparam注解的源码:
@Target({
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}
对比两者的源码
@Requestparam 比 @pathvariable多了一个参数String defaultValue()
大致意思:如果请求体中没有对应的参数变量,使用default对该参数变量赋值,赋上默认值
@pathvariable 和 @Requestparam 这两个注解一般配合如下注解进行联动:
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
总的来说:
@pathvariable 接收url路径上的参数
@RequestParam 接收参数请求的params
对于源码中的其他参数讲解:
- @Target 作用于参数中
- @Retention作用是定义被它所注解的注解保留多久,RetentionPolicy.RUNTIME注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
- @Documented,生成javadoc的时候就会把@Documented注解给显示出来,但其实也没啥用处,一个标识而已
2. 实战讲解
以上两个注解都要配合请求体的参数
@PathVariable 注解
@GetMapping ("/manongyanjiuseng/{age}")
@ResponseBody
public String xx( @PathVariable ( "age" ) String age)
@Requestparam注解
@RGetMapping ("/manongyanjiuseng")
@ResponseBody
public String xx( @RequestParam ( "age" ) String age)
具体的传参如下:
- @PathVariable:/manongyanjiuseng/18
- @RequestParam:/manongyanjiuseng?age=18
边栏推荐
猜你喜欢

Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture

【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先

I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction

Message queue: how to deal with message backlog?
![[cloud native] what is the microservice architecture?](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[cloud native] what is the microservice architecture?

毕业之后才知道的——知网查重原理以及降重举例

Why does the data center need a set of infrastructure visual management system

【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现

EMMC打印cqhci: timeout for tag 10提示分析与解决

2pc of distributed transaction solution
随机推荐
SQL query: subtract the previous row from the next row and make corresponding calculations
2pc of distributed transaction solution
Things about data storage 2
Flinksql 读写pgsql
原生小程序 之 input切換 text與password類型
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
Message queuing: how to ensure that messages are not lost
bat 批示处理详解
SQL Server 2008 各种DateTime的取值范围
得物客服一站式工作台卡顿优化之路
Modes of optical fiber - single mode and multimode
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
[solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
nVisual网络可视化
如果不知道这4种缓存模式,敢说懂缓存吗?
[daily training -- Tencent selected 50] 292 Nim games
Web Authentication API兼容版本信息
[云原生]微服务架构是什么?
yarn入门(一篇就够了)
980. Different path III DFS