当前位置:网站首页>@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
边栏推荐
- Dynamic memory management
- Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
- linear regression
- CMD permanently delete specified folders and files
- Solve pod install error: FFI is an incompatible architecture
- 【SQL实战】一条SQL统计全国各地疫情分布情况
- New Year Fireworks code plus copy, are you sure you don't want to have a look
- Question 102: sequence traversal of binary tree
- Win configuration PM2 boot auto start node project
- make makefile cmake qmake都是什么,有什么区别?
猜你喜欢
随机推荐
Digital IC interview summary (interview experience sharing of large manufacturers)
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
SAP ABAP BDC (batch data communication) -018
牙齿干细胞的存储问题(未完待续)
yarn入门(一篇就够了)
谈fpga和asic的区别
Get the way to optimize the one-stop worktable of customer service
PTA 天梯赛练习题集 L2-004 搜索树判断
nVisual网络可视化
《ClickHouse原理解析与应用实践》读书笔记(6)
Determine whether the file is a DICOM file
SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
PTA ladder game exercise set l2-002 linked list de duplication
How much do you know about clothing ERP?
往图片添加椒盐噪声或高斯噪声
Go language context explanation
上海字节面试问题及薪资福利
linear regression
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