Spring Boot @RestController Example

1. Introduction

Spring Boot's @RestController annotation provides a seamless way to build RESTful web services. This annotation simplifies the development process by merging the functionality of @Controller and @ResponseBody, enabling developers to create web services with less boilerplate code and more focus on business logic.

Key Points:

1. The @RestController annotation is a key element in creating RESTful web services in Spring Boot applications.

2. It eliminates the need to annotate every method with @ResponseBody, which indicates that the return value of the method should be used as the response body of the request.

3. @RestController handles HTTP requests by using methods annotated with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, etc.

2. Implementation Steps

1. Create a new Spring Boot project.

2. Define a new controller class annotated with @RestController.

3. Create request handler methods within the controller using the appropriate HTTP method mapping annotations.

4. Start the Spring Boot application and test the endpoints.

3. Implementation Example

Here is the complete code that demonstrates the usage of @RestController annotation:
// Step 1: Define a new Spring Boot application class
@SpringBootApplication
public class RestControllerApplication {

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

    // Step 2: Create a controller class annotated with @RestController
    @RestController
    public class GreetingController {

        // Step 3: Define a GET method to handle the greeting
        @GetMapping("/greet")
        public Greeting greet(@RequestParam(value = "name", defaultValue = "World") String name) {
            return new Greeting(counter.incrementAndGet(), String.format("Hello, %s!", name));
        }

        // Additional methods can be defined here
    }

    // Step 4: Create a simple domain class to represent the Greeting
    public static class Greeting {

        private final long id;
        private final String content;

        public Greeting(long id, String content) {
            this.id = id;
            this.content = content;
        }

        // Getters and setters
        public long getId() {
            return id;
        }

        public String getContent() {
            return content;
        }
    }
}

Output:

Spring Boot @RestController Example

Explanation:

1. @SpringBootApplication is used to mark the main class of a Spring Boot application and automatically configure Spring.

2. @RestController marks the class as a controller where each method returns a domain object instead of a view.

3. @GetMapping("/greet") maps HTTP GET requests to the greet method.

4. new Greeting(counter.incrementAndGet(), String.format("Hello, %s!", name)) creates a new instance of the Greeting class with a unique identifier and a personalized greeting message.

5. The output is a JSON representation of the Greeting object, which includes an id and a content attribute.


Comments