当前位置:网站首页>@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
2022-07-06 21:09:00 【Menon research monk】
Preface
These annotations are often confused , Even vague cognition
Today, let's sort out this knowledge point
in general @GetMapping be equal to @RequestMapping(method = RequestMethod.GET)@PostMapping be equal to @RequestMapping(method = RequestMethod.POST)
A deeper understanding can be seen as follows
1. The source code parsing
Understand a function function or annotation principle , You need to understand deep-seated code to deepen your understanding
@GetMapping The source code is as follows :
@Target({
ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
method = {
RequestMethod.GET}
)
public @interface GetMapping {
@AliasFor(
annotation = RequestMapping.class
)
String name() default "";
@AliasFor(
annotation = RequestMapping.class
)
String[] value() default {
};
@AliasFor(
annotation = RequestMapping.class
)
String[] path() default {
};
@AliasFor(
annotation = RequestMapping.class
)
String[] params() default {
};
@AliasFor(
annotation = RequestMapping.class
)
String[] headers() default {
};
@AliasFor(
annotation = RequestMapping.class
)
String[] consumes() default {
};
@AliasFor(
annotation = RequestMapping.class
)
String[] produces() default {
};
}
From the above code, you can see that some annotations are quoted above
- Use @Documented Marking the , It's generating javadoc When you do, you will put @Documented The annotations are shown , But it's useless , Just a logo
- @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
- @Target Act on a method
For these , I also mentioned in my previous article :java Functional interface @FunctionalInterface A detailed analysis of ( For reference )
The most important note indicates :
@RequestMapping(
method = {
RequestMethod.GET}
)
See also @PostMapping Annotation source code
@Target({
ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
method = {
RequestMethod.POST}
)
public @interface PostMapping {
@AliasFor(
annotation = RequestMapping.class
)
String name() default "";
@AliasFor(
annotation = RequestMapping.class
)
String[] value() default {
};
// Omit the definition code , Specific as @GetMapping Shown
}
Annotations are roughly the same definition , The only difference is
@RequestMapping(
method = {
RequestMethod.POST}
)
See also @RequestMapping Annotation source code
@Target({
ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {
};
@AliasFor("value")
String[] path() default {
};
RequestMethod[] method() default {
};
String[] params() default {
};
String[] headers() default {
};
String[] consumes() default {
};
String[] produces() default {
};
}
By defining Mapping And RequestMethod[] method() default {}; To specify method What's the way
2. Practical explanation
The use of annotations is simplified as follows :
@RequestMapping(value=”/manongyanjiuseng/{
id}”,method= RequestMethod.GET)
For actual combat code, it can be combined Restful
But look at my previous article , More comprehensive ( I'm not going to repeat it here )
RESTFul From getting started to mastering super full parsing ( whole )
3. summary
stay Spring4.3 Version after the introduction of @GetMapping、@PostMapping 、 @PutMapping、@DeleteMapping And so on HTTP Mapping of methods
Specifically, you can use Get obtain , Interactive can be used Post obtain ( Upload 、 Order and update subscription information )
边栏推荐
- Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
- The mail command is used in combination with the pipeline command statement
- Minimum cut edge set of undirected graph
- What is the problem with the SQL group by statement
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- js之遍历数组、字符串
- OSPF multi zone configuration
- 2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
- 拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
- PHP online examination system version 4.0 source code computer + mobile terminal
猜你喜欢

Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an

for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环

3D face reconstruction: from basic knowledge to recognition / reconstruction methods!

愛可可AI前沿推介(7.6)

What key progress has been made in deep learning in 2021?

基于STM32单片机设计的红外测温仪(带人脸检测)

强化学习-学习笔记5 | AlphaGo

Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition

HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅

防火墙基础之外网服务器区部署和双机热备
随机推荐
过程化sql在定义变量上与c语言中的变量定义有什么区别
el-table表格——sortable排序 & 出现小数、%时排序错乱
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
Math symbols in lists
防火墙基础之外网服务器区部署和双机热备
[200 opencv routines] 220 Mosaic the image
Regular expression collection
None of the strongest kings in the monitoring industry!
968 edit distance
Manifest of SAP ui5 framework json
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
js中,字符串和数组互转(二)——数组转为字符串的方法
面试官:Redis中有序集合的内部实现方式是什么?
Spark SQL chasing Wife Series (initial understanding)
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
PG basics -- Logical Structure Management (transaction)
[MySQL] basic use of cursor
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
@PathVariable