<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh" -->

---
title: How to Get The RequestBody and ResponseBody in...
description: A step-by-step guide to logging HTTP request and response bodies in Spring MVC using HandlerInterceptor without breaking @RequestBody deserialization. The...
canonical: https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Get The RequestBody and ResponseBody in HandlerInterceptor | daily.dev
og:description: A step-by-step guide to logging HTTP request and response bodies in Spring MVC using HandlerInterceptor without breaking @RequestBody deserialization. The...
og:url: https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh
og:image: https://api.daily.dev/og/posts/TKuS42gNh.png
og:image:alt: How to Get The RequestBody and ResponseBody in HandlerInterceptor
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Get The RequestBody and ResponseBody in HandlerInterceptor

**[Baeldung](https://daily.dev/sources/baeldung)** · 6 min read · 6 upvotes · 0 comments

## Summary

A step-by-step guide to logging HTTP request and response bodies in Spring MVC using HandlerInterceptor without breaking @RequestBody deserialization. The approach wraps the request and response in ContentCachingRequestWrapper and ContentCachingResponseWrapper inside a OncePerRequestFilter, then reads the cached bytes in afterCompletion() once the HttpMessageConverter has finished. The tutorial covers the Spring MVC execution chain, the one-shot stream nature of HTTP bodies, a sample REST API, filter and interceptor implementation, WebMvcConfigurer registration, and MockMvc integration testing with log output assertions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://feeds.feedblitz.com/~/960269651/0/baeldung>

## Questions this post answers

### Why does @RequestBody deserialization fail if a filter reads the request body first in Spring MVC?

Request and response bodies are represented as streams that can only be read once; if a filter or interceptor consumes the stream before it reaches the HttpMessageConverter, the converter receives an empty stream and @RequestBody deserialization fails. Wrapping the request with ContentCachingRequestWrapper buffers the bytes so they remain available after the first read.

_daily.dev surfaces practical Spring MVC fixes like this for developers debugging request body issues._

### Where should I log the response body when using HandlerInterceptor in Spring MVC?

Log it in afterCompletion(), not preHandle(), because by that point both the controller and the HttpMessageConverter have already run, so the ContentCachingResponseWrapper cache is fully populated. In preHandle(), the response body isn't written yet, so trying to read it there would return nothing useful.

_Developers structuring HandlerInterceptor logic can track patterns like this via daily.dev._

### How does ContentCachingRequestWrapper capture the request body without breaking @RequestBody binding?

ContentCachingRequestWrapper doesn't eagerly copy the request body; it only fills its internal cache when something actually reads the input stream, which happens naturally when Spring's HttpMessageConverter deserializes the @RequestBody. This lets a filter wrap the request early while still allowing normal deserialization to populate the cache for later logging.

_daily.dev helps backend developers keep up with Spring MVC request handling techniques like this._

## Similar posts on daily.dev

- [Testing Spring MVC HandlerInterceptor](https://daily.dev/posts/testing-spring-mvc-handlerinterceptor-3iziegemt) · Baeldung · 5 upvotes · 0 comments
- [Handling Feign GET Requests With a Body](https://daily.dev/posts/handling-feign-get-requests-with-a-body-3qeddbuy1) · Baeldung · 2 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Get The RequestBody and ResponseBody in HandlerInterceptor","url":"https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh"},"datePublished":"2026-07-13T15:47:05.746Z","dateModified":"2026-09-13T18:39:11.215Z","description":"A step-by-step guide to logging HTTP request and response bodies in Spring MVC using HandlerInterceptor without breaking @RequestBody deserialization. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/47b54e24b3c193fdbb083fc322bb2b36?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/47b54e24b3c193fdbb083fc322bb2b36?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Baeldung","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Baeldung","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/52cccfb454d946c4b7dfa5e816f9e576","url":"https://daily.dev/sources/baeldung"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":6},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Baeldung","item":"https://daily.dev/sources/baeldung"},{"@type":"ListItem","position":3,"name":"How to Get The RequestBody and ResponseBody in HandlerInterceptor"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-get-the-requestbody-and-responsebody-in-handlerinterceptor-tkus42gnh#faq","mainEntity":[{"@type":"Question","name":"Why does @RequestBody deserialization fail if a filter reads the request body first in Spring MVC?","acceptedAnswer":{"@type":"Answer","text":"Request and response bodies are represented as streams that can only be read once; if a filter or interceptor consumes the stream before it reaches the HttpMessageConverter, the converter receives an empty stream and @RequestBody deserialization fails. Wrapping the request with ContentCachingRequestWrapper buffers the bytes so they remain available after the first read. daily.dev surfaces practical Spring MVC fixes like this for developers debugging request body issues."}},{"@type":"Question","name":"Where should I log the response body when using HandlerInterceptor in Spring MVC?","acceptedAnswer":{"@type":"Answer","text":"Log it in afterCompletion(), not preHandle(), because by that point both the controller and the HttpMessageConverter have already run, so the ContentCachingResponseWrapper cache is fully populated. In preHandle(), the response body isn't written yet, so trying to read it there would return nothing useful. Developers structuring HandlerInterceptor logic can track patterns like this via daily.dev."}},{"@type":"Question","name":"How does ContentCachingRequestWrapper capture the request body without breaking @RequestBody binding?","acceptedAnswer":{"@type":"Answer","text":"ContentCachingRequestWrapper doesn't eagerly copy the request body; it only fills its internal cache when something actually reads the input stream, which happens naturally when Spring's HttpMessageConverter deserializes the @RequestBody. This lets a filter wrap the request early while still allowing normal deserialization to populate the cache for later logging. daily.dev helps backend developers keep up with Spring MVC request handling techniques like this."}}]}
```

