Affects: 5.3.8


When a StreamingResponseBody endpoint throws an exception, and it's handled by the RestControllerAdvice which handles as a 500 error, something strange happens. The endpoint returns the custom object and the default Spring WhiteLabel Error object together, resulting in an invalid JSON object.

I think that the problem is caused by this line https://github.com/spring-projects/spring-framework/blob/main/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.java#L111

Steps to reproduce: 1. MyRestController class

@RestController
public class MyRestController {
    @GetMapping("/whiteLabelError")
    public StreamingResponseBody whiteLabelError() {
        return out -> {
            throw new RuntimeException();
        };
    }
  1. RestControllerAdvice class
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

@RestControllerAdvice
public class MyRestControllerAdvice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = {RuntimeException.class})
    private ResponseEntity<Object> handleRestRuntimeException(final RuntimeException ex, final WebRequest request) {
        return handleExceptionInternal(ex, INTERNAL_SERVER_ERROR, new HttpHeaders(), INTERNAL_SERVER_ERROR, request);
    }
}

The endpoint's response:

"INTERNAL_SERVER_ERROR"{
    "timestamp": 1623421573315,
    "status": 500,
    "error": "Internal Server Error",
    "path": "/exceptions/whiteLabelError"
}

Comment From: rstoyanchev

As indicated in our guidelines, issue descriptions should be complete and self-sufficient. Please edit the description to provide all relevant details independent of SO.

Comment From: exody16

I've updated the description

Comment From: snicoll

@feco16 please move that code in text into an actual sample that we can run ourselves. In order to reproduce, we'd have to do that anyway and we might be missing a detail in doing so. Thanks!

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: rstoyanchev

This is a duplicate of #30702, subsequently superseded by PR #31104, and further refined in #31154.