Spring Boot @ResponseBody Example

1. Introduction

The @ResponseBody annotation in Spring Boot is a powerful annotation used within a controller to indicate that the return value of a method should be used as the body of the response to the client. This is especially common when building RESTful web services where you need to send JSON or XML directly to the client.

Key Points:

1. @ResponseBody tells Spring that the result of a handler method should be written directly to the body of the response, not returned as a view name.

2. It's useful for returning data objects that can be automatically converted into JSON/XML.

3. @ResponseBody can be used on the method level or on the return type level.

2. Implementation Steps

1. Set up a Spring Boot project with a web starter dependency.

2. Create a controller class using the @Controller annotation.

3. Inside the controller class, create handler methods annotated with @ResponseBody.

4. Use Spring's RestTemplate or test tools like Postman to test the responses from these methods.

3. Implementation Example

Here is the complete code that demonstrates the usage of @ResponseBody annotation:
// Step 2: Create a controller class
@Controller
public class SimpleController {

    // Step 3: Create a handler method and use @ResponseBody to send a response body directly
    @GetMapping("/stringResponse")
    @ResponseBody
    public String getStringResponse() {
        // Step 3: Return the response body content as a String
        return "Direct String Response";
    }

    // Another handler method returning a complex object as JSON
    @GetMapping("/jsonResponse")
    @ResponseBody
    public MyData getJsonResponse() {
        return new MyData("Data", 42);
    }
}

// A simple POJO for demonstrating a complex JSON response
class MyData {
    private String name;
    private int value;

    // Constructor, Getters and Setters...
    public MyData(String name, int value) {
        this.name = name;
        this.value = value;
    }

    // getters and setters...
}

// Step 4: Define the main application class
@SpringBootApplication
public class ResponseBodyExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ResponseBodyExampleApplication.class, args);
    }
}

Output:

// For the /stringResponse endpoint:
Direct String Response
// For the /jsonResponse endpoint, the output will be:
{
    "name": "Data",
    "value": 42
}

Explanation:

1. @Controller designates SimpleController as a controller in the Spring MVC framework.

2. @GetMapping("/stringResponse") maps the GET request to the getStringResponse method.

3. @ResponseBody on getStringResponse indicates that the return value should be written directly to the response body.

4. @GetMapping("/jsonResponse") along with @ResponseBody tells Spring to return the MyData object as JSON in the response body.

5. The MyData class is a simple POJO with a name and a value, which will be automatically converted to JSON by Spring.

6. ResponseBodyExampleApplication is the main application class that starts the Spring Boot application.

7. When a request is made to /stringResponse, it responds with a simple string. A request to /jsonResponse receives a JSON object representing the MyData object.


Comments