Difference between RequestMapping and PostMapping

0

I was looking at requests from Spring Boot and I saw that you can make a request POST , in two ways:

//primeira forma
@RequestMapping(value = "/dev", method = RequestMethod.POST)
    public ResponseEntity<String> dev(@RequestParam("nome") String nome) {
    return new ResponseEntity<String>(nome, HttpStatus.OK);
}

//segunda forma
@PostMapping("/dev")
    public ResponseEntity<String> nome(@RequestParam("nome") String nome) {
    return new ResponseEntity<String>(nome, HttpStatus.OK);
}

I tested with Postman and both returned the same result.

  • What's the difference between these two forms?

  • Is there any limitation on either?

  • Which is more appropriate when you are going to send data from a form, example?

asked by anonymous 09.11.2018 / 19:42

2 answers

1

@RequestMapping is the annotation traditionally used to implement URL handler, it supports Post, Get, Put, Delete and Pacth methods.

@PostMapping is a simpler way to implement the URL handler of the @RequestMapping annotation with the Post method. In the @PostMapping implementation it is annotated with @RequestMapping specifying the Post method, can be seen in link :

@Target(value=METHOD)
 @Retention(value=RUNTIME)
 @Documented
 @RequestMapping(method=POST)
public @interface PostMapping

Other types of requests also have simpler forms: @GetMapping , @PutMapping , @DeleteMapping and @PatchMapping , all internally use the @RequestMapping annotation, specifying the request type.

There is no limitation of one form or another, only in the second form you do not need to specify the type of request, because the annotation itself specifies, thus having a simpler syntax.

    
09.11.2018 / 21:24
0

Both do the same job. The difference is that @PostMapping is part of a predefined group of compound annotations that internally use @RequestMapping . These annotations act as shortcuts that serve to simplify the mapping of HTTP methods and to more concisely express the methods of manipulation. The advantage of composite annotations is in the semantic aspect, because with them the code gets cleaner and easier to read.

    
09.11.2018 / 21:34