Skip to content

Commit e5aac66

Browse files
committed
ServletResponseHeadersAdapter checks contentType property
Issue gh-36334
1 parent 8bf85d2 commit e5aac66

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.AbstractSet;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.Iterator;
2324
import java.util.LinkedHashMap;
2425
import java.util.List;
@@ -49,8 +50,12 @@ class ServletResponseHeadersAdapter implements MultiValueMap<String, String> {
4950

5051

5152
@Override
52-
public String getFirst(String key) {
53-
return this.response.getHeader(key);
53+
public @Nullable String getFirst(String key) {
54+
String header = this.response.getHeader(key);
55+
if (header == null && key.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE)) {
56+
header = this.response.getContentType();
57+
}
58+
return header;
5459
}
5560

5661
@Override
@@ -119,6 +124,10 @@ public boolean containsValue(Object rawValue) {
119124
public @Nullable List<String> get(Object key) {
120125
if (key instanceof String headerName) {
121126
Collection<String> values = this.response.getHeaders(headerName);
127+
if (values.isEmpty() && headerName.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE)) {
128+
String contentType = this.response.getContentType();
129+
return (contentType != null ? Collections.singletonList(contentType) : null);
130+
}
122131
if (!values.isEmpty()) {
123132
return new ArrayList<>(values);
124133
}
@@ -138,11 +147,11 @@ public boolean containsValue(Object rawValue) {
138147
@Override
139148
public @Nullable List<String> remove(Object key) {
140149
if (key instanceof String headerName) {
141-
Collection<String> previous = this.response.getHeaders(headerName);
150+
List<String> previous = get(headerName);
142151
if (previous != null) {
143152
this.response.setHeader(headerName, null);
144153
}
145-
return (previous != null ? new ArrayList<>(previous) : null);
154+
return previous;
146155
}
147156
return null;
148157
}

0 commit comments

Comments
 (0)