当前位置:网站首页>@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获取(上传、订单以及更新订阅信息等)
边栏推荐
- Huawei device command
- 知识图谱之实体对齐二
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- 快过年了,心也懒了
- C language games - three chess
- Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
- 967- letter combination of telephone number
- 性能测试过程和计划
- Regular expression collection
猜你喜欢

OAI 5G NR+USRP B210安装搭建

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,

知识图谱之实体对齐二

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient

No Yum source to install SPuG monitoring

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

2017 8th Blue Bridge Cup group a provincial tournament

Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition

性能测试过程和计划

审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
随机推荐
【微信小程序】运行机制和更新机制
Comment faire une radio personnalisée
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Interviewer: what is the internal implementation of ordered collection in redis?
None of the strongest kings in the monitoring industry!
[diy] how to make a personalized radio
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
面试官:Redis中有序集合的内部实现方式是什么?
R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
请问sql group by 语句问题
What are RDB and AOF
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
#yyds干货盘点#重新梳理箭头函数的this
Reinforcement learning - learning notes 5 | alphago
Xcode6 error: "no matching provisioning profiles found for application"
Data Lake (VIII): Iceberg data storage format
Performance test process and plan
强化学习-学习笔记5 | AlphaGo