Sub topic 2
What Spring Boot does by default
This chapter assumes you can write a simple Spring Boot REST controller and know HTTP status codes. You don't need any custom error handling yet. Here you see what an application does with an exception when you have written no handler for it.
An unhandled exception
Take a controller that fails on purpose:
package com.example.orders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class DemoController {
@GetMapping("/boom")
String boom() {
throw new IllegalStateException("Database password is hunter2");
}
}Call it with a client that asks for JSON:
curl -i -H "Accept: application/json" http://localhost:8080/boomThe response is a 500 with a body like this:
{
"timestamp": "2026-12-01T10:30:12.481+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/boom"
}The exception message is not in the body. Spring Boot leaves it out by default, so the secret stays on the server.
Where the response comes from
No Spring MVC handler claims IllegalStateException, so the exception goes back to the servlet container (Tomcat by default). Boot registers an error page at the path /error with the container. The container sends the request there again, as an internal forward. A controller called BasicErrorController serves that path.
BasicErrorController has two methods on the same path.
errorHtmlproducestext/html. Browsers get the "Whitelabel Error Page".errorreturns aResponseEntity<Map<String, Object>>, which becomes the JSON you saw above.
Spring chooses between them with normal content negotiation on the Accept header. A browser sends text/html. A tool like curl sends */* unless you set the header, and then the JSON method is used.
The map's contents come from DefaultErrorAttributes. The standard keys are timestamp, status, error and path. Boot can add message, errors, trace and exception, but it leaves them out unless you configure them. The next chapter covers the server.error.* properties that control this.
Not every error is an exception in your code
The same /error mechanism handles failures that happen before any controller runs. A request to a path that no controller or static resource matches produces a 404 through the same route:
{
"timestamp": "2026-12-01T10:31:05.102+00:00",
"status": 404,
"error": "Not Found",
"path": "/nope"
}Spring MVC translates some exceptions into status codes on its own. For example, a wrong HTTP method gives 405, and a malformed JSON body gives 400. In each case the status is set with sendError, and the container forwards to /error.
Limits of the default
The default is a safe fallback, but it is not a good API contract:
- The body is a loose map, not a documented format. Its keys can change between Boot versions.
- Every failure looks the same. A missing order and a database outage are both just a status and a generic
errorstring. - You can't attach useful data, such as the id that wasn't found.
- Stack traces are hidden from clients, but nothing here decides how errors are logged.
Exercise
A browser and a curl command without extra headers both request /missing, which no controller handles. Why can they receive different responses, and which part of Spring Boot makes the choice?
Show answer
Both requests end up at /error, handled by BasicErrorController. The browser sends Accept: text/html, so errorHtml returns the Whitelabel HTML page. curl sends */*, so the JSON method error is used. The choice is made by content negotiation on the Accept header.