@RequestMapping Annotation in Spring

@RequestMapping is a Spring Boot annotation that is used to map a specific HTTP request method to a specific Java method. It is commonly used in RESTful web services to specify the request type (GET, POST, PUT, DELETE) and the corresponding method that will handle the request.

@RequestMapping Above Controller Class

The @RequestMapping annotation can be used at the class level to specify a base path for all the request-handling methods in the class. For example:

@RestController
@RequestMapping("/users")
public class UserController {

  @GetMapping("/{id}")
  public User getUser(@PathVariable Long id) {
    // ...
  }

  @PostMapping
  public User createUser(@RequestBody User user) {
    // ...
  }
}

In this example, the base path is /users. The getUser() method handles GET requests to /users/{id}, and the createUser() method handles POST requests to /users.

Notice in the code example above the use of @GetMapping and the @PostMapping annotations? These are also request mapping annotations that are used above method names. There are also @PutMapping and @DeleteMapping. To learn more, read the following tutorial: @PostMapping, @GetMapping, @PutMapping, @DeleteMapping annotations.

@RequestMapping Annotation Above Method Name

You can also use @RequestMapping annotation above the method name that you want to map to an HTTP request. You can specify the request type by using the method attribute like this:

@RequestMapping(method=RequestMethod.GET)
public void getMethod() {
  // code here
}

Specify URL Path

Using the value attribute, you can also specify a path for your method. This path will be appended to the base URL of your application. For example:

@RequestMapping(value="/api/users", method=RequestMethod.GET)
public void getUsers() {
  // code here
}

This will map the GET request to the base URL of your application + “/api/users“.

Specify Request Parameters

You can also specify request parameters in your @RequestMapping annotation. For example:

@RequestMapping(value="/api/users", method=RequestMethod.GET, params="id")
public void getUserById(@RequestParam("id") int id) {

 // code here

}

This will map the GET request to the base URL of your application + “/api/users”, and will pass the value of the “id” request parameter to the method as an integer.

Specify Headers

You can also specify headers in your @RequestMapping annotation. For example:

@RequestMapping(value="/api/users", method=RequestMethod.GET, headers="X-API-KEY")
public void getUsersWithApiKey(@RequestHeader("X-API-KEY") String apiKey) {

// code here

}

This will map the GET request to the base URL of your application + “/api/users”, and will pass the value of the “X-API-KEY” request header to the method as a string.

Conclusion

By using the @RequestMapping annotation, you can easily map HTTP requests to specific methods in your Spring Boot application. This is a powerful tool for building RESTful web services, as it allows you to specify exactly how your application should handle different types of requests.

To learn more, check out the Spring Boot tutorials page.

Happy learning!