当前位置:网站首页>@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获取(上传、订单以及更新订阅信息等)
边栏推荐
- for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
- Opencv learning example code 3.2.3 image binarization
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
- 966 minimum path sum
- ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
- Web开发小妙招:巧用ThreadLocal规避层层传值
- js中,字符串和数组互转(二)——数组转为字符串的方法
- User defined current limiting annotation
- How to turn a multi digit number into a digital list
猜你喜欢
【论文解读】用于白内障分级/分类的机器学习技术
知识图谱构建流程步骤详解
(work record) March 11, 2020 to March 15, 2021
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
[MySQL] trigger
OAI 5g nr+usrp b210 installation and construction
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
【mysql】游标的基本使用
【mysql】触发器
随机推荐
C language games - three chess
C language games - minesweeping
Swagger UI tutorial API document artifact
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
过程化sql在定义变量上与c语言中的变量定义有什么区别
基于STM32单片机设计的红外测温仪(带人脸检测)
动态切换数据源
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
el-table表格——sortable排序 & 出现小数、%时排序错乱
正则表达式收集
Interviewer: what is the internal implementation of ordered collection in redis?
数据湖(八):Iceberg数据存储格式
2110 summary of knowledge points and common problems in redis class
Distributed ID
Entity alignment two of knowledge map
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
[asp.net core] set the format of Web API response data -- formatfilter feature
2017 8th Blue Bridge Cup group a provincial tournament
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序