Printing and PDF export are disabled for this content. View it online at Full Stack Learning Simplified.
Spring Boot REST Controllerπ± Beginner
A controller handles incoming web requests. @RestController marks a class whose methods return data (serialized to JSON) directly β the foundation of a REST API.
A basic controller
HelloController.java
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}Run the app and open http://localhost:8080/hello. @RestController = @Controller + @ResponseBody, so return values become the response body.
Returning JSON automatically
Return an object or list and Spring converts it to JSON with Jackson β no manual serialization.
UserController.java
@GetMapping("/user")
public User getUser() {
return new User(1, "Asha"); // -> {"id":1,"name":"Asha"}
}Controlling the status & headers
Wrap the body in ResponseEntity when you need a specific status code or headers.
UserController.java
@PostMapping("/users")
public ResponseEntity<User> create(@RequestBody User u) {
User saved = service.save(u);
return ResponseEntity.status(HttpStatus.CREATED).body(saved); // 201
}
@GetMapping("/users/{id}")
public ResponseEntity<User> find(@PathVariable Long id) {
return service.find(id)
.map(ResponseEntity::ok) // 200
.orElse(ResponseEntity.notFound().build()); // 404
}Tip: For a real API, keep controllers thin β they receive the request, delegate to a service, and return a response. Put business logic in the service layer, not the controller.
Spring Boot Key Takeaway: Spring Security enforces non-repudiation and access control via security filter chains, protecting against CSRF and unauthenticated endpoints.
Free preview. Sign in and subscribe to unlock all 982 lessons across 31 courses.
Free preview Β· Β© 2026 Full Stack Learning Simplified