当前位置:网站首页>@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
边栏推荐
- CMD permanently delete specified folders and files
- cf:C. Column Swapping【排序 + 模擬】
- 980. Different path III DFS
- [daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
- Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
- I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
- 驱动开发中platform设备驱动架构详解
- Red Hat安装内核头文件
- 苹果cms V10模板/MXone Pro自适应影视电影网站模板
- 云加速,帮助您有效解决攻击问题!
猜你喜欢
[云原生]微服务架构是什么?
Jinfo of JVM command: view and modify JVM configuration parameters in real time
[InstallShield] Introduction
TCC of distributed transaction solutions
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
Interview questions and salary and welfare of Shanghai byte
The solution of a simple algebraic problem
404 not found service cannot be reached in SAP WebService test
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
[daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
随机推荐
[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]
Interview skills of software testing
Say sqlyog deceived me!
What is make makefile cmake qmake and what is the difference?
[InstallShield] Introduction
《ClickHouse原理解析与应用实践》读书笔记(6)
Reading notes of Clickhouse principle analysis and Application Practice (6)
PowerPivot——DAX(函数)
POI excel export, one of my template methods
如果不知道这4种缓存模式,敢说懂缓存吗?
PTA 天梯赛练习题集 L2-002 链表去重
yarn入门(一篇就够了)
关于STC单片机“假死”状态的判别
如何提高网站权重
10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
JVM命令之 jstat:查看JVM统计信息
SAP Spartacus checkout 流程的扩展(extend)实现介绍
Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
pytorch_ 01 automatic derivation mechanism
The boss always asks me about my progress. Don't you trust me? (what do you think)