1919import java .util .AbstractSet ;
2020import java .util .ArrayList ;
2121import java .util .Collection ;
22+ import java .util .Collections ;
2223import java .util .Iterator ;
2324import java .util .LinkedHashMap ;
2425import 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