Skip to content

Commit ab419ab

Browse files
committed
Use ServletResponse#getContentType in ServletServerHttpResponse
This commit updates ServletServerHttpResponse.ServletResponseHttpHeaders in order to use ServletResponse#getContentType instead of ServletResponse#getHeader. It allows to have a consistent behavior between Tomcat (which set only the former) and Undertow/Jetty (which set both). See spring-projectsgh-32339
1 parent 516a203 commit ab419ab

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.http.HttpHeaders;
2929
import org.springframework.http.HttpStatusCode;
30+
import org.springframework.http.MediaType;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.CollectionUtils;
@@ -36,6 +37,7 @@
3637
*
3738
* @author Arjen Poutsma
3839
* @author Rossen Stoyanchev
40+
* @author Sebastien Deleuze
3941
* @since 3.0
4042
*/
4143
public class ServletServerHttpResponse implements ServerHttpResponse {
@@ -159,8 +161,8 @@ public boolean containsKey(Object key) {
159161
public String getFirst(String headerName) {
160162
if (headerName.equalsIgnoreCase(CONTENT_TYPE)) {
161163
// Content-Type is written as an override so check super first
162-
String value = super.getFirst(headerName);
163-
return (value != null ? value : servletResponse.getHeader(headerName));
164+
String contentType = super.getFirst(headerName);
165+
return (contentType != null ? contentType : servletResponse.getContentType());
164166
}
165167
else {
166168
String value = servletResponse.getHeader(headerName);
@@ -175,7 +177,8 @@ public List<String> get(Object key) {
175177
String headerName = (String) key;
176178
if (headerName.equalsIgnoreCase(CONTENT_TYPE)) {
177179
// Content-Type is written as an override so don't merge
178-
return Collections.singletonList(getFirst(headerName));
180+
String contentType = getFirst(headerName);
181+
return Collections.singletonList(contentType != null ? contentType : servletResponse.getContentType());
179182
}
180183

181184
Collection<String> values1 = servletResponse.getHeaders(headerName);

0 commit comments

Comments
 (0)