Ignore flushes on ServletServerHttpResponse output stream

Prior to this commit, flush calls on the output stream returned by
`ServletServerHttpResponse#getBody` would be delegated to the Servlet
response output stream.
This can cause performance issues when `HttpMessageConverter` and other
web components write and flush multiple times to the response body.
Here, the Servlet container is in a better position to flush to the
network at the optimal time and buffer the response body until then.

This is particularly true for `HttpMessageConverters` when they flush
many times the output stream, sometimes due to the underlying codec
library. Instead of revisiting the entire message converter contract, we
are here ignoring flush calls to that output stream.

This change does not affect the client side, nor the
`ServletServerHttpResponse#flush` calls.

This commit also introduces a new Spring property
`"spring.http.response.flush.enabled"` that reverts this behavior change
if necessary.

Closes gh-36385
This commit is contained in:
Brian Clozel
2026-02-24 21:44:48 +01:00
parent f2b64ad09c
commit e0b54e244e
5 changed files with 74 additions and 9 deletions
@@ -23,20 +23,35 @@ import java.nio.charset.Charset;
import jakarta.servlet.http.HttpServletResponse;
import org.jspecify.annotations.Nullable;
import org.springframework.core.SpringProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
/**
* {@link ServerHttpResponse} implementation that is based on a {@link HttpServletResponse}.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Brian Clozel
* @since 3.0
*/
public class ServletServerHttpResponse implements ServerHttpResponse {
/**
* System property that indicates whether {@code response.getBody().flush()}
* should effectively flush to the network. This is by default disabled,
* and developers must set the {@code "spring.http.response.flush.enabled"}
* {@link org.springframework.core.SpringProperties Spring property} to
* turn it on.
* <p>Applications should instead {@link #flush()} on the response directly.
*/
public static final String BODY_FLUSH_ENABLED = "spring.http.response.flush.enabled";
private final boolean flushEnabled = SpringProperties.getFlag(BODY_FLUSH_ENABLED);
private final HttpServletResponse servletResponse;
private final HttpHeaders headers;
@@ -86,11 +101,20 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
}
}
/**
* Return the body of the message as an output stream.
* <p>By default, flushing the output stream has no effect
* (see {@link #BODY_FLUSH_ENABLED}) and should be performed
* using {@link #flush()} instead.
* @return the output stream body (never {@code null})
* @throws IOException in case of I/O errors
*/
@Override
public OutputStream getBody() throws IOException {
this.bodyUsed = true;
writeHeaders();
return this.servletResponse.getOutputStream();
return (this.flushEnabled) ? this.servletResponse.getOutputStream() :
StreamUtils.nonFlushing(this.servletResponse.getOutputStream());
}
@Override