In this tutorial, we will learn what is @RestController annotation and how to use it in a Spring Boot Application.
@RestController Annotation Overview
The @RestController annotation is a specialized version of the @Controller annotation in Spring MVC specifically designed for building RESTful web services. It combines the functionality of the @Controller and @ResponseBody annotations into a single annotation, making it convenient for creating RESTful APIs.
When you annotate a class with @RestController, it indicates that the class is a controller responsible for handling incoming HTTP requests and generating the appropriate HTTP responses. Unlike the traditional @Controller, which typically returns a view name or model for server-side rendering, the @RestController directly returns the response body in a format like JSON or XML.
Key features and behaviors of the @RestController annotation include:
Response Body Handling: The @RestController annotation eliminates the need to annotate each individual handler method with @ResponseBody. By default, all methods in the class are considered to have @ResponseBody semantics, meaning their return values are automatically serialized and sent as the response body.
Content Negotiation: @RestController handles content negotiation automatically. It determines the response format based on the Accept header of the incoming request.
Exception Handling: Similar to a regular @Controller, you can include exception-handling methods within a @RestController to handle and customize the responses for specific exceptions. These methods can be annotated with @ExceptionHandler to define how to handle different types of exceptions thrown during request processing.
Spring MVC Features: @RestController can still leverage various features provided by Spring MVC, such as request mapping, path variables, query parameters, request validation, and more. It integrates seamlessly with other Spring components and annotations to build robust and scalable RESTful services.
@RestController Annotation Example
Add the below Maven dependency to your Spring Boot project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The spring-boot-starter-web dependency provides the necessary components to develop RESTful API in a Spring Boot project, including the embedded Tomcat server, Spring Web MVC, and other required dependencies.
Next, let's create HelloWorldController and annotate it with @RestController annotation:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello/{name}")
public String sayHello(@PathVariable String name) {
return "Hello, " + name + "!";
}
}
In the above example, the HelloWorldController class is annotated with @RestController, which indicates that it is a controller class responsible for handling HTTP requests and returning JSON responses.
The @GetMapping annotation is used to map the /hello/{name} URL path to the sayHello method. The @PathVariable annotation is used to extract the {name} path variable and pass it as a method parameter. The method returns a simple greeting message.
Next, run your Spring boot application and send a request to the REST API. For example, you can send a GET request to http://localhost:8080/hello/Ramesh to get a response of "Hello, Ramesh!".
Conclusion
The @RestController annotation is used in Spring Boot to indicate a class as a RESTful controller. It combines the functionality of the @Controller and @ResponseBody annotations, allowing for simplified RESTful API development.Check out the CRUD REST API development tutorial: Spring Boot 3, MySQL, Spring Data JPA, Hibernate CRUD REST API Tutorial
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment