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
Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)
提问by nowszy94
What's the difference between @GetMapping
and @RequestMapping(method = RequestMethod.GET)
?
I've seen in some Spring Reactive examples, that
@GetMapping
was used instead of @RequestMapping
@GetMapping
和 和有@RequestMapping(method = RequestMethod.GET)
什么区别?
我在一些 Spring Reactive 示例中看到,它
@GetMapping
被用来代替@RequestMapping
采纳答案by dhS
@GetMapping
is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)
.
@GetMapping
是一个组合注释,作为 的快捷方式@RequestMapping(method = RequestMethod.GET)
。
@GetMapping
is 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,
@GetMapping
is a composed annotation that acts as a shortcut for@RequestMapping(method = RequestMethod.GET)
.Difference between
@GetMapping
&@RequestMapping
@GetMapping
supports theconsumes
attribute like@RequestMapping
.
具体来说,
@GetMapping
是一个组合注释,用作@RequestMapping(method = RequestMethod.GET)
.
@GetMapping
&之间的区别@RequestMapping
@GetMapping
支持consumes
像@RequestMapping
.
回答by zee
@RequestMapping
is a class level
@RequestMapping
是一个班级
@GetMapping
is 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)
回答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:
进一步阅读:
RequestMapping
can 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 GetMapping
only applies to method:
whileGetMapping
仅适用于方法:
Annotation for mapping HTTP GET requests onto specific handler methods.
用于将 HTTP GET 请求映射到特定处理程序方法的注释。