当前位置:网站首页>@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 )
边栏推荐
- Common English vocabulary that every programmer must master (recommended Collection)
- [wechat applet] operation mechanism and update mechanism
- Three schemes of SVM to realize multi classification
- Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
- Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
- Spark SQL chasing Wife Series (initial understanding)
- 2110 summary of knowledge points and common problems in redis class
- 【微信小程序】运行机制和更新机制
- Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
- 967- letter combination of telephone number
猜你喜欢
OAI 5g nr+usrp b210 installation and construction
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
PHP online examination system version 4.0 source code computer + mobile terminal
What is the problem with the SQL group by statement
[MySQL] basic use of cursor
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
【微信小程序】運行機制和更新機制
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
随机推荐
Three schemes of SVM to realize multi classification
Yyds dry goods count re comb this of arrow function
2017 8th Blue Bridge Cup group a provincial tournament
Reinforcement learning - learning notes 5 | alphago
The biggest pain point of traffic management - the resource utilization rate cannot go up
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
966 minimum path sum
请问sql group by 语句问题
OneNote in-depth evaluation: using resources, plug-ins, templates
What is the difference between procedural SQL and C language in defining variables
快过年了,心也懒了
基于STM32单片机设计的红外测温仪(带人脸检测)
Common English vocabulary that every programmer must master (recommended Collection)
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
Taylor series fast Fourier transform (FFT)
OneNote 深度评测:使用资源、插件、模版
#yyds干货盘点#重新梳理箭头函数的this