DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Spring - @RequestMapping
YiฤŸit Erkal
YiฤŸit Erkal

Posted on

Spring - @RequestMapping

TL;DR

It is used to map web requests to Spring Controller methods.

In Spring Web applications, @RequestMapping is one of the most used annotations. HTTP requests are mapped to MVC and REST controller handler methods with this annotation.

URL handler using @RequestMapping annotation as it follows here:

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
Enter fullscreen mode Exit fullscreen mode

The alternative approach in other words possible short version is:

@GetMapping("/get/{id}")
Enter fullscreen mode Exit fullscreen mode

We can also implement other mappings mentioned below:

RequestMapping Spring Annotation

To sum up, it is a better approach to use RequestMapping or alternative mapping in class level controllers since all your requests and responses will be handled in controller. Here is a full code example:

@Controller
@RequestMapping(value = "/orders", method = RequestMethod.GET)
public class DemoController {


  @RequestMapping(value = "/{orderId}", method = RequestMethod.GET)
  @ResponseBody
  public String getOrder(@PathVariable final String orderId) {
    return "Order ID: " + orderId;
  }

  @RequestMapping(value = "/addProduct", method = RequestMethod.POST)
  public String addProductPost(@ModelAttribute("product") 
Product product) {
    // some code
}

 // other mappings
 // ...
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

โฌ‡๏ธ The only reason people scroll to the bottom...

...is because they want to read more. Create an account to bookmark, comment, and react to articles that interest you.