Sub topic 3
Tuning the default error response with server.error.* properties
This chapter assumes you can build a Spring Boot REST controller, know how HTTP status codes work, and have seen the default /error response from the previous chapter. Here you will change what that default response contains by setting server.error.* properties.
What the properties control
Spring Boot's BasicErrorController builds the default error body. It reads a set of properties to decide which details go into that body. Each property controls one kind of detail.
| Property | Controls | Allowed values |
|---|---|---|
| `server.error.include-message` | The exception message in the `message` field | `never`, `always`, `on_param` |
| `server.error.include-binding-errors` | The `errors` list from validation | `never`, `always`, `on_param` |
| `server.error.include-stacktrace` | The `trace` field | `never`, `always`, `on_param` |
| `server.error.include-exception` | The exception class name in `exception` | `true` or `false` |
The on_param value includes the detail only when the request carries a matching query parameter, such as ?message=true or ?trace=true. The default for the three enum properties is never. Older Spring Boot versions defaulted to showing more, so check the version you run.
A safe configuration
The following application.properties turns on RFC 9457 problem details for Spring MVC's own exceptions. It also keeps internal details out of the default response.
# Return RFC 9457 problem details for exceptions raised by Spring MVC itself
spring.mvc.problemdetails.enabled=true
# Keep internal details out of the default /error response
server.error.include-message=never
server.error.include-binding-errors=never
server.error.include-stacktrace=never
server.error.include-exception=falseThe spring.mvc.problemdetails.enabled property is not under server.error. It affects exceptions that Spring MVC raises itself, such as a missing request parameter or an unsupported media type. It does not change the /error page for other failures.
Choosing a value for each environment
Different environments need different settings. On a developer machine, a stack trace saves time. On a public server, the same trace tells an attacker which classes and libraries you use.
- In production, use
neverfor all four properties. - In local development,
alwaysforinclude-messageandinclude-stacktraceis acceptable. Put it in a profile file such asapplication-dev.properties, so it cannot leak into production by accident. - Avoid
on_paramon any server that is reachable from outside. Any caller can add the query parameter.
# application-dev.properties
server.error.include-message=always
server.error.include-stacktrace=alwaysActivate the profile with --spring.profiles.active=dev.
How the properties relate to your own handlers
These properties apply only to the default error flow, where a request ends up at /error. If you write an @ExceptionHandler or @RestControllerAdvice that returns its own body, the properties do not affect that body. You decide what that body contains.
This means the properties act as a safety net. Even when you have a global handler, some failures still reach /error. Examples are a request to a path that does not exist, or an error raised before the request reaches Spring MVC. Setting the properties to never covers those cases.
Common mistakes
- Relying on the properties for custom bodies. Setting
include-message=neverdoes not remove the message from aProblemDetailyou build yourself. - Leaving
alwaysin a shared file. Put development-only values in a profile file. - Expecting
include-binding-errorsto shape your own validation response. It only affects the default/errorbody.
Exercise
Exercise
Your API has a @RestControllerAdvice that handles every exception you know about, and you set server.error.include-stacktrace=never. A client requests /does-not-exist. Which component builds the response body, and do the server.error.* properties apply?
Show answer
No handler method matches an unknown path, so the request is forwarded to /error. BasicErrorController builds the body, and the server.error.* properties apply, so no stack trace appears.
Exercise
A teammate sets server.error.include-message=never and expects the detail field of an OrderNotFoundException ProblemDetail from your advice class to be empty. Is the teammate right?
Show answer
No. The advice class builds its own ProblemDetail, so the property does not apply. The detail field contains whatever your handler sets.