Remove trace field from ResponseStatusException
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If a Spring Boot error response includes a trace field, that field is coming from the application's error rendering layer, not from ResponseStatusException alone. In most cases, the quickest fix is to disable stack-trace inclusion in error responses through configuration. If you need stricter control, customize the error attributes or your exception handling response shape directly.
The Fast Configuration Fix
Spring Boot exposes a property for stack-trace inclusion:
With that setting, the default error payload no longer includes the trace in normal responses.
There are also environments where teams allow traces conditionally during development, but the production-safe default is still to keep them out of client responses.
This is why the same exception can look different between local development and production. The exception type did not change; the server's error-rendering policy did.
This is usually the right production default because stack traces are:
- Noisy for API clients
- Potentially sensitive
- Larger than necessary for standard error handling
Why ResponseStatusException Still Shows It
Throwing ResponseStatusException controls the HTTP status and reason, but the final JSON body is often produced by the framework's error-handling infrastructure. If that infrastructure is configured to include traces, you will still see them regardless of the exception type.
Example:
That does not by itself mean the response must contain a stack trace. The stack trace appears only if the error renderer is told to expose it.
Customize the Error Body
If you want tighter control, define custom error attributes:
This lets you keep the default error pipeline while removing the stack trace deliberately.
Another Option: Custom Exception Responses
For API-first services, many teams skip the default error shape entirely and use @ControllerAdvice to return a custom payload:
This gives you full control over what the client sees.
It also makes your API contract independent from framework defaults, which is useful if clients depend on a stable error schema across framework upgrades.
That approach is especially attractive when you want a stable API error contract instead of the framework's default error JSON shape.
Common Pitfalls
- Blaming
ResponseStatusExceptionitself when the trace is really added by the global error renderer. - Leaving stack traces enabled in production responses.
- Customizing exception handling in one place while default error attributes still leak traces elsewhere.
- Treating the problem as serialization-only when it is really an error-pipeline configuration issue.
Summary
- The
tracefield is usually controlled by Spring Boot error rendering, not byResponseStatusExceptionalone. - '
server.error.include-stacktrace=neveris the simplest way to remove it.' - For stronger control, customize
ErrorAttributesor return your own API error shape. - Production APIs should rarely expose stack traces to clients.
- Clean error responses come from controlling the whole error pipeline, not only the exception type.
That is usually the fastest way to reason about the problem in production systems. The exception class is only one piece of the pipeline. The renderer is the other.
Related reading
- Remove Using default security password on Spring Boot
- Removing an element from an Array Java
- Removing Java 8 JDK from Mac
- Removing whitespace from strings in Java
- Rendered manifests contain a resource that already exists. Could not get information about the resource resource name may not be empty
- Replication factor 3 larger than available brokers 1 when starting the kafka
- Rename a file using Java
- Replace carets with HTML superscript markup using Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.