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.
7 Answers
@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
@GetMapping is the newer annotaion.
It supports consumes
Consume options are :
consumes = "text/plain"
consumes = {"text/plain", "application/\*"}
For Further details see: GetMapping Annotation
or read: request mapping variants
RequestMapping supports consumes as well
GetMapping can be applied only at the method level, whereas the RequestMapping annotation can be applied at the class level as well as the method level.
2 Comments
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 theconsumesattribute like@RequestMapping.
1 Comment
@RequestMapping is a class level
@GetMapping is a method-level
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
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)
4 Comments
Short answer:
There is no difference in semantic.
Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
Further reading:
RequestMapping can be used at class level:
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.
while GetMapping only applies to method:
Annotation for mapping HTTP GET requests onto specific handler methods.
Comments
`@RequestMapping` since 2.5
=> Can handle all HTTP methods
=> Applicable to class and method
=> Can be used as a substitute of @Controller and @RestController, If we use it
along with @Component.
`@GetMapping` since 4.3
=> Can handle only GET method of HTTP
=> Applicable to method only
@GetMapping is a specific type of @RequestMapping(method = RequestMethod.GET). Both supports consumes
Comments
@GetMappingis the shortcut for@RequestMapping(method = RequestMethod.GET)@RequestMappingis a class level@GetMappingis a method-levelThe
@RequestMappingannotation is used to map web requests to specific handler classes and functions. This annotations key advantage is that it may be used on both the controller class and methods.It is always advised to be specific while declaring
@RequestMappingon the controller methods as in most mapping handler classes,@Getmappingis not used.
