InterviewSolution
| 1. |
Can you highlight the difference between @RequestMapping, @RequestParam, @RequestBody, and @PathVariable annotations? |
|
Answer» @RequestMapping: This annotation is used on methods in the controller class to specify the API path and the REST operation type via RequestMethod. This method will be responsible for serving the HTTP request to the given path and RETURN the desired response with the help of desired service classes. Example: @RequestMapping(path = "/contract/1.0/contracts", method = RequestMethod.PUT) @RequestBody: This annotation is used to bind the incoming HTTP request body to the parameter defined in the method annotated with @RequestMapping. SPRING uses HTTP Message converters to convert the HTTP request body into a defined domain object @PathVariable: This annotation is used to get the value from the HTTP URL and capture into the method arguments. Example: @RequestMapping(path = "/products/{id}",produces = "application/json") @RequestParam: This annotation is used to capture values from the HTTP URL based on keys defined in the methods. Spring parse the request parameters and put the appropriate ONES into the method arguments. Example: @RequestMapping(path = "/products/{id}",produces = "application/json") public Product getPlan (@PathVariable("id") final String planId, @RequestParam(value = "q", required = false) final String queryParameters) |
|