Spring MVC Annotations
In this topic, we will learn about some of the important Spring MVC Annotations used in Spring Web applications.
Table of Contents
We can use the @Controller annotation to designate a Controller class in a Spring Web application. Normally in such scenarios, the code maps each method in the controller to a URL specified via the @RequestMapping annotation. When the UI requests the particular URL, the code invokes the appropriate method of the controller class. This executes the business logic of the application and then redirects the user to the appropriate JSP page.
Refer to this blog post How to use @Controller annotation in Spring Boot application.
Spring 4.0 introduced the @RestController annotation. So the @RestController annotation is a specialization of the Controller annotation that can be used to designate a REST service. This combines the behaviour of the @Controller and @ResponseBody annotations. So when a method in a class is designated with this @ResponseBody annotation, it returns the REST response directly to the client instead of redirecting the user to an HTML or JSP page.
Refer to this blog post How to use @RestController annotation in Spring Boot application.
The @ResponseBody annotation converts the Java object returned by a controller method into the appropriate type (XML or JSON). This uses the Content-Type specified in the HTTP request header and writes it to the Response stream. So normally, if our method returns an object, this object is converted into JSON or XML format and written to the output stream directly. This can be used at the class level as well as the method level. This needs to be specified only if you create a REST service with the @Controller annotation. If we use the @RestController annotation, then the @ResponseBody annotation can be skipped since the @RestController annotation is a convenience controller which combines the behaviour of the @Controller and @ResponseBody annotations.
We should use the @RequestBody annotation when the client application sends an object data type as part of the HTTP request. So when we specify this annotation, Spring automatically converts the object in the request (which would be in XML or JSON format) to a Java object.
We can use the @RequestMapping annotation to map a method in a @Controller class to an appropriate HTTP method. This also specifies the URL that the method maps to. This can be specified at the class as well as method level. If the @RequestMapping annotation is specified at the class level, all methods in the class, map to the same URL specified with this annotation. If the @RequestMapping annotation is specified at the method level, the method-level annotation overrides the class-level annotation.
Refer to this blog post How to use @RequestMapping annotation in Spring Boot application.
The @GetMapping is a specialization of the @RequestMapping annotation that can be used to map to Get requests only. Just like the @GetMapping, the @PostMapping, the @PutMapping and @DeleteMapping are shortcut annotations corresponding to the HTTP POST, PUT and HTTP DELETE methods respectively.
Refer to this blog post How to use @GetMapping annotation in Spring Boot application.
We can use the @PathVariable annotation to extract request parameters that are part of the URI. So if request parameters as specified as part of the URI, then we need to have the @PathVariable annotation in the method declaration.
Refer to this blog post How to use @PathVariable annotation in Spring Boot application.
We can use the @RequestParam annotation to extract request parameters that are sent as query parameters. The query parameters are parameters specified with the “?” mark symbol after the main URL and are of the format “param-name=param-value”. So if such parameters are present in the client request, then the @RequestParam annotation should be used in the method declaration.
In this topic, we learnt about some of the important Spring MVC annotations.