Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @RequestMapping and @PostMapping

in the below example, I am trying to undersatnd the difference between @RequestMapping and @PostMapping. For @RequestMapping:

when i do the POST request: http://localhost:8085/call1/initparam1?val=1111 via postman, it executes correctly. but when its is proceeded by by GET request
http://localhost:8085/call1/getparam1

i do not get 1111 as a result.

For @PostMapping, when i do the POST request: http://localhost:8085/call1/initparam2/1999 via postman, it executes correctly. but when its is proceeded by by GET request
http://localhost:8085/call1/getparam1

i do not get 1999 as a result.

please explain to me what is the difference between using both annotations, as i spent time googling and researching but i could not figure out why the first example is not working.

Controller1

@Controller
@ResponseBody
@RequestMapping("/call1")
public class Call1 {

public String str = "inti";

@RequestMapping(value = "/initparam1", method = RequestMethod.POST)
public void initparam1(@RequestParam(value = "val") String val) {
    this.str = val;
}

@PostMapping(value = "/initparam2/{val}")
public void initparam2(@PathVariable String val) {
    this.str = val;
}

@RequestMapping("/getparam1")
@ResponseBody
public String getParam1() {
    return this.str;
}
}
like image 938
user2121 Avatar asked Jul 14 '19 07:07

user2121


People also ask

What is @PostMapping used for?

The POST HTTP method is used to create a resource and @PostMapping annotation for mapping HTTP POST requests onto specific handler methods. Specifically, @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.

What is the @RequestMapping annotation used for?

annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What is @PostMapping annotation in Spring boot?

@PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. POST) . @PostMapping annotated methods handle the HTTP POST requests matched with given URI expression.

What are the differences between @RequestMapping and RestController?

In case of @RestController the parameter value depicts the component name or bean name, whereas in @RequestMapping the value parameter is used to specify the path. Both are used for different purpose. If you want to specify request URI path on controller class name use @RequestMapping annotation with @RestController .


1 Answers

From the @PostMapping docs :

Specifically, @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).

So it is only convenience annotation that is more "verbose" and indicates that method annotated with it is used for handling POST HTTP requests.

I have just checked your controller methods with 2.1.4 spring boot version and your scenarios work as expected so there has to be something wrong in your configuration or the way you are sending requests.

like image 165
Michał Krzywański Avatar answered Oct 06 '22 13:10

Michał Krzywański