Spring Mvc Annotations
By AmarSivas | | Updated : 2021-07-27 | Viewed : 264 times

In this quick tutorial, we will explore the most important annotations with Spring MVC. Let\'s get started to learn.
Table of Contents:
@Controller
In Spring MVC web requests should be handled by the controllers. So to write the handler classes we need to use the
@Controller
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@RequestMapping(value = "/list", produces = {"application/JSON"})
@ResponseBody
List getProdcts(){
return prodService.getPrducts();
}
}
@Controller vs @RestController
Both are mostly equal to the @Component which is used for mapping web requests. These are get scanned within the ClassPath automatically.
@RequestMapping
In the implementation of handler classes, we need to map web requests to methods in controller classes. So it is required to use the
@RestController
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@RequestMapping(value = "/list", produces = {"application/JSON"})
@ResponseBody
List getProdcts(){
return prodService.getPrducts();
}
}
@PathVariable
@RestController
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@RequestMapping(value = "product/{productId}", produces = {"application/JSON"})
@ResponseBody
Product getProdcts(@PathVariable("id") String productId){
return ProdService.getProduct(id);
}
}
@RequestParam
You might aware of request params in the Servlet. So the same concept can be achieved by the
@RestController @RequestMapping("/products") public class ProdController { @Autowired ProdService prodService; @RequestMapping(value = "product", produces = {"application/JSON"}) @ResponseBody Product getProdcts(@RequestParam String id){ return ProdService.getProduct(id); } }
@RequestBody
For accessing the body of the HTTP request @RequestBody is used. Let's see an example of @RequestBody in the Spring.
@Controller
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@ResponseBody @RequestMapping("/save")
public Description saveProducts(@RequestBody Products products){
return prodService.saveProducts(products);
}
}
@ResponseBody
For sending the HTTP response we use @ResponseBody. So here is example for the same.
@Controller
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@ResponseBody @RequestMapping("/get")
public Description getAllProducts(){
return prodService.saveProducts(products);
}
}
@ModelAttribute
In simple words, The
@Controller
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveProducts(@ModelAttribute("employee") Employee employee){
rreturn "prodView";
}
}
@ExceptionHandler
To handle exceptions we simply use the
@ExceptionHandler(value = { MyResourceNotFoundException.class })
protected ResponseEntity handleNotFound(final RuntimeException ex, final WebRequest request) {
final String bodyOfResponse = "This should be application specific";
String error = "Product not found.";
return buildResponseEntity(new Error(HttpStatus.NOT_FOUND,error,ex) );
}
@ResponseStatus
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Product does not existed")
public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(int id) {
super();
}
}
@RequestHeader
@Controller
@RequestMapping("/products")
public class ProdController {
@Autowired ProdService prodService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveProducts(@RequestHeader(value="User-Agent") String userAgent){
rreturn "prodView";
}
}