ResponseEntity<StreamingResponseBody> Exception Handling Issues
See original GitHub issueSummary
- When using
@ExceptionHandler(or@ControllerAdvice), exceptions thrown while writing to aStreamingResponseBodyare not handled correctly (or at least as I expect them to be…) when the request mapping returnsResponseEntity<StreamingResponseBody> - Exception handling does work as expected when the return type is just
StreamingResponseBody - It’s possible my understanding of how these should work is incorrect, however I haven’t seen any evidence yet to point to this
Description
- If a runtime exception occurs while writing to the
StreamingResponseBodywrapped in aResponseEntitythe endpoint does invoke the@ExceptionHandlerbut returns a500status with no response body no matter what - If the request mapping returns a
StreamingResponseBodydirectly, error handling does work as expected, and the response code and response body are correct - While writing tests for this, I can’t get the tests to return anything other than 200 status. While running tests, I also don’t see the exception handlers getting invoked. I’m not sure if this is another bug or if my test setup is incorrect. If you hit the endpoints directly in the browser or via CURL, you do see the 500 status with no body. This is my first time using
StreamingResponseBodyso I apologize if I’m just doing things incorrectly - Sample project to reproduce: https://github.com/austinarbor/spring-boot-streaming-response-issue
Misc
- Spring Boot: 2.3.1 / 2.3.2
- Spring Framework: 5.2.8
- Java Version: 11
- StackOverflow Question
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:5 (3 by maintainers)
Top Results From Across the Web
spring - How to handle Exception occuring when returning ...
I have implemented a Spring Rest Controller that streams back large files using the StreamingResponseBody. However, these files are coming ...
Read more >Exception handling in StreamingResponseBody-Spring MVC
The best way I discovered to handle errors/exceptions in the web environment is to create your custom exception with the disabled stack trace,...
Read more >spring-projects/spring-boot - Gitter
Anyone knows how to handle exceptions when you are using ... Maybe in this case you could return a ResponseEntity (and a StreamingResponseBody...
Read more >Streaming large dataset over Rest? - jvm aware
This short article will explore a few options to handle checked and unchecked exceptions in lambda expressions. Lambda Expressions and Exception ...
Read more >22. Web MVC framework - Spring
Handler exception resolvers that are declared in the ... Note that StreamingResponseBody can also be used as the body in a ResponseEntity in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have a similar issue: the application works as expected, no matter if I wrap the
StreamingResponseBodyin aResponseEntityor not, but in the tests I always get a 200 and an empty body. I tried with@WebMvcTest+mockMvcand with@SpringBootTest+TestRestTemplate: same behaviour.@austinarbor yes that would be the workaround for the
@ExceptionHandlermethod. Also the test updates look about right from a quick look.