@GetMapping和@RequestMapping(method = RequestMethod.GET)注释之间的区别

now*_*y94 130 java spring spring-mvc spring-4

@GetMapping和之间有什么区别@RequestMapping(method = RequestMethod.GET)
我在一些Spring Reactive示例中看到过, @GetMapping而不是使用它@RequestMapping

dhS*_*dhS 176

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

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

消费选项包括:

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

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

或阅读: 请求映射变体

RequestMapping也支持消费

  • RequestMapping支持也很消耗:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes-- (2认同)

Der*_*ude 20

正如你在这里看到的:

具体来说,@GetMapping是一个作为快捷方式的组合注释@RequestMapping(method = RequestMethod.GET).

@GetMapping和之间的区别@RequestMapping

@GetMapping支持consumes类似的属性 @RequestMapping.

  • 好消息!截至4月4日,在您链接到的Spring网页上,GetMapping*现在支持'使用'和通常的RequestMapping属性.可能春天在你的帖子后添加了那些. (14认同)

Zha*_*ang 8

简短的回答:

语义上没有区别。

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

进一步阅读:

RequestMapping 可以在类级别使用:

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

whileGetMapping仅适用于方法:

用于将 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


zee*_*zee 6

@RequestMapping 是班级

@GetMapping 在方法级别

使用sprint Spring 4.3。一切都变了。现在,您可以在将处理http请求的方法上使用@GetMapping。使用(方法级别)@GetMapping注释完善了类级别的@RequestMapping规范

这是一个例子:

@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";
}
}
Run Code Online (Sandbox Code Playgroud)

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

克雷格·沃尔斯(Craig Walls)撰写的书中的额外读物 克雷格·沃尔斯(Craig Walls)撰写的书中的额外读物

  • @RequestMapping也可以应用于方法。 (8认同)