当前位置:网站首页>@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
2022-07-06 12:51:00 【码农研究僧】
前言
时常对这几个注解比较混乱,甚至只是模糊的认知
今天就梳理一下这个知识点
总的来说@GetMapping等于@RequestMapping(method = RequestMethod.GET)@PostMapping等于@RequestMapping(method = RequestMethod.POST)
更加深层次的理解可看如下
1. 源码解析
理解一个函数功能或者注解原理,需要了解深层次的代码才能加深认知
@GetMapping的源码如下:
@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 {
};
}
通过如上代码可看到注解上面又引用了一些注解
- 使用@Documented标注了,在生成javadoc的时候就会把@Documented注解给显示出来,但其实也没啥用处,一个标识而已
- @Retention作用是定义被它所注解的注解保留多久,RetentionPolicy.RUNTIME注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
- @Target 作用于方法中
对于这些,在我之前的文章中也讲过:java函数式接口@FunctionalInterface的详细解析(供参考)
最重要的一个注解表明:
@RequestMapping(
method = {
RequestMethod.GET}
)
同样查看@PostMapping的注解源码
@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 {
};
//省略定义代码,具体如@GetMapping所示
}
注解大致都相同定义,唯一的不同点在于
@RequestMapping(
method = {
RequestMethod.POST}
)
同样查看@RequestMapping的注解源码
@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 {
};
}
通过定义Mapping的注解以及 RequestMethod[] method() default {};的定义去指定method是什么方法
2. 实战讲解
注解的使用方法简化如下:
@RequestMapping(value=”/manongyanjiuseng/{
id}”,method= RequestMethod.GET)
对于实战代码可结合Restful
可看我之前这篇文章,比较全面(此处不加赘述)
RESTFul从入门到精通超全解析(全)
3. 总结
在Spring4.3版本之后引入了@GetMapping、@PostMapping 、 @PutMapping、@DeleteMapping等注解来简化HTTP方法的映射
具体通过查询搜索的时候可以使用Get获取,交互式可以使用Post获取(上传、订单以及更新订阅信息等)
边栏推荐
- el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
- 面试官:Redis中有序集合的内部实现方式是什么?
- Manifest of SAP ui5 framework json
- [MySQL] basic use of cursor
- What key progress has been made in deep learning in 2021?
- Interviewer: what is the internal implementation of ordered collection in redis?
- Summary of different configurations of PHP Xdebug 3 and xdebug2
- 每个程序员必须掌握的常用英语词汇(建议收藏)
- 2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
- use. Net drives the OLED display of Jetson nano
猜你喜欢

知识图谱之实体对齐二

【OpenCV 例程200篇】220.对图像进行马赛克处理

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件

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

C language operators

use. Net analysis Net talent challenge participation

全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
![[200 opencv routines] 220 Mosaic the image](/img/75/0293e10ad6de7ed86df4cacbd79b54.png)
[200 opencv routines] 220 Mosaic the image

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

The mail command is used in combination with the pipeline command statement
随机推荐
请问sql group by 语句问题
OneNote 深度评测:使用资源、插件、模版
[diy] how to make a personalized radio
性能测试过程和计划
Entity alignment two of knowledge map
动态切换数据源
ICML 2022 | flowformer: task generic linear complexity transformer
3D人脸重建:从基础知识到识别/重建方法!
What are RDB and AOF
Huawei device command
document.write()的用法-写入文本——修改样式、位置控制
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
Pycharm remote execution
What key progress has been made in deep learning in 2021?
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
基于深度学习的参考帧生成
SSO single sign on
@PathVariable
Swagger UI tutorial API document artifact
Yyds dry goods count re comb this of arrow function