当前位置:网站首页>@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
边栏推荐
- 一个简单的代数问题的求解
- Win configuration PM2 boot auto start node project
- PTA 天梯赛练习题集 L2-004 搜索树判断
- [daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
- 架构设计的五个核心要素
- Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
- 三级菜单数据实现,实现嵌套三级菜单数据
- Flask1.1.4 werkzeug1.0.1 source code analysis: start the process
- The 2022 China low / no code Market Research and model selection evaluation report was released
- [云原生]微服务架构是什么?
猜你喜欢

AI face editor makes Lena smile

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

PTA 天梯赛练习题集 L2-004 搜索树判断

Simple case of SSM framework

ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略

yarn入门(一篇就够了)

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

cf:C. Column Swapping【排序 + 模拟】

Différenciation et introduction des services groupés, distribués et microservices

Red Hat安装内核头文件
随机推荐
[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]
集群、分布式、微服务的区别和介绍
STM32按键状态机2——状态简化与增加长按功能
Lombok plug-in
Classic questions about data storage
Différenciation et introduction des services groupés, distribués et microservices
What is make makefile cmake qmake and what is the difference?
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
得物客服一站式工作台卡顿优化之路
关于服装ERP,你知道多少?
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
Determine whether the file is a DICOM file
搞懂fastjson 对泛型的反序列化原理
The 2022 China low / no code Market Research and model selection evaluation report was released
成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?
pytorch_ 01 automatic derivation mechanism
What is dependency injection (DI)
Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
《ClickHouse原理解析与应用实践》读书笔记(6)
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss