How to avoid HttpMessageNotReadableException when using ContentCachingRequestWrapper with Java Servlet Filters

If you're using a ContentCachingRequestWrapper from Spring, you may be confused to find errors, similar to the below, from your application:

HTTP/1.1 400
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 25 May 2020 15:45:29 GMT
Connection: close
{
    "timestamp": "2020-05-25T15:45:33.739+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.String> me.jvt.hacking.controller.Controller.echo(java.lang.String)",
    "path": "/"
}

This appears to be because the ContentCachingRequestWrapper doesn't cache the raw ServletInputStream, which is then consumed further down the line by Spring when trying to use @RequestBody.

The solution is detailed in my article Reading a Servlet/Spring Request Body Multiple Times, and involves not using the ContentCachingRequestWrapper, but instead using a custom class that can cache the ServletInputStream.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #java #spring #servlet.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.