【问题标题】:Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)注释 @GetMapping 和 @RequestMapping(method = RequestMethod.GET) 之间的区别
【发布时间】:2016-12-28 21:38:42
【问题描述】:

@GetMapping@RequestMapping(method = RequestMethod.GET) 有什么区别?
我在一些 Spring Reactive 示例中看到过, 使用@GetMapping 代替@RequestMapping

【问题讨论】:

    标签: java spring spring-mvc spring-4


    【解决方案1】:
    1. @RequestMapping 即使使用 method=GET 也支持消费,而 @GetMapping 不支持消费。
    2. @RequestMapping 是 Method & Type 级别的注解,而@GetMapping 是 Method 级别的注解

    除此之外@GetMapping 与@RequestMapping(method=RequestMethod.GET) 相同

    【讨论】:

      【解决方案2】:

      @GetMapping 是一个组合注释,充当@RequestMapping(method = RequestMethod.GET) 的快捷方式。

      @GetMapping 是较新的注解。 支持消费

      消费选项是:

      consumes = "text/plain"
      消耗 = {"text/plain", "application/*"}

      更多详情见: GetMapping Annotation

      或阅读: request mapping variants

      RequestMapping 也支持消费

      GetMapping 我们只能在方法级别应用,而 RequestMapping 注解我们可以在类级别和方法级别应用

      【讨论】:

      【解决方案3】:

      简答:

      语义上没有区别。

      具体来说,@GetMapping 是一个组合注解,它充当一个 @RequestMapping(method = RequestMethod.GET) 的快捷方式

      进一步阅读:

      RequestMapping 可用于类级别:

      这个注解可以在类和方法级别使用。 在大多数情况下,在方法级别的应用程序会更喜欢使用一个 HTTP 方法特定变体的@GetMapping、@PostMapping、 @PutMapping、@DeleteMapping 或@PatchMapping。

      GetMapping 仅适用于方法:

      用于将 HTTP GET 请求映射到特定处理程序的注释 方法。


      https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/GetMapping.html

      https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

      【讨论】:

        【解决方案4】:

        @RequestMapping 是类级别

        @GetMapping 是方法级别

        使用 sprint Spring 4.3。事情发生了变化。现在您可以在处理 http 请求的方法上使用 @GetMapping。类级别的@RequestMapping 规范使用(方法级别)@GetMapping 注释进行了细化

        这是一个例子:

        @Slf4j
        @Controller
        @RequestMapping("/orders")/* The @Request-Mapping annotation, when applied
                                    at the class level, specifies the kind of requests 
                                    that this controller handles*/  
        
        public class OrderController {
        
        @GetMapping("/current")/*@GetMapping paired with the classlevel
                                @RequestMapping, specifies that when an 
                                HTTP GET request is received for /order, 
                                orderForm() will be called to handle the request..*/
        
        public String orderForm(Model model) {
        
        model.addAttribute("order", new Order());
        
        return "orderForm";
        }
        }
        

        在 Spring 4.3 之前,它是@RequestMapping(method=RequestMethod.GET)

        Craig Walls 所著书籍的额外阅读

        【讨论】:

        • @RequestMapping 也可以应用于方法。
        • 是的,可以。但是,@GetMapping 更简洁,更具体到它所针对的 HTTP 方法。
        【解决方案5】:

        如你所见here:

        具体来说,@GetMapping 是一个组合注解,它充当 @RequestMapping(method = RequestMethod.GET) 的快捷方式。

        @GetMapping@RequestMapping 之间的区别

        @GetMapping 支持 consumes 属性,例如 @RequestMapping.

        【讨论】:

        • 好消息!自 2017 年 4 月起,在您链接到的 Spring 网页上,GetMapping 确实 现在支持“consumes”和通常的 RequestMapping 属性。可能 Spring 在您的帖子之后添加了这些内容。
        猜你喜欢
        • 2012-06-25
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        • 2017-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多