RestController annotation in Spring Boot

RestController annotation in Spring Boot

The @RestController annotation is a specialized version of the `@Controller` annotation in Spring Framework, introduced to simplify the creation of RESTful web services in Spring Boot applications. It is a convenience annotation that combines `@Controller` and `@ResponseBody`:

  • `@Controller`: Marks a class as a Spring MVC controller, capable of handling web requests.
  • `@ResponseBody`: Indicates that the return value of methods should be written directly to the HTTP response body, instead of being interpreted as a view name.

By using `@RestController`, you eliminate the need to annotate each method with `@ResponseBody`, making it easier to build RESTful APIs that return JSON or XML responses.

Key Features

1. Simplified Syntax: Automatically includes `@ResponseBody` behavior, so you don’t need to annotate each method separately.

2. Convenient for RESTful Services: Ideal for creating RESTful endpoints that handle HTTP requests and return data in JSON or XML format.

3. Integration with Spring Boot: Works seamlessly with other Spring Boot features and components.

RestController

Example

1. Basic Usage
1. Create a REST Controller Class:
   - This example demonstrates a simple REST controller that handles HTTP GET requests and returns a JSON response.

   ```java
   package com.example.demo;

   import org.springframework.web.bind.annotation.GetMapping;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RestController;

   @RestController
   @RequestMapping("/api")
   public class ApiController {

       @GetMapping("/greeting")
       public String greeting() {
           return "Hello, World!";
       }
   }
   ```

Explanation:

  • @RestController indicates that this class is a RESTful controller.
  • @RequestMapping("/api") specifies the base URL path for the controller.
  • @GetMapping("/greeting") maps HTTP GET requests to the /api/greeting endpoint.
  • The greeting() method returns a plain string response that is automatically serialized to JSON.

Returning a POJO
2. Returning a POJO

1. Create a POJO Class:
   - Define a simple Java class to represent a response object.

   ```java
   package com.example.demo;

   public class Greeting {

       private String message;

       public Greeting(String message) {
           this.message = message;
       }

       public String getMessage() {
           return message;
       }

       public void setMessage(String message) {
           this.message = message;
       }
   }
   ```

Example
2. Update REST Controller to Return POJO:
   - Modify the controller to return an instance of the `Greeting` class.

   ```java
   package com.example.demo;

   import org.springframework.web.bind.annotation.GetMapping;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RestController;

   @RestController
   @RequestMapping("/api")
   public class ApiController {

       @GetMapping("/greeting")
       public Greeting greeting() {
           return new Greeting("Hello, World!");
       }
   }
   ```

Explanation

The greeting() method now returns a Greeting object. Spring Boot automatically converts this object to JSON format and sends it as the HTTP response.

Conclusion

  • @RestController Annotation: Simplifies the creation of RESTful web services by combining @Controller and @ResponseBody.
  • Usage: Ideal for creating APIs that return JSON or XML responses directly from controller methods.
  • Examples:
  • Basic Usage: Returns a plain string response.
  • Returning a POJO: Automatically converts a Java object to JSON.

Homepage

Readmore