How to get the RequestBody in an ExceptionHandler Spring REST
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Reading the request body inside a Spring @ExceptionHandler is not straightforward because the HTTP input stream is normally consumed during request processing. If you want the raw body for logging or diagnostics after an exception, you need to cache it earlier in the request lifecycle.
Why the Body Is Not Directly Available
By the time an exception reaches @ExceptionHandler, Spring may already have read the request stream to deserialize @RequestBody into a Java object. A servlet request body is not freely reusable by default, so trying to read it again often returns nothing.
That is why the usual solution is not "read it later," but "wrap and cache it before the controller consumes it."
Use ContentCachingRequestWrapper
Spring provides ContentCachingRequestWrapper, which stores request content as it is read. A filter can wrap every incoming request so the cached bytes remain available during exception handling.
This wrapper does not magically read the body up front. It caches content as other parts of the stack read it. That is usually enough for controller exceptions, validation failures, and message conversion errors.
Access the Cached Body in @ControllerAdvice
Once the request is wrapped, an exception handler can inspect the cached bytes by looking at the current HttpServletRequest.
If the body was consumed during request handling, getContentAsByteArray() returns the cached content. That gives you safe access without trying to reopen the input stream.
When You Need the Deserialized Object Instead
If your real goal is not the raw JSON string but the parsed request payload, another option is to log or store the deserialized object before risky business logic runs. That can be cleaner than working with raw bytes in a global handler.
For example, a controller can validate or log the request object immediately after binding and then call service code that may throw later. That approach avoids double work and usually produces more structured logs.
Common Pitfalls
The most common mistake is trying to call request.getInputStream() directly inside the exception handler. In many cases the stream has already been consumed, so the result is empty or triggers another error.
Another pitfall is assuming ContentCachingRequestWrapper populates itself before anything reads the body. It caches what passes through it. If nothing ever reads the body, there may be nothing cached.
Character encoding also matters. If you always decode bytes as UTF-8 but a request used a different charset, the logged body may be garbled. In real systems, prefer request.getCharacterEncoding() when available.
Finally, be careful with sensitive data. Logging the full request body can expose passwords, tokens, or personal information. In production systems, it is often better to redact specific fields or limit body logging to known-safe endpoints.
Summary
- A Spring exception handler cannot usually reread the raw request stream directly.
- Wrap requests early with
ContentCachingRequestWrapperin a filter. - Read the cached bytes from
HttpServletRequestinside@ControllerAdvice. - Consider logging the parsed request object instead of the raw body when that better fits the problem.
- Be deliberate about character encoding and sensitive-data exposure when logging request bodies.
Related reading
- How to get the SPRING Boot HOST and PORT address during run time?
- How to get the ThreadPoolExecutor to increase threads to max before queueing?
- How to get the type of T from a member of a generic class or method
- How to get the unique ID of an object which overrides hashCode()?
- How to handle AccessViolationException
- How to handle async Start errors in TopShelf
- How to get the user input in Java?
- How to get thread id from a thread pool?

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.