• @GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)


    前言

    时常对这几个注解比较混乱,甚至只是模糊的认知
    今天就梳理一下这个知识点

    总的来说
    @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 {};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    通过如上代码可看到注解上面又引用了一些注解

    • 使用@Documented标注了,在生成javadoc的时候就会把@Documented注解给显示出来,但其实也没啥用处,一个标识而已
    • @Retention作用是定义被它所注解的注解保留多久,RetentionPolicy.RUNTIME注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
    • @Target 作用于方法中

    对于这些,在我之前的文章中也讲过:java函数式接口@FunctionalInterface的详细解析(供参考)

    最重要的一个注解表明:

    @RequestMapping(
        method = {RequestMethod.GET}
    )
    
    • 1
    • 2
    • 3

    同样查看@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所示
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    注解大致都相同定义,唯一的不同点在于

    @RequestMapping(
        method = {RequestMethod.POST}
    )
    
    • 1
    • 2
    • 3

    同样查看@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 {};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    通过定义Mapping的注解以及 RequestMethod[] method() default {};的定义去指定method是什么方法

    2. 实战讲解

    注解的使用方法简化如下:

    @RequestMapping(value=/manongyanjiuseng/{id},method= RequestMethod.GET)
    
    • 1

    对于实战代码可结合Restful
    可看我之前这篇文章,比较全面(此处不加赘述)
    RESTFul从入门到精通超全解析(全)

    3. 总结

    在Spring4.3版本之后引入了@GetMapping、@PostMapping 、 @PutMapping、@DeleteMapping等注解来简化HTTP方法的映射

    具体通过查询搜索的时候可以使用Get获取,交互式可以使用Post获取(上传、订单以及更新订阅信息等)

  • 相关阅读:
    CSPM值得考吗?一篇文章说透!(附报名流程)
    Windows下搭建nginx和rtspToWebRTC以及Windows程序添加为服务开机启动和后台运行
    GBase8s jdbc连接超时参数说明
    基于多能互补的热电联供型微网优化运行(Matlab代码实现)
    Ubuntu部署OpenStack踩坑指南:还要看系统版本?
    HP E1740A 模拟量输入模块
    【LeetCode刷题笔记】链表
    力扣第40题 组合总和 || c++ 回溯经典
    码蹄集 - MT2142 - 万民堂大厨
    torch.stack
  • 原文地址:https://blog.csdn.net/weixin_47872288/article/details/125627041