REST controllers help your application talk to other programs over the internet in a simple and organized way.
When you want to create a web service that other apps or websites can use.
When you need to send or receive data in a standard format like JSON.
When building APIs for mobile apps to get or send information.
When you want to separate your backend logic from the user interface.
When you want easy ways to handle different web requests like GET or POST.
Syntax
Spring Boot
@RestController
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return"Hello World";
}
}
Use @RestController to tell Spring Boot this class handles web requests and returns data.
Use @GetMapping, @PostMapping, etc., to link methods to web URLs and HTTP methods.
Examples
This example shows a simple GET request that returns a greeting message.
Spring Boot
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return"Hi there!";
}
}
This example handles a POST request to add a new user, receiving user data in JSON format.
Spring Boot
@RestController
public class UserController {
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// logic to save user
return user;
}
}
Sample Program
This Spring Boot REST controller responds to a GET request at '/welcome' with a simple welcome message.
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SimpleController {
@GetMapping("/welcome")
public String welcome() {
return"Welcome to REST Controller!";
}
}
OutputSuccess
Important Notes
REST controllers automatically convert return values to JSON or other formats for easy communication.
They help keep your code clean by separating web request handling from business logic.
Remember to use proper HTTP methods (GET, POST, PUT, DELETE) for different actions.
Summary
REST controllers make your app communicate over the web easily and clearly.
They handle web requests and send back data in formats like JSON.
Using REST controllers helps build APIs that other apps and devices can use smoothly.
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