A REST controller is a special type of controller in Spring Boot that handles HTTP requests and sends responses in a RESTful way, usually returning data as JSON or XML.
Click to reveal answer
beginner
Why do we use REST controllers instead of regular controllers?
REST controllers simplify building APIs by automatically converting Java objects to JSON/XML and handling HTTP methods like GET, POST, PUT, DELETE, making communication with clients easy and clear.
Click to reveal answer
intermediate
How do REST controllers improve client-server communication?
They provide a clear and standardized way to exchange data over HTTP, making it easy for clients like web browsers or mobile apps to understand and use the data sent by the server.
Click to reveal answer
beginner
What annotation marks a class as a REST controller in Spring Boot?
The @RestController annotation marks a class as a REST controller, combining @Controller and @ResponseBody to simplify REST API development.
Click to reveal answer
intermediate
How do REST controllers help with separation of concerns?
They separate the web layer from business logic by focusing only on handling HTTP requests and responses, making the code cleaner and easier to maintain.
Click to reveal answer
Which annotation is used to create a REST controller in Spring Boot?
A@RestController
B@Controller
C@Service
D@Repository
✗ Incorrect
The @RestController annotation is specifically designed for REST APIs, combining @Controller and @ResponseBody.
What format do REST controllers usually return data in?
AJSON or XML
BHTML
CPlain text only
DBinary files
✗ Incorrect
REST controllers typically return data in JSON or XML formats to be easily consumed by clients.
Why are REST controllers important for client-server communication?
AThey handle database connections
BThey create user interfaces
CThey manage server hardware
DThey provide a standard way to exchange data over HTTP
✗ Incorrect
REST controllers standardize how data is sent and received over HTTP, making communication clear and consistent.
Which HTTP methods do REST controllers commonly handle?
ACONNECT, TRACE
BGET, POST, PUT, DELETE
COPTIONS only
DHEAD only
✗ Incorrect
REST controllers handle common HTTP methods like GET, POST, PUT, and DELETE to perform CRUD operations.
How do REST controllers help keep code clean?
ABy mixing UI and business logic
BBy writing all code in one file
CBy separating HTTP handling from business logic
DBy ignoring HTTP requests
✗ Incorrect
REST controllers focus on HTTP requests and responses, letting business logic stay separate for better code organization.
Explain why REST controllers are essential in Spring Boot applications.
Think about how your app talks to other apps or frontends.
You got /4 concepts.
Describe the role of the @RestController annotation and how it differs from @Controller.
Focus on what each annotation does with the response.
You got /4 concepts.
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