Skip to content

Commit 8b14bf8

Browse files
committed
Merge branch '6.2.x'
2 parents 75329e6 + f895d76 commit 8b14bf8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.core.io.ClassPathResource;
5050
import org.springframework.core.io.support.PropertiesLoaderUtils;
5151
import org.springframework.core.log.LogFormatUtils;
52+
import org.springframework.http.HttpHeaders;
5253
import org.springframework.http.MediaType;
5354
import org.springframework.http.server.RequestPath;
5455
import org.springframework.http.server.ServletServerHttpRequest;
@@ -1208,9 +1209,10 @@ protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletExcepti
12081209

12091210
// Success and error responses may use different content types
12101211
request.removeAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
1211-
// Reset the response body buffer if the response is not committed already,
1212-
// leaving the response headers in place.
1212+
// Reset the response content-type header and body buffer if the response is not committed already,
1213+
// leaving the other response headers in place.
12131214
try {
1215+
response.setHeader(HttpHeaders.CONTENT_TYPE, null);
12141216
response.resetBuffer();
12151217
}
12161218
catch (IllegalStateException illegalStateException) {

spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,23 @@ void shouldAttemptToResetResponseBufferIfCommitted() throws Exception {
898898
assertThat(response.getHeader("Test-Header")).isEqualTo("spring");
899899
}
900900

901+
@Test
902+
void shouldResetContentTypeIfNotCommitted() throws Exception {
903+
StaticWebApplicationContext context = new StaticWebApplicationContext();
904+
context.setServletContext(getServletContext());
905+
context.registerSingleton("/error", ErrorController.class);
906+
DispatcherServlet servlet = new DispatcherServlet(context);
907+
servlet.init(servletConfig);
908+
909+
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error");
910+
MockHttpServletResponse response = new MockHttpServletResponse();
911+
assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class)
912+
.hasCauseInstanceOf(IllegalArgumentException.class);
913+
assertThat(response.getContentAsByteArray()).isEmpty();
914+
assertThat(response.getStatus()).isEqualTo(400);
915+
assertThat(response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_TYPE);
916+
}
917+
901918

902919
public static class ControllerFromParent implements Controller {
903920

@@ -950,6 +967,7 @@ private static class ErrorController implements Controller {
950967
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
951968
response.setStatus(400);
952969
response.setHeader("Test-Header", "spring");
970+
response.addHeader("Content-Type", "application/json");
953971
if (request.getAttribute("commit") != null) {
954972
response.flushBuffer();
955973
}

0 commit comments

Comments
 (0)