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

7

229

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

Oquassa answered 22/8, 2016 at 10:58 Comment(0)
P
253

@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 we can apply only on method level and RequestMapping annotation we can apply on class level and as well as on method level

Paddlefish answered 22/8, 2016 at 11:5 Comment(2)
@GetMapping supports consumes - docs.spring.io/spring-framework/docs/current/javadoc-api/org/…Anaesthesia
RequestMapping supports consumes as well: docs.spring.io/spring/docs/current/javadoc-api/org/…Depression
G
26

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 the consumes attribute like @RequestMapping.

Guarded answered 22/8, 2016 at 11:2 Comment(1)
Good news! As of 4/2017, on the Spring web page you linked to, GetMapping does now support 'consumes' and the usual RequestMapping attributes. Probably Spring added those after your post.Lavernlaverna
R
21

@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)

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

Rademacher answered 3/11, 2018 at 15:48 Comment(4)
@RequestMapping can also be applied to method.Cirrose
Yes, it can be. However, @GetMapping is more succinct and specific to the HTTP method that it targets.Rademacher
ZhaoGang makes a good point. Your answer is wrong. You can argue that the way you do things is a GOOD CONVENTION. But doesn't change the fact that your answer, as currently written, is wrong.Whizbang
@Whizbang why WOULD you ever want to use "@RequestMapping" more than once in a controller, xxxxController.java file.??? why.. Think about future maintainers.... other people too!Rademacher
C
14

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.


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

Cirrose answered 14/1, 2019 at 2:45 Comment(0)
C
2

                         `@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

Clotildecloture answered 18/2, 2022 at 14:55 Comment(0)
A
-1
  1. @RequestMapping supports consumes even with method=GET, while @GetMapping doesn't supports consumes.
  2. @RequestMapping is Method & Type level annotation, while @GetMapping is a Method level annotation

Other than that @GetMapping is same as @RequestMapping(method=RequestMethod.GET)

Accumbent answered 4/9, 2021 at 15:29 Comment(0)
M
-1
  1. @GetMapping is the shortcut for @RequestMapping(method = RequestMethod.GET)

  2. @RequestMapping is a class level

  3. @GetMapping is a method-level

4)The @RequestMapping annotation 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.

5)It is always advised to be specific while declaring @RequestMapping on the controller methods as in most mapping handler classes, @Getmapping is not used.

Mazzard answered 27/7, 2022 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.