Stage 12 - API Error Handling with ControllerAdvice
Error handling becomes visible the first time a frontend developer asks: “What exactly should I show to the user when this request fails?” If every controller returns a different error shape, the frontend cannot build one normal error component. One endpoint returns plain text, another returns an HTML error page, the third returns a stack trace, and validation errors arrive in a completely different format. The backend may technically work, but the API is unpleasant and unreliable.
Spring gives a better model: controllers should describe successful HTTP actions, services should throw meaningful exceptions when a use case cannot be completed, and one global handler should translate those exceptions into a stable JSON error contract. That global handler is usually a class annotated with @RestControllerAdvice. It listens to exceptions thrown from controllers and turns them into ResponseEntity objects with the right HTTP status and response body.

Request flow
The flow is simple. A request reaches OrderController. The controller calls orderService.cancel(orderId). The service loads the order, checks ownership and current status, and throws OrderNotFoundException, AccessDeniedException, or OrderAlreadyShippedException when the operation cannot continue. The controller does not catch these exceptions. They move to the advice class, where each exception type is mapped to a clear status and JSON body.
| Situation | HTTP status | Meaning |
|---|---|---|
| Broken JSON or invalid field | 400 Bad Request | The client sent data the API cannot accept. |
| Order id does not exist | 404 Not Found | The requested resource is absent. |
| Order belongs to another user | 403 Forbidden | The user is known but not allowed to access it. |
| Order already shipped | 409 Conflict | The request conflicts with current business state. |
| Unexpected bug or database outage | 500 Internal Server Error | The server failed and should hide internals. |
Concrete Spring example
public record ApiError(
String code,
String message,
List<FieldErrorDto> fieldErrors
) {}
@RestControllerAdvice
class ApiExceptionHandler {
@ExceptionHandler(OrderNotFoundException.class)
ResponseEntity<ApiError> notFound(OrderNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ApiError("ORDER_NOT_FOUND", ex.getMessage(), List.of()));
}
@ExceptionHandler(OrderAlreadyShippedException.class)
ResponseEntity<ApiError> conflict(OrderAlreadyShippedException ex) {
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(new ApiError("ORDER_ALREADY_SHIPPED", ex.getMessage(), List.of()));
}
}
Validation errors
Validation errors need one more handler because Spring throws MethodArgumentNotValidException before the controller method body runs. In that handler you read field errors and return a list like field=quantity, message=must be greater than or equal to 1. This is much more useful than a generic “Bad Request”.
The important rule is that the error body is part of the API contract. Do not put random exception class names into code. Do not expose SQL messages. Do not return 200 OK with an error field inside the JSON. Use HTTP status for transport meaning and a stable code for business meaning.
Common mistakes
- Catching exceptions in every controller and returning different response shapes.
- Returning stack traces, SQL errors, or Java exception names to clients.
- Using
500for business problems such as “order already shipped”. - Returning
200 OKfor failed operations because it is easier for the controller.
Glossary
- bean
- An object managed by the Spring container.
- IoC
- Inversion of Control, a design principle where a framework manages object creation and wiring.
- dependency injection
- A technique where required dependencies are provided to an object from the outside.
- application context
- The Spring container that stores beans, configuration, and runtime wiring.
- auto-configuration
- Spring Boot behavior that configures application components based on dependencies and settings.
- starter
- A Spring Boot dependency bundle that brings related libraries and default configuration together.
- dependency management
- The practice of controlling dependency versions and relationships in a project.
- controller
- A component that handles incoming requests and returns responses or view models.
- service layer
- The application layer where business logic and use-case orchestration usually live.
- repository layer
- The application layer responsible for data access and persistence operations.
- REST controller
- A controller that exposes HTTP endpoints and usually returns JSON or another response body.
- endpoint
- A specific API address and operation that clients can call.
- request mapping
- The configuration that connects an HTTP request path and method to controller code.
- Spring Security
- The Spring project used to implement authentication, authorization, and security filters.
- authentication
- The process of proving who a user or client is.
- authorization
- The process of deciding what an authenticated user or client is allowed to do.
- filter chain
- An ordered set of web filters that process a request before it reaches the application handler.
- observability
- The ability to understand system behavior from signals such as logs, metrics, traces, and health checks.