当前位置:网站首页>@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 notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- PG基础篇--逻辑结构管理(事务)
- Swagger UI tutorial API document artifact
- [MySQL] trigger
- 愛可可AI前沿推介(7.6)
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- [200 opencv routines] 220 Mosaic the image
- Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- 请问sql group by 语句问题
猜你喜欢

Spark SQL chasing Wife Series (initial understanding)

Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
![Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]

use. Net drives the OLED display of Jetson nano

Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?

966 minimum path sum

2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性

What is the problem with the SQL group by statement

PHP saves session data to MySQL database

KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
随机推荐
Detailed explanation of knowledge map construction process steps
User defined current limiting annotation
【OpenCV 例程200篇】220.对图像进行马赛克处理
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
##无yum源安装spug监控
This year, Jianzhi Tencent
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
968 edit distance
Three schemes of SVM to realize multi classification
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
@PathVariable
[asp.net core] set the format of Web API response data -- formatfilter feature
Web开发小妙招:巧用ThreadLocal规避层层传值
PG基础篇--逻辑结构管理(事务)
爱可可AI前沿推介(7.6)
Reflection operation exercise
Yyds dry goods count re comb this of arrow function
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit