当前位置:网站首页>@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
边栏推荐
- 职场经历反馈给初入职场的程序员
- Red hat install kernel header file
- Jstat of JVM command: View JVM statistics
- C. colonne Swapping [tri + Simulation]
- 搞懂fastjson 对泛型的反序列化原理
- Nvisual network visualization
- Cf:c. column swapping [sort + simulate]
- Value range of various datetimes in SQL Server 2008
- Get the way to optimize the one-stop worktable of customer service
- PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
猜你喜欢
404 not found service cannot be reached in SAP WebService test
Digital IC interview summary (interview experience sharing of large manufacturers)
EMMC打印cqhci: timeout for tag 10提示分析与解决
老板总问我进展,是不信任我吗?(你觉得呢)
软件测试面试技巧
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
绕过open_basedir
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
《ClickHouse原理解析与应用实践》读书笔记(6)
随机推荐
Value range of various datetimes in SQL Server 2008
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
如何提高网站权重
JVM命令之- jmap:导出内存映像文件&内存使用情况
Introduction to distributed transactions
Sidecar mode
pytorch_ 01 automatic derivation mechanism
Get the way to optimize the one-stop worktable of customer service
Question 102: sequence traversal of binary tree
Pytorch builds neural network to predict temperature
On the difference between FPGA and ASIC
Personal imitation SSM framework
一个简单的代数问题的求解
上海字节面试问题及薪资福利
《ClickHouse原理解析与应用实践》读书笔记(6)
yarn入门(一篇就够了)
Financial risk control practice - decision tree rule mining template
Reptile exercises (III)
An example of multi module collaboration based on NCF
Nvisual network visualization