Class HttpHeaders
- All Implemented Interfaces:
Serializable,Map<String,,List<String>> MultiValueMap<String,String>
- Direct Known Subclasses:
WebSocketHttpHeaders
In addition to the regular methods defined by Map, this class offers many common
convenience methods, for example:
getFirst(String)returns the first value associated with a given header nameadd(String, String)adds a header value to the list of values for a header nameset(String, String)sets the header value to a single string value
Note that HttpHeaders instances created by the default constructor
treat header names in a case-insensitive manner. Instances created with the
HttpHeaders(MultiValueMap) constructor like those instantiated
internally by the framework to adapt to existing HTTP headers data structures
do guarantee per-header get/set/add operations to be case-insensitive as
mandated by the HTTP specification. However, it is not necessarily the case
for operations that deal with the collection as a whole (like size(),
values(), keySet() and entrySet()). Prefer using
headerSet() for these cases.
Some backing implementations can store header names in a case-sensitive
manner, which will lead to duplicates during the entrySet() iteration where
multiple occurrences of a header name can surface depending on letter casing
but each such entry has the full List of values. — This can be
problematic for example when copying headers into a new instance by iterating
over the old instance's entrySet() and using
addAll(String, List) rather than put(String, List).
This class is meant to reference "well-known" headers supported by Spring Framework. If your application or library relies on other headers defined in RFCs, please use methods that accept the header name as a parameter.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Sebastien Deleuze, Brian Clozel, Juergen Hoeller, Josh Long, Sam Brannen, Simon Baslé
- See Also:
-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe HTTPAcceptheader field name.static final StringThe HTTPAccept-Charsetheader field name.static final StringThe HTTPAccept-Encodingheader field name.static final StringThe HTTPAccept-Languageheader field name.static final StringThe HTTPAccept-Patchheader field name.static final StringThe HTTPAccept-Rangesheader field name.static final StringThe CORSAccess-Control-Allow-Credentialsresponse header field name.static final StringThe CORSAccess-Control-Allow-Headersresponse header field name.static final StringThe CORSAccess-Control-Allow-Methodsresponse header field name.static final StringThe CORSAccess-Control-Allow-Originresponse header field name.static final StringThe CORSAccess-Control-Expose-Headersresponse header field name.static final StringThe CORSAccess-Control-Max-Ageresponse header field name.static final StringThe CORSAccess-Control-Request-Headersrequest header field name.static final StringThe CORSAccess-Control-Request-Methodrequest header field name.static final StringThe HTTPAgeheader field name.static final StringThe HTTPAllowheader field name.static final StringThe HTTPAuthorizationheader field name.static final StringThe HTTPCache-Controlheader field name.static final StringThe HTTPConnectionheader field name.static final StringThe HTTPContent-Dispositionheader field name.static final StringThe HTTPContent-Encodingheader field name.static final StringThe HTTPContent-Languageheader field name.static final StringThe HTTPContent-Lengthheader field name.static final StringThe HTTPContent-Locationheader field name.static final StringThe HTTPContent-Rangeheader field name.static final StringThe HTTPContent-Typeheader field name.static final StringThe HTTPCookieheader field name.static final StringThe HTTPDateheader field name.static final HttpHeadersAn emptyHttpHeadersinstance (immutable).static final StringThe HTTPETagheader field name.static final StringThe HTTPExpectheader field name.static final StringThe HTTPExpiresheader field name.static final StringThe HTTPFromheader field name.static final StringThe HTTPHostheader field name.static final StringThe HTTPIf-Matchheader field name.static final StringThe HTTPIf-Modified-Sinceheader field name.static final StringThe HTTPIf-None-Matchheader field name.static final StringThe HTTPIf-Rangeheader field name.static final StringThe HTTPIf-Unmodified-Sinceheader field name.static final StringThe HTTPLast-Modifiedheader field name.static final StringThe HTTPLinkheader field name.static final StringThe HTTPLocationheader field name.static final StringThe HTTPMax-Forwardsheader field name.static final StringThe HTTPOriginheader field name.static final StringThe HTTPPragmaheader field name.static final StringThe HTTPProxy-Authenticateheader field name.static final StringThe HTTPProxy-Authorizationheader field name.static final StringThe HTTPRangeheader field name.static final StringThe HTTPRefererheader field name.static final StringThe HTTPRetry-Afterheader field name.static final StringThe HTTPServerheader field name.static final StringThe HTTPSet-Cookieheader field name.static final StringThe HTTPSet-Cookie2header field name.static final StringThe HTTPTEheader field name.static final StringThe HTTPTrailerheader field name.static final StringThe HTTPTransfer-Encodingheader field name.static final StringThe HTTPUpgradeheader field name.static final StringThe HTTPUser-Agentheader field name.static final StringThe HTTPVaryheader field name.static final StringThe HTTPViaheader field name.static final StringThe HTTPWarningheader field name.static final StringThe HTTPWWW-Authenticateheader field name. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new, empty instance of theHttpHeadersobject using an underlying case-insensitive map.HttpHeaders(MultiValueMap<String, String> headers) Construct a newHttpHeadersinstance backed by an existing map. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd the given, single header value under the given name.voidAdd all the values of the given list to the current list of values for the given key.voidaddAll(MultiValueMap<String, String> values) Add all the values of the givenMultiValueMapto the current values.Return this map as aMapwith the first values contained in thisMultiValueMap.voidclear()voidRemove the well-known"Content-*"HTTP headers.booleancontainsKey(Object key) booleancontainsValue(Object value) static StringencodeBasicAuth(String username, String password, Charset charset) Encode the given username and password into Basic Authentication credentials.entrySet()booleanvoidforEach(BiConsumer<? super String, ? super List<String>> action) Perform an action over each header, as when iterated viaentrySet().static StringformatHeaders(MultiValueMap<String, String> headers) Helps to format HTTP header values, as HTTP header values themselves can contain comma-separated values, can become confusing with regularMapformatting that also uses commas between entries.Return the list of acceptable media types, as specified by theAcceptheader.Return the list of acceptable charsets, as specified by theAccept-Charsetheader.Return the language ranges from the "Accept-Language" header.Return the list of acceptable media types forPATCHmethods, as specified by theAccept-Patchheader.booleanReturn the value of theAccess-Control-Allow-Credentialsresponse header.Return the value of theAccess-Control-Allow-Headersresponse header.Return the value of theAccess-Control-Allow-Methodsresponse header.Return the value of theAccess-Control-Allow-Originresponse header.Return the value of theAccess-Control-Expose-Headersresponse header.longReturn the value of theAccess-Control-Max-Ageresponse header.Return the value of theAccess-Control-Request-Headersrequest header.Return the value of theAccess-Control-Request-Methodrequest header.getAllow()Return the set of allowedHTTP methods, as specified by theAllowheader.Return the value of theCache-Controlheader.Return the value of theConnectionheader.Return a parsed representation of the Content-Disposition header.Get the firstLocaleof the content languages, as specified by theContent-Languageheader.longReturn the length of the body in bytes, as specified by theContent-Lengthheader.Return the media type of the body, as specified by theContent-Typeheader.longgetDate()Return the date and time at which the message was created, as specified by theDateheader.getETag()Return the entity tag of the body, as specified by theETagheader.getETagValuesAsList(String name) Retrieve a combined result from the field values of the ETag header.longReturn the date and time at which the message is no longer valid, as specified by theExpiresheader.protected StringgetFieldValues(String headerName) Retrieve a combined result from the field values of multivalued headers.Return the first header value for the given header name, if any.longgetFirstDate(String headerName) Parse the first header value for the given header name as a date, return -1 if there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.getFirstZonedDateTime(String headerName) Parse the first header value for the given header name as a date, returnnullif there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.getHost()Return the value of theHostheader, if available.Return the value of theIf-Matchheader.longReturn the value of theIf-Modified-Sinceheader.Return the value of theIf-None-Matchheader.longReturn the value of theIf-Unmodified-Sinceheader.longReturn the time the resource was last changed, as specified by theLast-Modifiedheader.Return the (new) location of a resource as specified by theLocationheader.getOrEmpty(Object headerName) Get the list of header values for the given header name, if any.Return the value of theOriginheader.Return the value of thePragmaheader.getRange()Return the value of theRangeheader.Return the value of theUpgradeheader.getValuesAsList(String headerName) Return all values of a given header name, even if this header is set multiple times.getVary()Return the request header names subject to content negotiation.inthashCode()Return a view of the headers as an entrySetof key-list pairs.booleanisEmpty()keySet()Return aSetview of header names.voidputIfAbsent(String key, List<String> value) static HttpHeadersreadOnlyHttpHeaders(HttpHeaders headers) Apply a read-onlyHttpHeaderswrapper around the given headers, if necessary.static HttpHeadersreadOnlyHttpHeaders(MultiValueMap<String, String> headers) Apply a read-onlyHttpHeaderswrapper around the given headers, if necessary.voidSet the given, single header value under the given name.voidSet the list of acceptable media types, as specified by theAcceptheader.voidsetAcceptCharset(List<Charset> acceptableCharsets) Set the list of acceptable charsets, as specified by theAccept-Charsetheader.voidsetAcceptLanguage(List<Locale.LanguageRange> languages) Set the acceptable language ranges, as specified by the Accept-Language header.voidsetAcceptLanguageAsLocales(List<Locale> locales) Variant ofsetAcceptLanguage(List)usingLocale's.voidsetAcceptPatch(List<MediaType> mediaTypes) voidsetAccessControlAllowCredentials(boolean allowCredentials) Set the (new) value of theAccess-Control-Allow-Credentialsresponse header.voidsetAccessControlAllowHeaders(List<String> allowedHeaders) Set the (new) value of theAccess-Control-Allow-Headersresponse header.voidsetAccessControlAllowMethods(List<HttpMethod> allowedMethods) Set the (new) value of theAccess-Control-Allow-Methodsresponse header.voidsetAccessControlAllowOrigin(String allowedOrigin) Set the (new) value of theAccess-Control-Allow-Originresponse header.voidsetAccessControlExposeHeaders(List<String> exposedHeaders) Set the (new) value of theAccess-Control-Expose-Headersresponse header.voidsetAccessControlMaxAge(long maxAge) Set the (new) value of theAccess-Control-Max-Ageresponse header.voidsetAccessControlMaxAge(Duration maxAge) Set the (new) value of theAccess-Control-Max-Ageresponse header.voidsetAccessControlRequestHeaders(List<String> requestHeaders) Set the (new) value of theAccess-Control-Request-Headersrequest header.voidsetAccessControlRequestMethod(HttpMethod requestMethod) Set the (new) value of theAccess-Control-Request-Methodrequest header.voidSet the given values under.voidsetAllow(Set<HttpMethod> allowedMethods) Set the set of allowedHTTP methods, as specified by theAllowheader.voidsetBasicAuth(String encodedCredentials) Set the value of the Authorization header to Basic Authentication based on the given encoded credentials.voidsetBasicAuth(String username, String password) Set the value of the Authorization header to Basic Authentication based on the given username and password.voidsetBasicAuth(String username, String password, Charset charset) Set the value of the Authorization header to Basic Authentication based on the given username and password.voidsetBearerAuth(String token) Set the value of the Authorization header to the given Bearer token.voidsetCacheControl(String cacheControl) Set the (new) value of theCache-Controlheader.voidsetCacheControl(CacheControl cacheControl) Set a configuredCacheControlinstance as the new value of theCache-Controlheader.voidsetConnection(String connection) Set the (new) value of theConnectionheader.voidsetConnection(List<String> connection) Set the (new) value of theConnectionheader.voidsetContentDisposition(ContentDisposition contentDisposition) Set the Content-Disposition header.voidsetContentDispositionFormData(String name, String filename) Set theContent-Dispositionheader when creating a"multipart/form-data"request.voidsetContentLanguage(Locale locale) Set theLocaleof the content language, as specified by the Content-Language header.voidsetContentLength(long contentLength) Set the length of the body in bytes, as specified by theContent-Lengthheader.voidsetContentType(MediaType mediaType) Set the media type of the body, as specified by theContent-Typeheader.voidsetDate(long date) Set the date and time at which the message was created, as specified by theDateheader.voidSet the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.voidSet the date and time at which the message was created, as specified by theDateheader.voidsetDate(ZonedDateTime date) Set the date and time at which the message was created, as specified by theDateheader.voidSet the (new) entity tag of the body, as specified by theETagheader.voidsetExpires(long expires) Set the date and time at which the message is no longer valid, as specified by theExpiresheader.voidsetExpires(Instant expires) Set the date and time at which the message is no longer valid, as specified by theExpiresheader.voidsetExpires(ZonedDateTime expires) Set the duration after which the message is no longer valid, as specified by theExpiresheader.voidsetHost(InetSocketAddress host) Set the (new) value of theHostheader.voidsetIfMatch(String ifMatch) Set the (new) value of theIf-Matchheader.voidsetIfMatch(List<String> ifMatchList) Set the (new) value of theIf-Matchheader.voidsetIfModifiedSince(long ifModifiedSince) Set the (new) value of theIf-Modified-Sinceheader.voidsetIfModifiedSince(Instant ifModifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetIfModifiedSince(ZonedDateTime ifModifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetIfNoneMatch(String ifNoneMatch) Set the (new) value of theIf-None-Matchheader.voidsetIfNoneMatch(List<String> ifNoneMatchList) Set the (new) values of theIf-None-Matchheader.voidsetIfUnmodifiedSince(long ifUnmodifiedSince) Set the (new) value of theIf-Unmodified-Sinceheader.voidsetIfUnmodifiedSince(Instant ifUnmodifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetIfUnmodifiedSince(ZonedDateTime ifUnmodifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetInstant(String headerName, Instant date) Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.voidsetLastModified(long lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetLastModified(Instant lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetLastModified(ZonedDateTime lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetLocation(URI location) Set the (new) location of a resource, as specified by theLocationheader.voidSet the (new) value of theOriginheader.voidSet the (new) value of thePragmaheader.voidSets the (new) value of theRangeheader.voidsetUpgrade(String upgrade) Set the (new) value of theUpgradeheader.voidSet the request header names (for example, "Accept-Language") for which the response is subject to content negotiation and variances based on the value of those request headers.voidsetZonedDateTime(String headerName, ZonedDateTime date) Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.intsize()Return the number of headers in the collection.protected StringtoCommaDelimitedString(List<String> headerValues) Turn the given list of header values into a comma-delimited result.Return aMapwith the first values contained in thisMultiValueMap.toString()values()Return aCollectionview of all the header values, reconstructed from iterating over thekeySet().static HttpHeaderswritableHttpHeaders(HttpHeaders headers) Deprecated, for removal: This API element is subject to removal in a future version.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, getOrDefault, merge, remove, replace, replace, replaceAllMethods inherited from interface org.springframework.util.MultiValueMap
addIfAbsent
-
Field Details
-
ACCEPT
The HTTPAcceptheader field name. -
ACCEPT_CHARSET
The HTTPAccept-Charsetheader field name. -
ACCEPT_ENCODING
The HTTPAccept-Encodingheader field name. -
ACCEPT_LANGUAGE
The HTTPAccept-Languageheader field name. -
ACCEPT_PATCH
The HTTPAccept-Patchheader field name.- Since:
- 5.3.6
- See Also:
-
ACCEPT_RANGES
The HTTPAccept-Rangesheader field name. -
ACCESS_CONTROL_ALLOW_CREDENTIALS
The CORSAccess-Control-Allow-Credentialsresponse header field name.- See Also:
-
ACCESS_CONTROL_ALLOW_HEADERS
The CORSAccess-Control-Allow-Headersresponse header field name.- See Also:
-
ACCESS_CONTROL_ALLOW_METHODS
The CORSAccess-Control-Allow-Methodsresponse header field name.- See Also:
-
ACCESS_CONTROL_ALLOW_ORIGIN
The CORSAccess-Control-Allow-Originresponse header field name.- See Also:
-
ACCESS_CONTROL_EXPOSE_HEADERS
The CORSAccess-Control-Expose-Headersresponse header field name.- See Also:
-
ACCESS_CONTROL_MAX_AGE
The CORSAccess-Control-Max-Ageresponse header field name.- See Also:
-
ACCESS_CONTROL_REQUEST_HEADERS
The CORSAccess-Control-Request-Headersrequest header field name.- See Also:
-
ACCESS_CONTROL_REQUEST_METHOD
The CORSAccess-Control-Request-Methodrequest header field name.- See Also:
-
AGE
The HTTPAgeheader field name.- See Also:
-
ALLOW
The HTTPAllowheader field name. -
AUTHORIZATION
The HTTPAuthorizationheader field name.- See Also:
-
CACHE_CONTROL
The HTTPCache-Controlheader field name.- See Also:
-
CONNECTION
The HTTPConnectionheader field name.- See Also:
-
CONTENT_ENCODING
The HTTPContent-Encodingheader field name. -
CONTENT_DISPOSITION
The HTTPContent-Dispositionheader field name.- See Also:
-
CONTENT_LANGUAGE
The HTTPContent-Languageheader field name. -
CONTENT_LENGTH
The HTTPContent-Lengthheader field name. -
CONTENT_LOCATION
The HTTPContent-Locationheader field name. -
CONTENT_RANGE
The HTTPContent-Rangeheader field name.- See Also:
-
CONTENT_TYPE
The HTTPContent-Typeheader field name. -
COOKIE
The HTTPCookieheader field name. -
DATE
The HTTPDateheader field name. -
ETAG
The HTTPETagheader field name.- See Also:
-
EXPECT
The HTTPExpectheader field name. -
EXPIRES
The HTTPExpiresheader field name.- See Also:
-
FROM
The HTTPFromheader field name. -
HOST
The HTTPHostheader field name.- See Also:
-
IF_MATCH
The HTTPIf-Matchheader field name.- See Also:
-
IF_MODIFIED_SINCE
The HTTPIf-Modified-Sinceheader field name.- See Also:
-
IF_NONE_MATCH
The HTTPIf-None-Matchheader field name.- See Also:
-
IF_RANGE
The HTTPIf-Rangeheader field name.- See Also:
-
IF_UNMODIFIED_SINCE
The HTTPIf-Unmodified-Sinceheader field name.- See Also:
-
LAST_MODIFIED
The HTTPLast-Modifiedheader field name.- See Also:
-
LINK
The HTTPLinkheader field name.- See Also:
-
LOCATION
The HTTPLocationheader field name. -
MAX_FORWARDS
The HTTPMax-Forwardsheader field name. -
ORIGIN
The HTTPOriginheader field name.- See Also:
-
PRAGMA
The HTTPPragmaheader field name.- See Also:
-
PROXY_AUTHENTICATE
The HTTPProxy-Authenticateheader field name.- See Also:
-
PROXY_AUTHORIZATION
The HTTPProxy-Authorizationheader field name.- See Also:
-
RANGE
The HTTPRangeheader field name.- See Also:
-
REFERER
The HTTPRefererheader field name. -
RETRY_AFTER
The HTTPRetry-Afterheader field name. -
SERVER
The HTTPServerheader field name. -
SET_COOKIE
The HTTPSet-Cookieheader field name. -
SET_COOKIE2
The HTTPSet-Cookie2header field name.- See Also:
-
TE
The HTTPTEheader field name.- See Also:
-
TRAILER
The HTTPTrailerheader field name.- See Also:
-
TRANSFER_ENCODING
The HTTPTransfer-Encodingheader field name. -
UPGRADE
The HTTPUpgradeheader field name.- See Also:
-
USER_AGENT
The HTTPUser-Agentheader field name. -
VARY
The HTTPVaryheader field name. -
VIA
The HTTPViaheader field name. -
WARNING
The HTTPWarningheader field name.- See Also:
-
WWW_AUTHENTICATE
The HTTPWWW-Authenticateheader field name.- See Also:
-
EMPTY
An emptyHttpHeadersinstance (immutable).- Since:
- 5.0
-
-
Constructor Details
-
HttpHeaders
public HttpHeaders()Construct a new, empty instance of theHttpHeadersobject using an underlying case-insensitive map. -
HttpHeaders
Construct a newHttpHeadersinstance backed by an existing map.This constructor is available as an optimization for adapting to existing headers map structures, primarily for internal use within the framework.
- Parameters:
headers- the headers map (expected to operate with case-insensitive keys)- Since:
- 5.1
-
-
Method Details
-
getOrEmpty
Get the list of header values for the given header name, if any.- Parameters:
headerName- the header name- Returns:
- the list of header values, or an empty list
- Since:
- 5.2
-
setAccept
Set the list of acceptable media types, as specified by theAcceptheader. -
getAccept
Return the list of acceptable media types, as specified by theAcceptheader.Returns an empty list when the acceptable media types are unspecified.
-
setAcceptLanguage
Set the acceptable language ranges, as specified by the Accept-Language header.- Since:
- 5.0
-
getAcceptLanguage
Return the language ranges from the "Accept-Language" header.If you only need sorted, preferred locales only use
getAcceptLanguageAsLocales()or if you need to filter based on a list of supported locales you can pass the returned list toLocale.filter(List, Collection).- Throws:
IllegalArgumentException- if the value cannot be converted to a language range- Since:
- 5.0
-
setAcceptLanguageAsLocales
Variant ofsetAcceptLanguage(List)usingLocale's.- Since:
- 5.0
-
getAcceptLanguageAsLocales
- Returns:
- the locales or an empty list
- Throws:
IllegalArgumentException- if the value cannot be converted to a locale- Since:
- 5.0
-
setAcceptPatch
- Since:
- 5.3.6
-
getAcceptPatch
Return the list of acceptable media types forPATCHmethods, as specified by theAccept-Patchheader.Returns an empty list when the acceptable media types are unspecified.
- Since:
- 5.3.6
-
setAccessControlAllowCredentials
public void setAccessControlAllowCredentials(boolean allowCredentials) Set the (new) value of theAccess-Control-Allow-Credentialsresponse header. -
getAccessControlAllowCredentials
public boolean getAccessControlAllowCredentials()Return the value of theAccess-Control-Allow-Credentialsresponse header. -
setAccessControlAllowHeaders
Set the (new) value of theAccess-Control-Allow-Headersresponse header. -
getAccessControlAllowHeaders
Return the value of theAccess-Control-Allow-Headersresponse header. -
setAccessControlAllowMethods
Set the (new) value of theAccess-Control-Allow-Methodsresponse header. -
getAccessControlAllowMethods
Return the value of theAccess-Control-Allow-Methodsresponse header. -
setAccessControlAllowOrigin
Set the (new) value of theAccess-Control-Allow-Originresponse header. -
getAccessControlAllowOrigin
Return the value of theAccess-Control-Allow-Originresponse header. -
setAccessControlExposeHeaders
Set the (new) value of theAccess-Control-Expose-Headersresponse header. -
getAccessControlExposeHeaders
Return the value of theAccess-Control-Expose-Headersresponse header. -
setAccessControlMaxAge
Set the (new) value of theAccess-Control-Max-Ageresponse header.- Since:
- 5.2
-
setAccessControlMaxAge
public void setAccessControlMaxAge(long maxAge) Set the (new) value of theAccess-Control-Max-Ageresponse header. -
getAccessControlMaxAge
public long getAccessControlMaxAge()Return the value of theAccess-Control-Max-Ageresponse header.Returns -1 when the max age is unknown.
-
setAccessControlRequestHeaders
Set the (new) value of theAccess-Control-Request-Headersrequest header. -
getAccessControlRequestHeaders
Return the value of theAccess-Control-Request-Headersrequest header. -
setAccessControlRequestMethod
Set the (new) value of theAccess-Control-Request-Methodrequest header. -
getAccessControlRequestMethod
Return the value of theAccess-Control-Request-Methodrequest header. -
setAcceptCharset
Set the list of acceptable charsets, as specified by theAccept-Charsetheader. -
getAcceptCharset
Return the list of acceptable charsets, as specified by theAccept-Charsetheader. -
setAllow
Set the set of allowedHTTP methods, as specified by theAllowheader. -
getAllow
Return the set of allowedHTTP methods, as specified by theAllowheader.Returns an empty set when the allowed methods are unspecified.
-
setBasicAuth
Set the value of the Authorization header to Basic Authentication based on the given username and password.Note that this method only supports characters in the
ISO-8859-1character set.- Parameters:
username- the usernamepassword- the password- Throws:
IllegalArgumentException- if eitheruserorpasswordcontain characters that cannot be encoded to ISO-8859-1- Since:
- 5.1
- See Also:
-
setBasicAuth
Set the value of the Authorization header to Basic Authentication based on the given username and password.- Parameters:
username- the usernamepassword- the passwordcharset- the charset to use to convert the credentials into an octet sequence. Defaults to ISO-8859-1.- Throws:
IllegalArgumentException- ifusernameorpasswordcontains characters that cannot be encoded to the given charset- Since:
- 5.1
- See Also:
-
setBasicAuth
Set the value of the Authorization header to Basic Authentication based on the given encoded credentials.Favor this method over
setBasicAuth(String, String)andsetBasicAuth(String, String, Charset)if you wish to cache the encoded credentials.- Parameters:
encodedCredentials- the encoded credentials- Throws:
IllegalArgumentException- if supplied credentials string isnullor blank- Since:
- 5.2
- See Also:
-
setBearerAuth
Set the value of the Authorization header to the given Bearer token.- Parameters:
token- the Base64 encoded token- Since:
- 5.1
- See Also:
-
setCacheControl
Set a configuredCacheControlinstance as the new value of theCache-Controlheader.- Since:
- 5.0.5
-
setCacheControl
Set the (new) value of theCache-Controlheader. -
getCacheControl
Return the value of theCache-Controlheader. -
setConnection
Set the (new) value of theConnectionheader. -
setConnection
Set the (new) value of theConnectionheader. -
getConnection
Return the value of theConnectionheader. -
setContentDispositionFormData
Set theContent-Dispositionheader when creating a"multipart/form-data"request.Applications typically would not set this header directly but rather prepare a
MultiValueMap<String, Object>, containing an Object or aResourcefor each part, and then pass that to theRestTemplateorWebClient.- Parameters:
name- the control namefilename- the filename (may benull)- See Also:
-
setContentDisposition
Set the Content-Disposition header.This could be used on a response to indicate if the content is expected to be displayed inline in the browser or as an attachment to be saved locally.
It can also be used for a
"multipart/form-data"request. For more details see notes onsetContentDispositionFormData(java.lang.String, java.lang.String).- Since:
- 5.0
- See Also:
-
getContentDisposition
Return a parsed representation of the Content-Disposition header.- Since:
- 5.0
- See Also:
-
setContentLanguage
Set theLocaleof the content language, as specified by the Content-Language header.Use
put(CONTENT_LANGUAGE, list)if you need to set multiple content languages.- Since:
- 5.0
-
getContentLanguage
Get the firstLocaleof the content languages, as specified by theContent-Languageheader.Use
getValuesAsList(String)if you need to get multiple content languages.- Returns:
- the first
Localeof the content languages, ornullif unknown - Since:
- 5.0
-
setContentLength
public void setContentLength(long contentLength) Set the length of the body in bytes, as specified by theContent-Lengthheader.- Parameters:
contentLength- content length (greater than or equal to zero)- Throws:
IllegalArgumentException- if the content length is negative
-
getContentLength
public long getContentLength()Return the length of the body in bytes, as specified by theContent-Lengthheader.Returns -1 when the content-length is unknown.
-
setContentType
Set the media type of the body, as specified by theContent-Typeheader. -
getContentType
Return the media type of the body, as specified by theContent-Typeheader.Returns
nullwhen theContent-Typeheader is not set.- Throws:
InvalidMediaTypeException- if the media type value cannot be parsed
-
setDate
Set the date and time at which the message was created, as specified by theDateheader.- Since:
- 5.2
-
setDate
Set the date and time at which the message was created, as specified by theDateheader.- Since:
- 5.2
-
setDate
public void setDate(long date) Set the date and time at which the message was created, as specified by theDateheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT.
-
getDate
public long getDate()Return the date and time at which the message was created, as specified by theDateheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
- Throws:
IllegalArgumentException- if the value cannot be converted to a date
-
setETag
Set the (new) entity tag of the body, as specified by theETagheader. -
getETag
Return the entity tag of the body, as specified by theETagheader. -
setExpires
Set the duration after which the message is no longer valid, as specified by theExpiresheader.- Since:
- 5.0.5
-
setExpires
Set the date and time at which the message is no longer valid, as specified by theExpiresheader.- Since:
- 5.2
-
setExpires
public void setExpires(long expires) Set the date and time at which the message is no longer valid, as specified by theExpiresheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT.
-
getExpires
public long getExpires()Return the date and time at which the message is no longer valid, as specified by theExpiresheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
- See Also:
-
setHost
Set the (new) value of theHostheader.If the given port is
0, the host header will only contain the host name.- Since:
- 5.0
-
getHost
Return the value of theHostheader, if available.If the header value does not contain a port, the port in the returned address will be
0.- Since:
- 5.0
-
setIfMatch
Set the (new) value of theIf-Matchheader.- Since:
- 4.3
-
setIfMatch
Set the (new) value of theIf-Matchheader.- Since:
- 4.3
-
getIfMatch
Return the value of theIf-Matchheader.- Throws:
IllegalArgumentException- if parsing fails- Since:
- 4.3
-
setIfModifiedSince
Set the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
-
setIfModifiedSince
Set the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
-
setIfModifiedSince
public void setIfModifiedSince(long ifModifiedSince) Set the (new) value of theIf-Modified-Sinceheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT.
-
getIfModifiedSince
public long getIfModifiedSince()Return the value of theIf-Modified-Sinceheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
- See Also:
-
setIfNoneMatch
Set the (new) value of theIf-None-Matchheader. -
setIfNoneMatch
Set the (new) values of theIf-None-Matchheader. -
getIfNoneMatch
Return the value of theIf-None-Matchheader.- Throws:
IllegalArgumentException- if parsing fails
-
setIfUnmodifiedSince
Set the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
-
setIfUnmodifiedSince
Set the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
-
setIfUnmodifiedSince
public void setIfUnmodifiedSince(long ifUnmodifiedSince) Set the (new) value of theIf-Unmodified-Sinceheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT.
- Since:
- 4.3
-
getIfUnmodifiedSince
public long getIfUnmodifiedSince()Return the value of theIf-Unmodified-Sinceheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
- Since:
- 4.3
- See Also:
-
setLastModified
Set the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
-
setLastModified
Set the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
-
setLastModified
public void setLastModified(long lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT.
-
getLastModified
public long getLastModified()Return the time the resource was last changed, as specified by theLast-Modifiedheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
- See Also:
-
setLocation
Set the (new) location of a resource, as specified by theLocationheader. -
getLocation
Return the (new) location of a resource as specified by theLocationheader.Returns
nullwhen the location is unknown. -
setOrigin
Set the (new) value of theOriginheader. -
getOrigin
Return the value of theOriginheader. -
setPragma
Set the (new) value of thePragmaheader. -
getPragma
Return the value of thePragmaheader. -
setRange
Sets the (new) value of theRangeheader. -
getRange
Return the value of theRangeheader.Returns an empty list when the range is unknown.
-
setUpgrade
Set the (new) value of theUpgradeheader. -
getUpgrade
Return the value of theUpgradeheader. -
setVary
Set the request header names (for example, "Accept-Language") for which the response is subject to content negotiation and variances based on the value of those request headers.- Parameters:
requestHeaders- the request header names- Since:
- 4.3
-
getVary
Return the request header names subject to content negotiation.- Since:
- 4.3
-
setZonedDateTime
Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter. The equivalent ofset(String, String)but for date headers.- Since:
- 5.0
-
setInstant
Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter. The equivalent ofset(String, String)but for date headers.- Since:
- 5.1.4
-
setDate
Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter. The equivalent ofset(String, String)but for date headers.- Since:
- 3.2.4
- See Also:
-
getFirstDate
Parse the first header value for the given header name as a date, return -1 if there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.- Parameters:
headerName- the header name- Returns:
- the parsed date header, or -1 if none
- Since:
- 3.2.4
- See Also:
-
getFirstZonedDateTime
Parse the first header value for the given header name as a date, returnnullif there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.- Parameters:
headerName- the header name- Returns:
- the parsed date header, or
nullif none - Since:
- 5.0
-
getValuesAsList
Return all values of a given header name, even if this header is set multiple times.This method supports double-quoted values, as described in RFC 9110, section 5.5.
- Parameters:
headerName- the header name- Returns:
- all associated values
- Since:
- 4.3
-
clearContentHeaders
public void clearContentHeaders()Remove the well-known"Content-*"HTTP headers.Such headers should be cleared from the response if the intended body can't be written due to errors.
- Since:
- 5.2.3
-
getETagValuesAsList
Retrieve a combined result from the field values of the ETag header.- Parameters:
name- the header name- Returns:
- the combined result
- Throws:
IllegalArgumentException- if parsing fails- Since:
- 4.3
-
getFieldValues
Retrieve a combined result from the field values of multivalued headers.- Parameters:
headerName- the header name- Returns:
- the combined result
- Since:
- 4.3
-
toCommaDelimitedString
Turn the given list of header values into a comma-delimited result.- Parameters:
headerValues- the list of header values- Returns:
- a combined result with comma delimitation
-
getFirst
Return the first header value for the given header name, if any.- Specified by:
getFirstin interfaceMultiValueMap<String,String> - Parameters:
headerName- the header name- Returns:
- the first header value, or
nullif none
-
add
Add the given, single header value under the given name.- Specified by:
addin interfaceMultiValueMap<String,String> - Parameters:
headerName- the header nameheaderValue- the header value- Throws:
UnsupportedOperationException- if adding headers is not supported- See Also:
-
addAll
Description copied from interface:MultiValueMapAdd all the values of the given list to the current list of values for the given key.- Specified by:
addAllin interfaceMultiValueMap<String,String> - Parameters:
key- they keyvalues- the values to be added
-
addAll
Description copied from interface:MultiValueMapAdd all the values of the givenMultiValueMapto the current values.- Specified by:
addAllin interfaceMultiValueMap<String,String> - Parameters:
values- the values to be added
-
set
Set the given, single header value under the given name.- Specified by:
setin interfaceMultiValueMap<String,String> - Parameters:
headerName- the header nameheaderValue- the header value- Throws:
UnsupportedOperationException- if adding headers is not supported- See Also:
-
setAll
Description copied from interface:MultiValueMapSet the given values under.- Specified by:
setAllin interfaceMultiValueMap<String,String> - Parameters:
values- the values.
-
toSingleValueMap
Description copied from interface:MultiValueMapReturn aMapwith the first values contained in thisMultiValueMap. The difference between this method andMultiValueMap.asSingleValueMap()is that this method returns a copy of the entries of this map, whereas the latter returns a view.- Specified by:
toSingleValueMapin interfaceMultiValueMap<String,String> - Returns:
- a single value representation of this map
-
asSingleValueMap
Description copied from interface:MultiValueMapReturn this map as aMapwith the first values contained in thisMultiValueMap.The difference between this method and
MultiValueMap.toSingleValueMap()is that this method returns a view of the entries of this map, whereas the latter returns a copy.- Specified by:
asSingleValueMapin interfaceMultiValueMap<String,String> - Returns:
- a single value representation of this map
-
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<String,List<String>>
-
containsValue
- Specified by:
containsValuein interfaceMap<String,List<String>>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
putIfAbsent
- Specified by:
putIfAbsentin interfaceMap<String,List<String>>
-
size
public int size()Return the number of headers in the collection. This can be inflated, seeclass level javadoc. -
keySet
Return aSetview of header names. This can include multiple casing variants of a given header name, seeclass level javadoc. -
values
Return aCollectionview of all the header values, reconstructed from iterating over thekeySet(). This can include duplicates if multiple casing variants of a given header name are tracked, seeclass level javadoc. -
entrySet
Return aSetviews of header entries, reconstructed from iterating over thekeySet(). This can include duplicate entries if multiple casing variants of a given header name are tracked, seeclass level javadoc. -
forEach
Perform an action over each header, as when iterated viaentrySet(). This can include duplicate entries if multiple casing variants of a given header name are tracked, seeclass level javadoc. -
headerSet
Return a view of the headers as an entrySetof key-list pairs. BothIterator.remove()andMap.Entry.setValue(V)are supported and mutate the headers.This collection is guaranteed to contain one entry per header name even if the backing structure stores multiple casing variants of names, at the cost of first copying the names into a case-insensitive set for filtering the iteration.
- Returns:
- a
Setview that iterates over all headers in a case-insensitive manner - Since:
- 6.1.15
-
equals
-
hashCode
public int hashCode() -
toString
-
readOnlyHttpHeaders
Apply a read-onlyHttpHeaderswrapper around the given headers, if necessary.Also caches the parsed representations of the "Accept" and "Content-Type" headers.
- Parameters:
headers- the headers to expose- Returns:
- a read-only variant of the headers, or the original headers as-is
(in case it happens to be a read-only
HttpHeadersinstance already) - Since:
- 5.3
-
readOnlyHttpHeaders
Apply a read-onlyHttpHeaderswrapper around the given headers, if necessary.Also caches the parsed representations of the "Accept" and "Content-Type" headers.
- Parameters:
headers- the headers to expose- Returns:
- a read-only variant of the headers, or the original headers as-is if already read-only
-
writableHttpHeaders
@Deprecated(since="6.2", forRemoval=true) public static HttpHeaders writableHttpHeaders(HttpHeaders headers) Deprecated, for removal: This API element is subject to removal in a future version.as of 6.2 in favor ofHttpHeaders(MultiValueMap).Remove any read-only wrapper that may have been previously applied around the given headers viareadOnlyHttpHeaders(HttpHeaders).Once the writable instance is mutated, the read-only instance is likely to be out of sync and should be discarded.
- Parameters:
headers- the headers to expose- Returns:
- a writable variant of the headers, or the original headers as-is
- Since:
- 5.1.1
-
formatHeaders
Helps to format HTTP header values, as HTTP header values themselves can contain comma-separated values, can become confusing with regularMapformatting that also uses commas between entries.Additionally, this method displays the native list of header names with the mention
with native header namesif the underlying implementation stores multiple casing variants of header names (seeclass level javadoc).- Parameters:
headers- the headers to format- Returns:
- the headers to a String
- Since:
- 5.1.4
-
encodeBasicAuth
Encode the given username and password into Basic Authentication credentials.The encoded credentials returned by this method can be supplied to
setBasicAuth(String)to set the Basic Authentication header.- Parameters:
username- the usernamepassword- the passwordcharset- the charset to use to convert the credentials into an octet sequence. Defaults to ISO-8859-1.- Throws:
IllegalArgumentException- ifusernameorpasswordcontains characters that cannot be encoded to the given charset- Since:
- 5.2
- See Also:
-
HttpHeaders(MultiValueMap).