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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39077787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:51:36  来源:igfitidea点击:

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

javaspringspring-mvcspring-4

提问by nowszy94

What's the difference between @GetMappingand @RequestMapping(method = RequestMethod.GET)?
I've seen in some Spring Reactive examples, that @GetMappingwas used instead of @RequestMapping

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

采纳答案by dhS

@GetMappingis a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

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

@GetMappingis the newer annotaion. It supports consumes

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

Consume options are :

消费选项是:

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

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

For Further details see: GetMapping Annotation

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

or read: request mapping variants

或阅读: 请求映射变体

RequestMapping supports consumes as well

RequestMapping 也支持消费

回答by Deroude

As you can see here:

正如你在这里看到的:

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

Difference between @GetMapping& @RequestMapping

@GetMappingsupports the consumesattribute like @RequestMapping.

具体来说,@GetMapping是一个组合注释,用作@RequestMapping(method = RequestMethod.GET).

@GetMapping&之间的区别@RequestMapping

@GetMapping支持consumes@RequestMapping.

回答by zee

@RequestMappingis a class level

@RequestMapping是一个班级

@GetMappingis a method-level

@GetMapping是方法级别的

With sprint Spring 4.3. and up things have changed. Now you can use @GetMapping on the method that will handle the http request. The class-level @RequestMapping specification is refined with the (method-level)@GetMapping annotation

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

Here is an example:

下面是一个例子:

@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";
}
}

Prior to Spring 4.3, it was @RequestMapping(method=RequestMethod.GET)

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

Extra reading from a book authored by Craig WallsExtra reading from a book authored by Craig Walls

克雷格·沃尔斯 (Craig Walls) 所著书籍的额外阅读克雷格·沃尔斯 (Craig Walls) 所著书籍的额外阅读

回答by ZhaoGang

Short answer:

简短的回答:

There is no difference in semantic.

语义上没有区别。

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

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

Further reading:

进一步阅读:

RequestMappingcan be used at class level:

RequestMapping可以在类级别使用:

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.

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

while GetMappingonly applies to method:

whileGetMapping仅适用于方法:

Annotation for mapping HTTP GET requests onto specific handler methods.

用于将 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/GetMapping.html

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

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