Browse Source

Flush headers after null StreamingResponseBody

Issue: SPR-14315
pull/1055/merge
Lifu Zhou 9 years ago committed by Rossen Stoyanchev
parent
commit
7e95cd8b4e
  1. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.java
  2. 13
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandlerTests.java

2
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.java

@ -74,6 +74,8 @@ public class StreamingResponseBodyReturnValueHandler implements HandlerMethodRet @@ -74,6 +74,8 @@ public class StreamingResponseBodyReturnValueHandler implements HandlerMethodRet
returnValue = responseEntity.getBody();
if (returnValue == null) {
mavContainer.setRequestHandled(true);
// Ensure headers are flushed
outputMessage.flush();
return;
}
}

13
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandlerTests.java

@ -29,6 +29,7 @@ import org.junit.Before; @@ -29,6 +29,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
@ -58,6 +59,8 @@ public class StreamingResponseBodyReturnValueHandlerTests { @@ -58,6 +59,8 @@ public class StreamingResponseBodyReturnValueHandlerTests {
private MockHttpServletResponse response;
private HttpHeaders headers = new HttpHeaders();
@Before
public void setUp() throws Exception {
@ -69,6 +72,8 @@ public class StreamingResponseBodyReturnValueHandlerTests { @@ -69,6 +72,8 @@ public class StreamingResponseBodyReturnValueHandlerTests {
this.response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, this.response);
this.headers.add("foo", "bar");
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
this.request.setAsyncSupported(true);
@ -140,6 +145,14 @@ public class StreamingResponseBodyReturnValueHandlerTests { @@ -140,6 +145,14 @@ public class StreamingResponseBodyReturnValueHandlerTests {
assertEquals(204, this.response.getStatus());
}
@Test
public void responseEntityWithHeadersAndNoContent() throws Exception {
MethodParameter returnType = returnType(TestController.class, "handleResponseEntity");
ResponseEntity<?> emitter = ResponseEntity.noContent().headers(headers).build();
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
assertEquals(this.response.getHeaders("foo"), this.headers.get("foo"));
}
private MethodParameter returnType(Class<?> clazz, String methodName) throws NoSuchMethodException {
Method method = clazz.getDeclaredMethod(methodName);

Loading…
Cancel
Save