当前位置:网站首页>@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获取(上传、订单以及更新订阅信息等)
边栏推荐
- 2017 8th Blue Bridge Cup group a provincial tournament
- el-table表格——sortable排序 & 出现小数、%时排序错乱
- js中,字符串和数组互转(一)——字符串转为数组的方法
- ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
- What are RDB and AOF
- PHP saves session data to MySQL database
- Spark SQL chasing Wife Series (initial understanding)
- 2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
- Huawei device command
- Interviewer: what is the internal implementation of ordered collection in redis?
猜你喜欢

Redis insert data garbled solution

3D人脸重建:从基础知识到识别/重建方法!

Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
![[DSP] [Part 1] start DSP learning](/img/81/051059958dfb050cb04b8116d3d2a8.png)
[DSP] [Part 1] start DSP learning

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
![Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]

Infrared thermometer based on STM32 single chip microcomputer (with face detection)

爱可可AI前沿推介(7.6)

Opencv learning example code 3.2.3 image binarization

968 edit distance
随机推荐
Pat 1085 perfect sequence (25 points) perfect sequence
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
Spark SQL chasing Wife Series (initial understanding)
What are RDB and AOF
document.write()的用法-写入文本——修改样式、位置控制
PHP saves session data to MySQL database
js之遍历数组、字符串
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
2110 summary of knowledge points and common problems in redis class
el-table表格——sortable排序 & 出现小数、%时排序错乱
知识图谱构建流程步骤详解
C language games - three chess
PG基础篇--逻辑结构管理(事务)
Common English vocabulary that every programmer must master (recommended Collection)
R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
性能测试过程和计划
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
基于深度学习的参考帧生成
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
【mysql】触发器