Spring boot 404 error custom error response ReST
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
A REST API should return a structured JSON response for 404 errors instead of an HTML error page. Clients, gateways, and monitoring tools all depend on a predictable error contract. In Spring Boot, the main design choice is to separate two different 404 cases: a request for a route that does not exist, and a request for a real route whose target resource was not found.
Distinguish Route 404 From Resource 404
These two cases use the same HTTP status code but mean different things.
- route 404: no controller mapping matches the request path
- resource 404: the controller exists, but the requested entity does not
That distinction matters because API clients often need different messages or handling for each one.
Handle Missing Domain Resources Explicitly
For domain-level not-found cases, define your own exception and handle it in @RestControllerAdvice.
This makes resource-level errors explicit and easy to test.
Handle Unknown Routes as JSON Too
Unknown routes are different because the request never reaches your controller. For that case, configure Spring MVC to throw an exception for missing handlers.
Then handle NoHandlerFoundException in the same advice layer.
Now unmapped endpoints return JSON instead of a default HTML response.
ProblemDetail Is a Good Modern Option
If you are on a newer Spring stack, ProblemDetail is a good standard format.
The exact schema is less important than consistency across the whole API surface.
Test the Contract End to End
Custom error handling is part of the API contract, so test it with MockMvc or the equivalent HTTP-level tooling.
That keeps future framework upgrades from silently changing your 404 payload shape.
Common Pitfalls
- Handling missing resources but forgetting unmapped routes.
- Returning HTML for some 404 cases and JSON for others.
- Using one generic message for every not-found scenario.
- Skipping tests for the error payload contract.
- Forgetting that gateways or proxies may rewrite error responses upstream.
Summary
- A REST API should return structured JSON for 404 responses.
- Route-level 404 and resource-level 404 are different cases.
- Use
@RestControllerAdviceto centralize the error contract. - Enable
NoHandlerFoundExceptionhandling if you want JSON for unknown routes. - Keep the payload shape consistent so clients can rely on it.
Related reading
- Spring Boot Actuator / Swagger
- Spring Boot Adding Http Request Interceptors
- Spring Boot Adding Http Request Interceptors
- Spring Boot Cloud Zuul Proxy 404 Error
- Spring Boot + Kafka + Kerberos configuration
- Spring Boot / Kafka Json Deserialization - Trusted Packages
- Spring Boot & Kafka, Producer thrown exception with key=''null''
- Spring Boot access static resources missing scr/main/resources

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.