How to modify request body before reaching controller in spring boot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot controllers usually read request bodies once, so body transformation must happen before the controller consumes input. This is common for tasks like payload normalization, encrypted body decoding, schema adaptation, and tenant metadata injection. The safest technique is a filter plus request wrapper that replaces the readable body stream.
Core Sections
Why a wrapper is required
HttpServletRequest input streams are one time read by default. If middleware reads the body without wrapping, controllers may see empty content. A custom HttpServletRequestWrapper lets you store modified bytes and expose them through getInputStream and getReader.
Build a filter that modifies JSON body
Use OncePerRequestFilter so execution happens once per request dispatch. Read original bytes, transform, then forward wrapped request.
Register the filter with a FilterRegistrationBean when ordering matters relative to security and logging filters.
Alternative with RequestBodyAdvice
If you only need to transform objects after deserialization and before controller method execution, RequestBodyAdvice can be cleaner than raw byte manipulation.
Use filter plus wrapper for raw transport level changes. Use body advice for object level policy changes.
Common Pitfalls
- Reading request body in a filter without wrapping and forwarding modified bytes. Controllers then receive empty body.
- Transforming every content type as JSON without checks. Gate logic by content type and path.
- Ignoring filter order relative to security and tracing filters. Set explicit order in registration.
- Performing expensive parsing in global filters unnecessarily. Narrow scope to matching endpoints.
- Mixing transport and domain transformations in one class. Separate byte level and object level concerns.
Summary
- Request body modification must happen before controller consumption.
- A custom
HttpServletRequestWrapperis the core technique for replacing request body bytes. OncePerRequestFilterprovides predictable interception points.RequestBodyAdviceis useful for post deserialization object transformation.- Clear layering and filter ordering prevent subtle production bugs.
Additional implementation notes: verify behavior with realistic tests, document assumptions, and keep boundary handling explicit so long term maintenance stays predictable.
Related reading
- How to negate a method reference predicate
- How to obtain all subsequence combinations of a String in Java, or C etc
- How to obtain JNI interface pointer JNIEnv for asynchronous calls
- How to override application.properties during production in Spring-Boot?
- How to parse a JSON string into JsonNode in Jackson?
- How to parse JSON in Java
- How to pass a function as a parameter in Java?
- How to pass a MapString, String with application.properties

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.