question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InputStream consumed by DispatcherServlet logRequest

See original GitHub issue

Affects: 5.2.1


A few weeks ago I enabled debug logging for my application, today I had forgotten about but noticed that my injected InputStream was empty. After a debugging session (first assuming it again was related to the HiddenHttpMethodFilter) I ended up at

https://github.com/spring-projects/spring-framework/blob/8d846500eff3e51fc49846a969ae1a7f4671015d/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java#L955-L965

So every time I enable debug logging I cannot use my injected InputStream:

@PostMapping
public ResponseEntity<String> doPost(InputStream input, HttpServletRequest request) {
    ...

I do now get this logging:

2019-12-10T10:27:46,305 [http-nio-8082-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - POST "/communication", parameters={masked}

But maybe it would make sense to auto-wrap the request in DEBUG when that logging is active or give a warning when injecting the consumed InputStream later and even report who did it 😄

Thanks!

Related to (but not covered by): #22985

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
rstoyanchevcommented, Dec 10, 2019

Yes this is expected, and besides the logging of request details, any other code could cause a similar issue by accessing a request parameter.

Note that you can inject the content reliably as @RequestBody String or @RequestBody ByteArrayResource because we reconstruct the body from request parameters in ServletServerHttpRequest#getBody.

1reaction
rstoyanchevcommented, Jan 17, 2020

Yes, just use the standard multipart support in the Servlet container.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot - How to log all requests and responses with ...
... Log request and response payload ("body") ======== // We CANNOT simply read the request payload here, because then the InputStream would be...
Read more >
Spring - Log Incoming Requests - Baeldung
The main issue with the reading request is that, as soon as the input stream is read for the first time, it's marked...
Read more >
Log Incoming Requests In Spring - Java Development Journal
You have to be careful while using such approach as input stream will be marked as consumed the moment it is read for...
Read more >
Chapter 5. Creating a Web service with Spring-WS
The MessageDispatcherServlet is a standard Servlet which conveniently extends from the standard Spring Web DispatcherServlet , and wraps a MessageDispatcher .
Read more >
Logging HttpRequest parameters and request body-Spring MVC
If the request body is huge I recommend putting the input stream into a ... HttpServletRequestWrapper { public LogRequest(HttpServletRequest request) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found