当前位置:网站首页>@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 )
边栏推荐
- OneNote 深度评测:使用资源、插件、模版
- Web开发小妙招:巧用ThreadLocal规避层层传值
- PG basics -- Logical Structure Management (transaction)
- Three schemes of SVM to realize multi classification
- use. Net drives the OLED display of Jetson nano
- What key progress has been made in deep learning in 2021?
- Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
- 快过年了,心也懒了
- OAI 5g nr+usrp b210 installation and construction
- #yyds干货盘点#重新梳理箭头函数的this
猜你喜欢

Study notes of grain Mall - phase I: Project Introduction

OneNote 深度评测:使用资源、插件、模版
![[wechat applet] operation mechanism and update mechanism](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
[wechat applet] operation mechanism and update mechanism

use. Net analysis Net talent challenge participation

967- letter combination of telephone number

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

防火墙基础之外网服务器区部署和双机热备

KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐

Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)

ICML 2022 | flowformer: task generic linear complexity transformer
随机推荐
Regular expression collection
C language games - minesweeping
OAI 5g nr+usrp b210 installation and construction
Web开发小妙招:巧用ThreadLocal规避层层传值
@PathVariable
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
过程化sql在定义变量上与c语言中的变量定义有什么区别
Web开发小妙招:巧用ThreadLocal规避层层传值
use. Net analysis Net talent challenge participation
每个程序员必须掌握的常用英语词汇(建议收藏)
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
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,
Huawei device command
[MySQL] trigger
OneNote in-depth evaluation: using resources, plug-ins, templates
##无yum源安装spug监控
038. (2.7) less anxiety
【论文解读】用于白内障分级/分类的机器学习技术
Is this the feeling of being spoiled by bytes?
R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖