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
Modifier and TypeFieldDescriptionstatic final String
The HTTPAccept
header field name.static final String
The HTTPAccept-Charset
header field name.static final String
The HTTPAccept-Encoding
header field name.static final String
The HTTPAccept-Language
header field name.static final String
The HTTPAccept-Patch
header field name.static final String
The HTTPAccept-Ranges
header field name.static final String
The CORSAccess-Control-Allow-Credentials
response header field name.static final String
The CORSAccess-Control-Allow-Headers
response header field name.static final String
The CORSAccess-Control-Allow-Methods
response header field name.static final String
The CORSAccess-Control-Allow-Origin
response header field name.static final String
The CORSAccess-Control-Expose-Headers
response header field name.static final String
The CORSAccess-Control-Max-Age
response header field name.static final String
The CORSAccess-Control-Request-Headers
request header field name.static final String
The CORSAccess-Control-Request-Method
request header field name.static final String
The HTTPAge
header field name.static final String
The HTTPAllow
header field name.static final String
The HTTPAuthorization
header field name.static final String
The HTTPCache-Control
header field name.static final String
The HTTPConnection
header field name.static final String
The HTTPContent-Disposition
header field name.static final String
The HTTPContent-Encoding
header field name.static final String
The HTTPContent-Language
header field name.static final String
The HTTPContent-Length
header field name.static final String
The HTTPContent-Location
header field name.static final String
The HTTPContent-Range
header field name.static final String
The HTTPContent-Type
header field name.static final String
The HTTPCookie
header field name.static final String
The HTTPDate
header field name.static final HttpHeaders
An emptyHttpHeaders
instance (immutable).static final String
The HTTPETag
header field name.static final String
The HTTPExpect
header field name.static final String
The HTTPExpires
header field name.static final String
The HTTPFrom
header field name.static final String
The HTTPHost
header field name.static final String
The HTTPIf-Match
header field name.static final String
The HTTPIf-Modified-Since
header field name.static final String
The HTTPIf-None-Match
header field name.static final String
The HTTPIf-Range
header field name.static final String
The HTTPIf-Unmodified-Since
header field name.static final String
The HTTPLast-Modified
header field name.static final String
The HTTPLink
header field name.static final String
The HTTPLocation
header field name.static final String
The HTTPMax-Forwards
header field name.static final String
The HTTPOrigin
header field name.static final String
The HTTPPragma
header field name.static final String
The HTTPProxy-Authenticate
header field name.static final String
The HTTPProxy-Authorization
header field name.static final String
The HTTPRange
header field name.static final String
The HTTPReferer
header field name.static final String
The HTTPRetry-After
header field name.static final String
The HTTPServer
header field name.static final String
The HTTPSet-Cookie
header field name.static final String
The HTTPSet-Cookie2
header field name.static final String
The HTTPTE
header field name.static final String
The HTTPTrailer
header field name.static final String
The HTTPTransfer-Encoding
header field name.static final String
The HTTPUpgrade
header field name.static final String
The HTTPUser-Agent
header field name.static final String
The HTTPVary
header field name.static final String
The HTTPVia
header field name.static final String
The HTTPWarning
header field name.static final String
The HTTPWWW-Authenticate
header field name. -
Constructor Summary
ConstructorDescriptionConstruct a new, empty instance of theHttpHeaders
object using an underlying case-insensitive map.HttpHeaders
(MultiValueMap<String, String> headers) Construct a newHttpHeaders
instance backed by an existing map. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add the given, single header value under the given name.void
Add all the values of the given list to the current list of values for the given key.void
addAll
(MultiValueMap<String, String> values) Add all the values of the givenMultiValueMap
to the current values.Return this map as aMap
with the first values contained in thisMultiValueMap
.void
clear()
void
Remove the well-known"Content-*"
HTTP headers.boolean
containsKey
(Object key) boolean
containsValue
(Object value) static String
encodeBasicAuth
(String username, String password, Charset charset) Encode the given username and password into Basic Authentication credentials.entrySet()
boolean
void
forEach
(BiConsumer<? super String, ? super List<String>> action) Perform an action over each header, as when iterated viaentrySet()
.static String
formatHeaders
(MultiValueMap<String, String> headers) Helps to format HTTP header values, as HTTP header values themselves can contain comma-separated values, can become confusing with regularMap
formatting that also uses commas between entries.Return the list of acceptable media types, as specified by theAccept
header.Return the list of acceptable charsets, as specified by theAccept-Charset
header.Return the language ranges from the "Accept-Language" header.Return the list of acceptable media types forPATCH
methods, as specified by theAccept-Patch
header.boolean
Return the value of theAccess-Control-Allow-Credentials
response header.Return the value of theAccess-Control-Allow-Headers
response header.Return the value of theAccess-Control-Allow-Methods
response header.Return the value of theAccess-Control-Allow-Origin
response header.Return the value of theAccess-Control-Expose-Headers
response header.long
Return the value of theAccess-Control-Max-Age
response header.Return the value of theAccess-Control-Request-Headers
request header.Return the value of theAccess-Control-Request-Method
request header.getAllow()
Return the set of allowedHTTP methods
, as specified by theAllow
header.Return the value of theCache-Control
header.Return the value of theConnection
header.Return a parsed representation of the Content-Disposition header.Get the firstLocale
of the content languages, as specified by theContent-Language
header.long
Return the length of the body in bytes, as specified by theContent-Length
header.Return the media type of the body, as specified by theContent-Type
header.long
getDate()
Return the date and time at which the message was created, as specified by theDate
header.getETag()
Return the entity tag of the body, as specified by theETag
header.getETagValuesAsList
(String name) Retrieve a combined result from the field values of the ETag header.long
Return the date and time at which the message is no longer valid, as specified by theExpires
header.protected String
getFieldValues
(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.long
getFirstDate
(String headerName) Parse the first header value for the given header name as a date, return -1 if there is no value, or raiseIllegalArgumentException
if the value cannot be parsed as a date.getFirstZonedDateTime
(String headerName) Parse the first header value for the given header name as a date, returnnull
if there is no value, or raiseIllegalArgumentException
if the value cannot be parsed as a date.getHost()
Return the value of theHost
header, if available.Return the value of theIf-Match
header.long
Return the value of theIf-Modified-Since
header.Return the value of theIf-None-Match
header.long
Return the value of theIf-Unmodified-Since
header.long
Return the time the resource was last changed, as specified by theLast-Modified
header.Return the (new) location of a resource as specified by theLocation
header.getOrEmpty
(Object headerName) Get the list of header values for the given header name, if any.Return the value of theOrigin
header.Return the value of thePragma
header.getRange()
Return the value of theRange
header.Return the value of theUpgrade
header.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.int
hashCode()
Return a view of the headers as an entrySet
of key-list pairs.boolean
isEmpty()
keySet()
Return aSet
view of header names.void
putIfAbsent
(String key, List<String> value) static HttpHeaders
readOnlyHttpHeaders
(HttpHeaders headers) Apply a read-onlyHttpHeaders
wrapper around the given headers, if necessary.static HttpHeaders
readOnlyHttpHeaders
(MultiValueMap<String, String> headers) Apply a read-onlyHttpHeaders
wrapper around the given headers, if necessary.void
Set the given, single header value under the given name.void
Set the list of acceptable media types, as specified by theAccept
header.void
setAcceptCharset
(List<Charset> acceptableCharsets) Set the list of acceptable charsets, as specified by theAccept-Charset
header.void
setAcceptLanguage
(List<Locale.LanguageRange> languages) Set the acceptable language ranges, as specified by the Accept-Language header.void
setAcceptLanguageAsLocales
(List<Locale> locales) Variant ofsetAcceptLanguage(List)
usingLocale
's.void
setAcceptPatch
(List<MediaType> mediaTypes) void
setAccessControlAllowCredentials
(boolean allowCredentials) Set the (new) value of theAccess-Control-Allow-Credentials
response header.void
setAccessControlAllowHeaders
(List<String> allowedHeaders) Set the (new) value of theAccess-Control-Allow-Headers
response header.void
setAccessControlAllowMethods
(List<HttpMethod> allowedMethods) Set the (new) value of theAccess-Control-Allow-Methods
response header.void
setAccessControlAllowOrigin
(String allowedOrigin) Set the (new) value of theAccess-Control-Allow-Origin
response header.void
setAccessControlExposeHeaders
(List<String> exposedHeaders) Set the (new) value of theAccess-Control-Expose-Headers
response header.void
setAccessControlMaxAge
(long maxAge) Set the (new) value of theAccess-Control-Max-Age
response header.void
setAccessControlMaxAge
(Duration maxAge) Set the (new) value of theAccess-Control-Max-Age
response header.void
setAccessControlRequestHeaders
(List<String> requestHeaders) Set the (new) value of theAccess-Control-Request-Headers
request header.void
setAccessControlRequestMethod
(HttpMethod requestMethod) Set the (new) value of theAccess-Control-Request-Method
request header.void
Set the given values under.void
setAllow
(Set<HttpMethod> allowedMethods) Set the set of allowedHTTP methods
, as specified by theAllow
header.void
setBasicAuth
(String encodedCredentials) Set the value of the Authorization header to Basic Authentication based on the given encoded credentials.void
setBasicAuth
(String username, String password) Set the value of the Authorization header to Basic Authentication based on the given username and password.void
setBasicAuth
(String username, String password, Charset charset) Set the value of the Authorization header to Basic Authentication based on the given username and password.void
setBearerAuth
(String token) Set the value of the Authorization header to the given Bearer token.void
setCacheControl
(String cacheControl) Set the (new) value of theCache-Control
header.void
setCacheControl
(CacheControl cacheControl) Set a configuredCacheControl
instance as the new value of theCache-Control
header.void
setConnection
(String connection) Set the (new) value of theConnection
header.void
setConnection
(List<String> connection) Set the (new) value of theConnection
header.void
setContentDisposition
(ContentDisposition contentDisposition) Set the Content-Disposition header.void
setContentDispositionFormData
(String name, String filename) Set theContent-Disposition
header when creating a"multipart/form-data"
request.void
setContentLanguage
(Locale locale) Set theLocale
of the content language, as specified by the Content-Language header.void
setContentLength
(long contentLength) Set the length of the body in bytes, as specified by theContent-Length
header.void
setContentType
(MediaType mediaType) Set the media type of the body, as specified by theContent-Type
header.void
setDate
(long date) Set the date and time at which the message was created, as specified by theDate
header.void
Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.void
Set the date and time at which the message was created, as specified by theDate
header.void
setDate
(ZonedDateTime date) Set the date and time at which the message was created, as specified by theDate
header.void
Set the (new) entity tag of the body, as specified by theETag
header.void
setExpires
(long expires) Set the date and time at which the message is no longer valid, as specified by theExpires
header.void
setExpires
(Instant expires) Set the date and time at which the message is no longer valid, as specified by theExpires
header.void
setExpires
(ZonedDateTime expires) Set the duration after which the message is no longer valid, as specified by theExpires
header.void
setHost
(InetSocketAddress host) Set the (new) value of theHost
header.void
setIfMatch
(String ifMatch) Set the (new) value of theIf-Match
header.void
setIfMatch
(List<String> ifMatchList) Set the (new) value of theIf-Match
header.void
setIfModifiedSince
(long ifModifiedSince) Set the (new) value of theIf-Modified-Since
header.void
setIfModifiedSince
(Instant ifModifiedSince) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setIfModifiedSince
(ZonedDateTime ifModifiedSince) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setIfNoneMatch
(String ifNoneMatch) Set the (new) value of theIf-None-Match
header.void
setIfNoneMatch
(List<String> ifNoneMatchList) Set the (new) values of theIf-None-Match
header.void
setIfUnmodifiedSince
(long ifUnmodifiedSince) Set the (new) value of theIf-Unmodified-Since
header.void
setIfUnmodifiedSince
(Instant ifUnmodifiedSince) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setIfUnmodifiedSince
(ZonedDateTime ifUnmodifiedSince) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setInstant
(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.void
setLastModified
(long lastModified) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setLastModified
(Instant lastModified) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setLastModified
(ZonedDateTime lastModified) Set the time the resource was last changed, as specified by theLast-Modified
header.void
setLocation
(URI location) Set the (new) location of a resource, as specified by theLocation
header.void
Set the (new) value of theOrigin
header.void
Set the (new) value of thePragma
header.void
Sets the (new) value of theRange
header.void
setUpgrade
(String upgrade) Set the (new) value of theUpgrade
header.void
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.void
setZonedDateTime
(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.int
size()
Return the number of headers in the collection.protected String
toCommaDelimitedString
(List<String> headerValues) Turn the given list of header values into a comma-delimited result.Return aMap
with the first values contained in thisMultiValueMap
.toString()
values()
Return aCollection
view of all the header values, reconstructed from iterating over thekeySet()
.static HttpHeaders
writableHttpHeaders
(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, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, getOrDefault, merge, remove, replace, replace, replaceAll
Methods inherited from interface org.springframework.util.MultiValueMap
addIfAbsent
-
Field Details
-
ACCEPT
The HTTPAccept
header field name. -
ACCEPT_CHARSET
The HTTPAccept-Charset
header field name. -
ACCEPT_ENCODING
The HTTPAccept-Encoding
header field name. -
ACCEPT_LANGUAGE
The HTTPAccept-Language
header field name. -
ACCEPT_PATCH
The HTTPAccept-Patch
header field name.- Since:
- 5.3.6
- See Also:
-
ACCEPT_RANGES
The HTTPAccept-Ranges
header field name. -
ACCESS_CONTROL_ALLOW_CREDENTIALS
The CORSAccess-Control-Allow-Credentials
response header field name.- See Also:
-
ACCESS_CONTROL_ALLOW_HEADERS
The CORSAccess-Control-Allow-Headers
response header field name.- See Also:
-
ACCESS_CONTROL_ALLOW_METHODS
The CORSAccess-Control-Allow-Methods
response header field name.- See Also:
-
ACCESS_CONTROL_ALLOW_ORIGIN
The CORSAccess-Control-Allow-Origin
response header field name.- See Also:
-
ACCESS_CONTROL_EXPOSE_HEADERS
The CORSAccess-Control-Expose-Headers
response header field name.- See Also:
-
ACCESS_CONTROL_MAX_AGE
The CORSAccess-Control-Max-Age
response header field name.- See Also:
-
ACCESS_CONTROL_REQUEST_HEADERS
The CORSAccess-Control-Request-Headers
request header field name.- See Also:
-
ACCESS_CONTROL_REQUEST_METHOD
The CORSAccess-Control-Request-Method
request header field name.- See Also:
-
AGE
The HTTPAge
header field name.- See Also:
-
ALLOW
The HTTPAllow
header field name. -
AUTHORIZATION
The HTTPAuthorization
header field name.- See Also:
-
CACHE_CONTROL
The HTTPCache-Control
header field name.- See Also:
-
CONNECTION
The HTTPConnection
header field name.- See Also:
-
CONTENT_ENCODING
The HTTPContent-Encoding
header field name. -
CONTENT_DISPOSITION
The HTTPContent-Disposition
header field name.- See Also:
-
CONTENT_LANGUAGE
The HTTPContent-Language
header field name. -
CONTENT_LENGTH
The HTTPContent-Length
header field name. -
CONTENT_LOCATION
The HTTPContent-Location
header field name. -
CONTENT_RANGE
The HTTPContent-Range
header field name.- See Also:
-
CONTENT_TYPE
The HTTPContent-Type
header field name. -
COOKIE
The HTTPCookie
header field name. -
DATE
The HTTPDate
header field name. -
ETAG
The HTTPETag
header field name.- See Also:
-
EXPECT
The HTTPExpect
header field name. -
EXPIRES
The HTTPExpires
header field name.- See Also:
-
FROM
The HTTPFrom
header field name. -
HOST
The HTTPHost
header field name.- See Also:
-
IF_MATCH
The HTTPIf-Match
header field name.- See Also:
-
IF_MODIFIED_SINCE
The HTTPIf-Modified-Since
header field name.- See Also:
-
IF_NONE_MATCH
The HTTPIf-None-Match
header field name.- See Also:
-
IF_RANGE
The HTTPIf-Range
header field name.- See Also:
-
IF_UNMODIFIED_SINCE
The HTTPIf-Unmodified-Since
header field name.- See Also:
-
LAST_MODIFIED
The HTTPLast-Modified
header field name.- See Also:
-
LINK
The HTTPLink
header field name.- See Also:
-
LOCATION
The HTTPLocation
header field name. -
MAX_FORWARDS
The HTTPMax-Forwards
header field name. -
ORIGIN
The HTTPOrigin
header field name.- See Also:
-
PRAGMA
The HTTPPragma
header field name.- See Also:
-
PROXY_AUTHENTICATE
The HTTPProxy-Authenticate
header field name.- See Also:
-
PROXY_AUTHORIZATION
The HTTPProxy-Authorization
header field name.- See Also:
-
RANGE
The HTTPRange
header field name.- See Also:
-
REFERER
The HTTPReferer
header field name. -
RETRY_AFTER
The HTTPRetry-After
header field name. -
SERVER
The HTTPServer
header field name. -
SET_COOKIE
The HTTPSet-Cookie
header field name. -
SET_COOKIE2
The HTTPSet-Cookie2
header field name.- See Also:
-
TE
The HTTPTE
header field name.- See Also:
-
TRAILER
The HTTPTrailer
header field name.- See Also:
-
TRANSFER_ENCODING
The HTTPTransfer-Encoding
header field name. -
UPGRADE
The HTTPUpgrade
header field name.- See Also:
-
USER_AGENT
The HTTPUser-Agent
header field name. -
VARY
The HTTPVary
header field name. -
VIA
The HTTPVia
header field name. -
WARNING
The HTTPWarning
header field name.- See Also:
-
WWW_AUTHENTICATE
The HTTPWWW-Authenticate
header field name.- See Also:
-
EMPTY
An emptyHttpHeaders
instance (immutable).- Since:
- 5.0
-
-
Constructor Details
-
HttpHeaders
public HttpHeaders()Construct a new, empty instance of theHttpHeaders
object using an underlying case-insensitive map. -
HttpHeaders
Construct a newHttpHeaders
instance 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 theAccept
header. -
getAccept
Return the list of acceptable media types, as specified by theAccept
header.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 forPATCH
methods, as specified by theAccept-Patch
header.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-Credentials
response header. -
getAccessControlAllowCredentials
public boolean getAccessControlAllowCredentials()Return the value of theAccess-Control-Allow-Credentials
response header. -
setAccessControlAllowHeaders
Set the (new) value of theAccess-Control-Allow-Headers
response header. -
getAccessControlAllowHeaders
Return the value of theAccess-Control-Allow-Headers
response header. -
setAccessControlAllowMethods
Set the (new) value of theAccess-Control-Allow-Methods
response header. -
getAccessControlAllowMethods
Return the value of theAccess-Control-Allow-Methods
response header. -
setAccessControlAllowOrigin
Set the (new) value of theAccess-Control-Allow-Origin
response header. -
getAccessControlAllowOrigin
Return the value of theAccess-Control-Allow-Origin
response header. -
setAccessControlExposeHeaders
Set the (new) value of theAccess-Control-Expose-Headers
response header. -
getAccessControlExposeHeaders
Return the value of theAccess-Control-Expose-Headers
response header. -
setAccessControlMaxAge
Set the (new) value of theAccess-Control-Max-Age
response header.- Since:
- 5.2
-
setAccessControlMaxAge
public void setAccessControlMaxAge(long maxAge) Set the (new) value of theAccess-Control-Max-Age
response header. -
getAccessControlMaxAge
public long getAccessControlMaxAge()Return the value of theAccess-Control-Max-Age
response header.Returns -1 when the max age is unknown.
-
setAccessControlRequestHeaders
Set the (new) value of theAccess-Control-Request-Headers
request header. -
getAccessControlRequestHeaders
Return the value of theAccess-Control-Request-Headers
request header. -
setAccessControlRequestMethod
Set the (new) value of theAccess-Control-Request-Method
request header. -
getAccessControlRequestMethod
Return the value of theAccess-Control-Request-Method
request header. -
setAcceptCharset
Set the list of acceptable charsets, as specified by theAccept-Charset
header. -
getAcceptCharset
Return the list of acceptable charsets, as specified by theAccept-Charset
header. -
setAllow
Set the set of allowedHTTP methods
, as specified by theAllow
header. -
getAllow
Return the set of allowedHTTP methods
, as specified by theAllow
header.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-1
character set.- Parameters:
username
- the usernamepassword
- the password- Throws:
IllegalArgumentException
- if eitheruser
orpassword
contain 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
- ifusername
orpassword
contains 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 isnull
or 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 configuredCacheControl
instance as the new value of theCache-Control
header.- Since:
- 5.0.5
-
setCacheControl
Set the (new) value of theCache-Control
header. -
getCacheControl
Return the value of theCache-Control
header. -
setConnection
Set the (new) value of theConnection
header. -
setConnection
Set the (new) value of theConnection
header. -
getConnection
Return the value of theConnection
header. -
setContentDispositionFormData
Set theContent-Disposition
header 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 aResource
for each part, and then pass that to theRestTemplate
orWebClient
.- 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 theLocale
of 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 firstLocale
of the content languages, as specified by theContent-Language
header.Use
getValuesAsList(String)
if you need to get multiple content languages.- Returns:
- the first
Locale
of the content languages, ornull
if unknown - Since:
- 5.0
-
setContentLength
public void setContentLength(long contentLength) Set the length of the body in bytes, as specified by theContent-Length
header.- 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-Length
header.Returns -1 when the content-length is unknown.
-
setContentType
Set the media type of the body, as specified by theContent-Type
header. -
getContentType
Return the media type of the body, as specified by theContent-Type
header.Returns
null
when theContent-Type
header 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 theDate
header.- Since:
- 5.2
-
setDate
Set the date and time at which the message was created, as specified by theDate
header.- Since:
- 5.2
-
setDate
public void setDate(long date) Set the date and time at which the message was created, as specified by theDate
header.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 theDate
header.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 theETag
header. -
getETag
Return the entity tag of the body, as specified by theETag
header. -
setExpires
Set the duration after which the message is no longer valid, as specified by theExpires
header.- Since:
- 5.0.5
-
setExpires
Set the date and time at which the message is no longer valid, as specified by theExpires
header.- 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 theExpires
header.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 theExpires
header.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 theHost
header.If the given port is
0
, the host header will only contain the host name.- Since:
- 5.0
-
getHost
Return the value of theHost
header, 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-Match
header.- Since:
- 4.3
-
setIfMatch
Set the (new) value of theIf-Match
header.- Since:
- 4.3
-
getIfMatch
Return the value of theIf-Match
header.- Throws:
IllegalArgumentException
- if parsing fails- Since:
- 4.3
-
setIfModifiedSince
Set the time the resource was last changed, as specified by theLast-Modified
header.- Since:
- 5.1.4
-
setIfModifiedSince
Set the time the resource was last changed, as specified by theLast-Modified
header.- Since:
- 5.1.4
-
setIfModifiedSince
public void setIfModifiedSince(long ifModifiedSince) Set the (new) value of theIf-Modified-Since
header.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-Since
header.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-Match
header. -
setIfNoneMatch
Set the (new) values of theIf-None-Match
header. -
getIfNoneMatch
Return the value of theIf-None-Match
header.- Throws:
IllegalArgumentException
- if parsing fails
-
setIfUnmodifiedSince
Set the time the resource was last changed, as specified by theLast-Modified
header.- Since:
- 5.1.4
-
setIfUnmodifiedSince
Set the time the resource was last changed, as specified by theLast-Modified
header.- Since:
- 5.1.4
-
setIfUnmodifiedSince
public void setIfUnmodifiedSince(long ifUnmodifiedSince) Set the (new) value of theIf-Unmodified-Since
header.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-Since
header.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-Modified
header.- Since:
- 5.1.4
-
setLastModified
Set the time the resource was last changed, as specified by theLast-Modified
header.- Since:
- 5.1.4
-
setLastModified
public void setLastModified(long lastModified) Set the time the resource was last changed, as specified by theLast-Modified
header.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-Modified
header.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 theLocation
header. -
getLocation
Return the (new) location of a resource as specified by theLocation
header.Returns
null
when the location is unknown. -
setOrigin
Set the (new) value of theOrigin
header. -
getOrigin
Return the value of theOrigin
header. -
setPragma
Set the (new) value of thePragma
header. -
getPragma
Return the value of thePragma
header. -
setRange
Sets the (new) value of theRange
header. -
getRange
Return the value of theRange
header.Returns an empty list when the range is unknown.
-
setUpgrade
Set the (new) value of theUpgrade
header. -
getUpgrade
Return the value of theUpgrade
header. -
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 raiseIllegalArgumentException
if 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, returnnull
if there is no value, or raiseIllegalArgumentException
if the value cannot be parsed as a date.- Parameters:
headerName
- the header name- Returns:
- the parsed date header, or
null
if 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:
getFirst
in interfaceMultiValueMap<String,
String> - Parameters:
headerName
- the header name- Returns:
- the first header value, or
null
if none
-
add
Add the given, single header value under the given name.- Specified by:
add
in 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:MultiValueMap
Add all the values of the given list to the current list of values for the given key.- Specified by:
addAll
in interfaceMultiValueMap<String,
String> - Parameters:
key
- they keyvalues
- the values to be added
-
addAll
Description copied from interface:MultiValueMap
Add all the values of the givenMultiValueMap
to the current values.- Specified by:
addAll
in interfaceMultiValueMap<String,
String> - Parameters:
values
- the values to be added
-
set
Set the given, single header value under the given name.- Specified by:
set
in 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:MultiValueMap
Set the given values under.- Specified by:
setAll
in interfaceMultiValueMap<String,
String> - Parameters:
values
- the values.
-
toSingleValueMap
Description copied from interface:MultiValueMap
Return aMap
with 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:
toSingleValueMap
in interfaceMultiValueMap<String,
String> - Returns:
- a single value representation of this map
-
asSingleValueMap
Description copied from interface:MultiValueMap
Return this map as aMap
with 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:
asSingleValueMap
in interfaceMultiValueMap<String,
String> - Returns:
- a single value representation of this map
-
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKey
in interfaceMap<String,
List<String>>
-
containsValue
- Specified by:
containsValue
in interfaceMap<String,
List<String>>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
putIfAbsent
- Specified by:
putIfAbsent
in 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 aSet
view of header names. This can include multiple casing variants of a given header name, seeclass level javadoc
. -
values
Return aCollection
view 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 aSet
views 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 entrySet
of 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
Set
view that iterates over all headers in a case-insensitive manner - Since:
- 6.1.15
-
equals
-
hashCode
public int hashCode() -
toString
-
readOnlyHttpHeaders
Apply a read-onlyHttpHeaders
wrapper 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
HttpHeaders
instance already) - Since:
- 5.3
-
readOnlyHttpHeaders
Apply a read-onlyHttpHeaders
wrapper 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 regularMap
formatting that also uses commas between entries.Additionally, this method displays the native list of header names with the mention
with native header names
if 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
- ifusername
orpassword
contains characters that cannot be encoded to the given charset- Since:
- 5.2
- See Also:
-
HttpHeaders(MultiValueMap)
.