当前位置:网站首页>@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
边栏推荐
- JVM命令之 jstat:查看JVM统计信息
- [shell] clean up nohup Out file
- STM32 key state machine 2 - state simplification and long press function addition
- Industrial Finance 3.0: financial technology of "dredging blood vessels"
- Cf:c. column swapping [sort + simulate]
- 【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现
- Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
- Things about data storage 2
- pytorch_ 01 automatic derivation mechanism
- [daily training -- Tencent selected 50] 292 Nim games
猜你喜欢

CTFshow--常用姿势

Sidecar mode

Sequential storage of stacks

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

yarn入门(一篇就够了)
![[cloud native] what is the microservice architecture?](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[cloud native] what is the microservice architecture?

Understand the deserialization principle of fastjson for generics

10W word segmentation searches per second, the product manager raised another demand!!! (Collection)

Mac version PHP installed Xdebug environment (M1 version)

2pc of distributed transaction solution
随机推荐
Chain storage of stack
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
Check Point:企业部署零信任网络(ZTNA)的核心要素
What EDA companies are there in China?
cf:C. Column Swapping【排序 + 模拟】
Question 102: sequence traversal of binary tree
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
Add salt and pepper noise or Gaussian noise to the picture
Flask1.1.4 Werkzeug1.0.1 源碼分析:啟動流程
Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
nVisual网络可视化
话说SQLyog欺骗了我!
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
谈fpga和asic的区别
Go language context explanation
Classic questions about data storage
Bat instruction processing details
Randomly generate session_ id