diff --git a/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java b/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java index ab1e253309..a4340ac752 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java @@ -112,7 +112,7 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple handleErrorStatus(request, response, status, wrapped.getMessage()); response.flushBuffer(); } - else if (!request.isAsyncStarted()) { + else if (!request.isAsyncStarted() && !response.isCommitted()) { response.flushBuffer(); } } @@ -184,7 +184,7 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple + request.getRequestURI() + " as the response has already been" + " committed. As a result, the response may have the wrong status" + " code. If your application is running on WebSphere Application" - + " Server you may be able to resolve this problem by setting " + + " Server you may be able to resolve this problem by setting" + " com.ibm.ws.webcontainer.invokeFlushAfterService to false"; if (ex == null) { logger.error(message); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java b/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java index c6dee4244d..fe73f0b146 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java @@ -40,6 +40,10 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** * Tests for {@link ErrorPageFilter}. @@ -343,4 +347,16 @@ public class ErrorPageFilterTests { assertTrue(this.response.isCommitted()); } + @Test + public void responseIsNotFlushedIfStatusIsLessThan400AndItHasAlreadyBeenCommitted() + throws Exception { + HttpServletResponse committedResponse = mock(HttpServletResponse.class); + given(committedResponse.isCommitted()).willReturn(true); + given(committedResponse.getStatus()).willReturn(200); + + this.filter.doFilter(this.request, committedResponse, this.chain); + + verify(committedResponse, times(0)).flushBuffer(); + } + }