Deferred handling of Flux error in Spring MVC

This commit defers flushing of the response until the first item is
emitted that needs to be written (and flushed) to the response.

This makes Spring MVC consistent with WebFlux in this regard.

Closes gh-21972
This commit is contained in:
Rossen Stoyanchev
2019-05-06 12:26:25 -04:00
parent 15e1af2281
commit 53cadf15e7
2 changed files with 37 additions and 9 deletions

View File

@@ -152,9 +152,8 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur
// At this point we know we're streaming..
ShallowEtagHeaderFilter.disableContentCaching(request);
// Commit the response and wrap to ignore further header changes
outputMessage.getBody();
outputMessage.flush();
// Wrap the response to ignore further header changes
// Headers will be flushed at the first write
outputMessage = new StreamingServletServerHttpResponse(outputMessage);
DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
@@ -198,7 +197,13 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur
@Override
public void complete() {
this.deferredResult.setResult(null);
try {
this.outputMessage.flush();
this.deferredResult.setResult(null);
}
catch (IOException ex) {
this.deferredResult.setErrorResult(ex);
}
}
@Override