注解@GetMapping 和@RequestMapping(method = RequestMethod.GET) 的区别

Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)

@GetMapping@RequestMapping(method = RequestMethod.GET)有什么区别?
我在一些 Spring 响应式示例中看到, 使用 @GetMapping 而不是 @RequestMapping

如你所见here:

Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

Difference between @GetMapping & @RequestMapping

@GetMapping supports the consumes attribute like @RequestMapping.

@GetMapping 是一个组合注释,充当 @RequestMapping(method = RequestMethod.GET).

的快捷方式

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

消费选项是:

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

有关更多详细信息,请参阅: GetMapping Annotation

或阅读: request mapping variants

RequestMapping 也支持消费

GetMapping 我们只能应用于方法级别,而 RequestMapping 注释我们可以应用于 class 级别以及方法级别

@RequestMapping 是 class 级别

@GetMapping是方法级

冲刺 Spring 4.3。事情发生了变化。现在您可以在处理 http 请求的方法上使用 @GetMapping。 class 级别的@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) 所著书籍的额外阅读内容

简答:

语义上没有区别

Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

进一步阅读:

RequestMapping可用于class级别:

This annotation can be used both at the class and at the method level. In most cases, at the method level applications will prefer to use one of the HTTP method specific variants @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, or @PatchMapping.

GetMapping 仅适用于方法:

Annotation for mapping HTTP GET requests onto specific handler methods.


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

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

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

相同

                         `@RequestMapping` since 2.5

=> 可以处理所有 HTTP 方法

=>适用于class和方法

=> 可以代替@Controller@RestController,如果我们用 连同 @Component.


                        `@GetMapping` since 4.3

=> 只能处理 HTTP 的 GET 方法

=> 仅适用于方法

@GetMapping@RequestMapping(method = RequestMethod.GET) 的特定类型。两者都支持消耗