当前位置:网站首页>@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获取(上传、订单以及更新订阅信息等)
边栏推荐
- PHP saves session data to MySQL database
- Comment faire une radio personnalisée
- js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序
- What key progress has been made in deep learning in 2021?
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- use. Net analysis Net talent challenge participation
- PHP online examination system version 4.0 source code computer + mobile terminal
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
- [200 opencv routines] 220 Mosaic the image
- 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
猜你喜欢

Aiko ai Frontier promotion (7.6)

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
![[diy] how to make a personalized radio](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[diy] how to make a personalized radio

Interviewer: what is the internal implementation of ordered collection in redis?

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)

MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.

Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)

愛可可AI前沿推介(7.6)

PHP saves session data to MySQL database
随机推荐
##无yum源安装spug监控
数据湖(八):Iceberg数据存储格式
None of the strongest kings in the monitoring industry!
【mysql】游标的基本使用
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
[diy] self designed Microsoft makecode arcade, official open source software and hardware
SAP UI5 框架的 manifest.json
The mail command is used in combination with the pipeline command statement
Aiko ai Frontier promotion (7.6)
Swagger UI tutorial API document artifact
#yyds干货盘点#重新梳理箭头函数的this
自定义限流注解
Huawei device command
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
JS操作dom元素(一)——获取DOM节点的六种方式
OSPF multi zone configuration
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code