当前位置:网站首页>@Detailed differences between pathvariable and @requestparam
@Detailed differences between pathvariable and @requestparam
2022-07-07 06:02:00 【Menon research monk】
Preface
Use springboot When the project is , I often see the mixing of these two annotations in projects
about springboot See my previous article for the basic knowledge points of :
springboot From entry to mastery ( whole )
Today, let's analyze in detail @pathvariable and @Requestparam( All belong spring annotation )
- @PathVariable:/manongyanjiuseng/18
- @RequestParam:/manongyanjiuseng?age=18
The difference is that one is to use ?, A use / To transmit data
This format is similar :RESTFul From getting started to mastering super full parsing ( whole )

1. The source code parsing
see @pathvariable Annotated source code :
@Target({
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
}
see @Requestparam Annotated source code :
@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";
}
Compare the source code of the two
@Requestparam Than @pathvariable One more parameter String defaultValue()
What do you mean : If there is no corresponding parameter variable in the request body , Use default Assign a value to this parameter variable , Assign default values to
@pathvariable and @Requestparam These two annotations are generally linked with the following annotations :
@GetMapping、@PostMapping and @RequestMapping Detailed difference with actual combat code ( whole )
in general :
@pathvariable receive url Parameters on the path
@RequestParam Receive parameter request params
Explain other parameters in the source code :
- @Target Act on parameters
- @Retention The function is to define how long the annotation annotated by it will remain ,RetentionPolicy.RUNTIME Annotations are not only saved to class In file ,jvm load class After the document , There is still
- @Documented, Generate javadoc When you do, you will put @Documented The annotations are shown , But it's useless , Just a logo
2. Practical explanation
The above two annotations should match the parameters of the request body
@PathVariable annotation
@GetMapping ("/manongyanjiuseng/{age}")
@ResponseBody
public String xx( @PathVariable ( "age" ) String age)
@Requestparam annotation
@RGetMapping ("/manongyanjiuseng")
@ResponseBody
public String xx( @RequestParam ( "age" ) String age)
The specific transmission parameters are as follows :
- @PathVariable:/manongyanjiuseng/18
- @RequestParam:/manongyanjiuseng?age=18
边栏推荐
- Web authentication API compatible version information
- How much do you know about clothing ERP?
- TCC of distributed transaction solutions
- Reptile exercises (III)
- Add salt and pepper noise or Gaussian noise to the picture
- Interview skills of software testing
- Mysql-centos7 install MySQL through yum
- [InstallShield] Introduction
- 老板总问我进展,是不信任我吗?(你觉得呢)
- New Year Fireworks code plus copy, are you sure you don't want to have a look
猜你喜欢

Industrial Finance 3.0: financial technology of "dredging blood vessels"
![Cf:c. column swapping [sort + simulate]](/img/0e/64d17980d3ec0051cdfb5fdb34e119.png)
Cf:c. column swapping [sort + simulate]

pytorch_ 01 automatic derivation mechanism
Interview questions and salary and welfare of Shanghai byte

PowerPivot - DAX (function)

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

POI excel export, one of my template methods
上海字节面试问题及薪资福利

力扣102题:二叉树的层序遍历

Jinfo of JVM command: view and modify JVM configuration parameters in real time
随机推荐
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
Pytorch builds neural network to predict temperature
[InstallShield] Introduction
PowerPivot - DAX (function)
Introduction to distributed transactions
EMMC打印cqhci: timeout for tag 10提示分析与解决
VScode进行代码补全
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
Explication contextuelle du langage Go
Hcip eighth operation
Classic questions about data storage
牙齿干细胞的存储问题(未完待续)
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
Digital IC interview summary (interview experience sharing of large manufacturers)
@pathvariable 和 @Requestparam的详细区别
How to improve website weight
ML's shap: Based on the adult census income binary prediction data set (whether the predicted annual income exceeds 50K), use the shap decision diagram combined with the lightgbm model to realize the
CTFshow--常用姿势
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略