Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)
@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
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
.
@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