Sub topic 3
Tuning the default error response with server.error.* properties
This chapter assumes you know how a Spring Boot REST controller works and what the common HTTP status codes mean. The previous chapter showed what Spring Boot returns when nothing handles an exception. Here you will learn which parts of that response you can change with configuration alone, and which settings are safe to change.
What the properties control
Spring Boot registers BasicErrorController and maps it to /error. When a request fails, the servlet container forwards it to that path. The controller asks an ErrorAttributes bean (by default DefaultErrorAttributes) for a map of values, then renders the map as JSON or as the whitelabel HTML page.
The server.error.* properties bind to ErrorProperties. They control two things:
- Which optional fields
DefaultErrorAttributesadds to the map. - Where the error page lives and whether the whitelabel page is shown.
Without any configuration, a failing request from a JSON client gets a body like this:
{
"timestamp": "2026-12-01T10:18:40.750+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/boom"
}The four fields shown above are always present. Everything else is opt-in.
The property reference
| Property | Values | Default | Effect |
|---|---|---|---|
| `server.error.include-message` | `never`, `always`, `on_param` | `never` | Adds the `message` field with the exception message. |
| `server.error.include-stacktrace` | `never`, `always`, `on_param` | `never` | Adds a `trace` field with the full stack trace. |
| `server.error.include-binding-errors` | `never`, `always`, `on_param` | `never` | Adds an `errors` field listing validation errors. |
| `server.error.include-exception` | `true`, `false` | `false` | Adds an `exception` field with the exception class name. |
| `server.error.path` | any path | `/error` | The path the container forwards failed requests to. |
| `server.error.whitelabel.enabled` | `true`, `false` | `true` | Switches the HTML fallback page on or off. |
The three include-* properties that take never, always or on_param are enums. The values are case-insensitive, so ON_PARAM and on_param both work.
Including the message
Since Spring Boot 2.3 the exception message is hidden by default. The reason is practical. Messages often contain table names, file paths or user data, and none of that belongs in a response that anyone can read.
Suppose a controller throws a plain exception:
package com.example.orders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class BoomController {
@GetMapping("/boom")
String boom() {
throw new IllegalStateException("Order 7 exploded");
}
}With server.error.include-message=always, the response gains a field:
{
"timestamp": "2026-12-01T10:18:40.750+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "Order 7 exploded",
"path": "/boom"
}The same setting applies to the reason of a ResponseStatusException and to the message of an exception annotated with @ResponseStatus. If you throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Customer 5 was not found") and leave the default, clients see a 404 with no explanation. That surprises many developers the first time.
Using on_param for debugging
on_param includes a field only when the client asks for it with a query parameter. Each property has its own parameter name:
| Property | Query parameter |
|---|---|
| `include-message` | `message` |
| `include-stacktrace` | `trace` |
| `include-binding-errors` | `errors` |
server.error.include-message=on_param
server.error.include-stacktrace=on_paramWith that configuration:
curl "http://localhost:8080/boom?message=true"
curl "http://localhost:8080/boom?trace=true"The first call returns the message. The second returns a trace field containing the whole stack trace as one string. A request without the parameter gets neither.
on_param is convenient on a developer machine and a bad idea anywhere reachable from the internet. Any caller can add the parameter. Treat it as a local debugging tool, not as an access control.
Binding errors and the exception class
include-binding-errors matters for requests that fail validation, for example a @Valid @RequestBody that does not pass Bean Validation. With always, the default body gets an errors array with one entry per violation, including internal object names, codes and rejected values.
server.error.include-binding-errors=alwaysThis output is verbose and shaped by Spring's ObjectError type, not by a contract you designed. For public APIs, it is better to build your own field-level response in a handler, which a later chapter covers. Use this property when you need a quick improvement and can accept the shape.
include-exception=true adds the fully qualified class name, such as java.lang.IllegalStateException. It tells an attacker which libraries and which failure modes your code has, and it ties clients to internal class names. Leave it off for any API you publish.
Moving or disabling the error page
server.error.path changes the forward target. You rarely need this, but it helps when /error clashes with a real route in your application:
server.error.path=/problemBoot also registers the matching path for the container's error page, so you do not need to configure Tomcat yourself.
Browsers send Accept: text/html, so they get an HTML page instead of JSON. The default page is the whitelabel page, a plain page titled "Whitelabel Error Page". You have three options:
- Keep it, which is fine for an API-only service that no browser calls.
- Provide static pages such as
src/main/resources/public/error/404.html, or templates such astemplates/error/5xx.html. The status code or a series like4xxpicks the file. - Disable it with
server.error.whitelabel.enabled=false.
server.error.whitelabel.enabled=falseDisabling the whitelabel page does not remove error handling. The container's own error page takes over, so a Tomcat-styled page appears for browser clients. If you want no HTML at all, define a view named error or send Accept: application/json.
The same settings in YAML
server:
error:
include-message: never
include-stacktrace: never
include-binding-errors: never
include-exception: false
whitelabel:
enabled: falseSpring profiles let you relax the settings only in development. Put the permissive values in application-dev.yml and keep the strict ones in application.yml:
# application-dev.yml
server:
error:
include-message: always
include-stacktrace: on_paramRun with --spring.profiles.active=dev locally. Production, which never activates dev, stays on the strict defaults.
Changing the fields themselves
The properties only toggle optional fields. To rename a field, drop timestamp or add something of your own, replace the ErrorAttributes bean. Extending DefaultErrorAttributes keeps everything else intact:
package com.example.orders;
import java.util.Map;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
@Component
class CustomErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> attributes = super.getErrorAttributes(webRequest, options);
attributes.remove("timestamp");
attributes.put("service", "orders");
return attributes;
}
}The package org.springframework.boot.web.error for ErrorAttributeOptions applies to Spring Boot 3. Earlier versions kept it under org.springframework.boot.web.servlet.error. Because DefaultErrorAttributes is a conditional bean, your component replaces it. The include-* properties still work because BasicErrorController passes the matching ErrorAttributeOptions into your method.
Limits of this approach
Configuration properties are a blunt tool. Know where they stop:
- They shape only the fallback.
BasicErrorControllerruns when no handler resolved the exception. A@RestControllerAdvicemethod that returns its own body bypasses/error, so these properties have no effect on it. Chapters on@ExceptionHandlerandProblemDetailcover that path. - The format is not a standard. The default body is a Spring Boot convention. It is not RFC 9457, so clients cannot depend on it outside your stack.
- Errors raised outside MVC differ. Failures in servlet filters or in the container, such as a malformed URL, may produce a response that does not come from Spring at all.
- The
messagesetting is global. It cannot hide the message for one exception and show it for another. Per-exception control needs a handler.
Recommended baseline
For a service that will be reachable by other teams or the public, start from this and add handlers on top:
server.error.include-message=never
server.error.include-stacktrace=never
server.error.include-binding-errors=never
server.error.include-exception=false
server.error.whitelabel.enabled=falseThese values hide internals from clients. Your logs should keep the full detail, which a later chapter covers.
Exercise
A controller throws new ResponseStatusException(HttpStatus.NOT_FOUND, "Customer 5 was not found"). With no server.error.* configuration, a client receives a 404 body with no message field. Which property brings the reason into the response, which value would you pick for a production API, and why?
Show answer
Set server.error.include-message. The default is never, which hides the reason. always would show the text, but it applies to every exception in the application, including ones whose messages contain internal details. For production, keep never and instead handle the case in a @RestControllerAdvice or with a dedicated exception, so you control exactly which text clients see. Use on_param only in a local profile.
Exercise
You set server.error.include-stacktrace=on_param. What does a client have to send to receive the stack trace, and why is this setting unsafe on a public server?
Show answer
The client adds the query parameter trace=true, for example GET /boom?trace=true. It is unsafe because any caller can add that parameter, so the stack trace is available to everyone, not only to developers.
Exercise
You add a @RestControllerAdvice method that returns its own JSON body for IllegalStateException, then set server.error.include-message=always. Does the handler's response change? Explain.
Show answer
No. The handler resolves the exception inside Spring MVC and writes its own body, so the request is never forwarded to /error. The server.error.* properties only affect responses built by BasicErrorController and DefaultErrorAttributes.