📖 Scenario: You are building a simple web service for a bookstore. You want to let users see the list of books and add new books through the web.
🎯 Goal: Create a REST controller in Spring Boot that handles HTTP requests to get all books and add a new book. This will show why REST controllers are essential for connecting your backend logic to the web.
📋 What You'll Learn
Create a list of books as initial data
Add a configuration variable for the base URL path
Write a REST controller class with methods to handle GET and POST requests
Use Spring Boot annotations to map URLs and HTTP methods
💡 Why This Matters
🌍 Real World
REST controllers are essential in web applications to expose backend services as APIs that frontend apps or other services can use.
💼 Career
Understanding REST controllers is key for backend developers working with Spring Boot to build scalable and maintainable web services.
Progress0 / 4 steps
1
Create initial book data
Create a List<String> called books with these exact entries: "Spring Boot Basics", "RESTful APIs", "Java Programming".
Spring Boot
Hint
Use List.of() to create an immutable list with the given book titles.
2
Add base URL path configuration
Create a String variable called basePath and set it to "/api/books".
Spring Boot
Hint
This string will be used as the base URL path for the REST controller.
3
Create REST controller class with GET and POST methods
Create a public class called BookController annotated with @RestController and @RequestMapping(basePath). Inside it, write a method getBooks() annotated with @GetMapping that returns the books list. Also, add a method addBook(String book) annotated with @PostMapping that returns a string "Added: " + book.
Spring Boot
Hint
Use Spring Boot annotations to map the class and methods to HTTP requests. The @RequestBody annotation is needed to get the POST request body.
4
Complete REST controller setup
Add the necessary import statements for List and org.springframework.web.bind.annotation.RequestBody at the top of the file to complete the REST controller code.
Spring Boot
Hint
Import statements are needed for the List type and the @RequestBody annotation.
Practice
(1/5)
1. Why are REST controllers essential in a Spring Boot application?
easy
A. They manage database connections directly.
B. They style the user interface of the application.
C. They handle HTTP requests and return data in formats like JSON.
D. They compile the Java code into bytecode.
Solution
Step 1: Understand the role of REST controllers
REST controllers are designed to handle web requests and responses in a Spring Boot app.
Step 2: Identify what REST controllers return
They typically return data in formats like JSON, which is easy for other apps to use.
Final Answer:
They handle HTTP requests and return data in formats like JSON. -> Option C
Quick Check:
REST controllers = handle HTTP + return JSON [OK]
Hint: REST controllers handle web requests and send JSON responses [OK]
Hint: @RestController auto-converts POJOs to JSON, no extra annotation needed [OK]
Common Mistakes:
Adding unnecessary @ResponseBody on methods inside @RestController
Thinking POJO must be @Entity for REST response
Changing return type to String unnecessarily
5. You want to create a REST controller that returns a list of users as JSON. Which approach correctly ensures the response is JSON and accessible at "/users" endpoint?
@RestController
public class UserController {
public List<User> getUsers() {
return List.of(new User("Bob", 25), new User("Carol", 28));
}
}
hard
A. Return a String with JSON manually instead of List<User>
B. Change @RestController to @Controller and add @ResponseBody on getUsers()
C. Annotate User class with @RestController
D. Add @GetMapping("/users") above getUsers() method
Solution
Step 1: Identify missing mapping annotation
The method lacks @GetMapping, so it won't be exposed as an endpoint.
Step 2: Confirm correct annotation for REST endpoint
Adding @GetMapping("/users") exposes the method at /users and returns JSON automatically.
Final Answer:
Add @GetMapping("/users") above getUsers() method -> Option D