When learning Spring Boot, one of the most confusing things is seeing controllers written in many different ways.
Sometimes a method returns a DTO.
Sometimes ResponseEntity<T> .
Sometimes void , String , ModelAndView , CompletableFuture , Mono , and more.
At first it feels random.
But each return type has a specific purpose.
This article is a practical cheat sheet to quickly understand the most common controller patterns in Spring Boot REST APIs.
What Is a Controller?
A controller handles HTTP requests.
Its job is simple:
Receive the request
Call the service layer
Return a response
Example:
@RestController @RequestMapping ( "/api/users" ) public class UserController {
}
Enter fullscreen mode
Exit fullscreen mode
Most Common HTTP Annotations
Annotation HTTP Method Purpose
@GetMapping GET Retrieve data
@PostMapping POST Create data
@PutMapping PUT Replace/update completely
@PatchMapping PATCH Partial update
@DeleteMapping DELETE Delete data
1.…