@RequestMapping
Using @RequestMapping you can map a specific path to specific method in the controller to handle the request. You can specify multiple attributes in the @RequestMapping annotation based on your requirement.
Value attribute specifies the path for handling the request and method attribute specifies the
type of Http method this method will handle.
@Controller
public class WelcomeController {
@RequestMapping(value="/welcome", method=RequestMethod.GET, produces = "application/json")
public String welcome() {
return "welcome";
}
}
So, whenever a /welcome path call happens, in backend it call the welcome() method of WelcomeController
produces attribue specifies what type of content this method produces in response.
You can also provide @RequestMapping("/home") at the class level for the common base path for all methods in the controller.
Here in this case if we provide @RequestMapping("/home") at class level then the path become /home/welcome for the welcome() method.
We can also specify multiple paths in the value attribute, multiple method in the method attribue.
Following attributes we can use in the RequestMapping annotation:
path ="/welcome" or {"/welcome","/home"}
path to serve the request
@RequestMapping("/welcome") is similar to @RequestMapping(path="/welcome")
value="/welcome" or {"/welcome","/home"}
multiple paths values to handle the request
remember: path is alias for value and value alias for the path
method=RequestMethod.GET or { RequestMethod.POST, RequestMethod.DELETE }
the type of http method
produces = "application/json" or {"text/plain", "application/*"}
the method produces the type of data
consumes= "application/json" or {"text/plain", "application/*"}
the method consumes the type of data
headers = "content-type=text/*"
To specify headers content
params = "param1" or {"param1","param2"}
Request parameters
Examples
@RequestMapping(value={"/employee/remove","/employee/delete"},produces = "application/json",consumes= "application/json",params = "param1")
@RequestMapping(value = { "remove", "delete" }, method = { RequestMethod.POST, RequestMethod.DELETE })
@GetMapping: Annotation to map the Http GET method on specific handler method in controller.
@PostMapping: Annotation to map the Http POST method on specific handler method in controller.
@PutMapping: Annotation to map the Http PUT method on specific handler method in controller.
@DeleteMapping: Annotation to map the Http DELETE method on specific handler method in controller.