• @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获取(上传、订单以及更新订阅信息等)

  • 相关阅读:
    【node.js从入门到精通】编写接口,使用CROS解决跨域问题,jsonp的接口
    MSDC 4.3 接口规范(7)
    JavaScript函数中this的指向问题讨论(普通函数和箭头函数)
    1811_spacemacs从v.0.200.13升级到v.0.200.14的几点变化感受
    性能测试 —— 通过Jmeter压测存储过程!
    架构思考 (五)
    语音识别工具kaldi简介
    微服务系列开端-简述
    水处理行业污水处理厂电能质量监测与治理系统解决方案-安科瑞黄安南
    SpringMVC请求、响应与异步请求
  • 原文地址:https://blog.csdn.net/weixin_47872288/article/details/125627041