Class MediaType
- All Implemented Interfaces:
Serializable
,Comparable<MimeType>
MimeType
that adds support for quality parameters
as defined in the HTTP specification.- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev, Sebastien Deleuze, Kazuki Shimizu, Sam Brannen
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.util.MimeType
MimeType.SpecificityComparator<T extends MimeType>
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MediaType
Public constant media type that includes all media ranges (i.e.static final String
A String equivalent ofALL
.static final MediaType
Public constant media type forapplication/atom+xml
.static final String
A String equivalent ofAPPLICATION_ATOM_XML
.static final MediaType
Public constant media type forapplication/cbor
.static final String
A String equivalent ofAPPLICATION_CBOR
.static final MediaType
Public constant media type forapplication/x-www-form-urlencoded
.static final String
A String equivalent ofAPPLICATION_FORM_URLENCODED
.static final MediaType
Public constant media type forapplication/graphql+json
.static final String
A String equivalent ofAPPLICATION_GRAPHQL
.static final MediaType
Public constant media type forapplication/json
.static final MediaType
Deprecated.static final String
Deprecated.as of 5.2 in favor ofAPPLICATION_JSON_VALUE
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.static final String
A String equivalent ofAPPLICATION_JSON
.static final MediaType
Public constant media type forapplication/x-ndjson
.static final String
A String equivalent ofAPPLICATION_NDJSON
.static final MediaType
Public constant media type forapplication/octet-stream
.static final String
A String equivalent ofAPPLICATION_OCTET_STREAM
.static final MediaType
Public constant media type forapplication/pdf
.static final String
A String equivalent ofAPPLICATION_PDF
.static final MediaType
Public constant media type forapplication/problem+json
.static final MediaType
Deprecated.as of 5.2 in favor ofAPPLICATION_PROBLEM_JSON
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.static final String
Deprecated.as of 5.2 in favor ofAPPLICATION_PROBLEM_JSON_VALUE
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.static final String
A String equivalent ofAPPLICATION_PROBLEM_JSON
.static final MediaType
Public constant media type forapplication/problem+xml
.static final String
A String equivalent ofAPPLICATION_PROBLEM_XML
.static final MediaType
Public constant media type forapplication/x-protobuf
.static final String
A String equivalent ofAPPLICATION_PROTOBUF
.static final MediaType
Public constant media type forapplication/rss+xml
.static final String
A String equivalent ofAPPLICATION_RSS_XML
.static final MediaType
Deprecated.as of 5.3, see notice onAPPLICATION_STREAM_JSON_VALUE
.static final String
Deprecated.as of 5.3 since it originates from the W3C Activity Streams specification which has a more specific purpose and has been since replaced with a different mime type.static final MediaType
Public constant media type forapplication/xhtml+xml
.static final String
A String equivalent ofAPPLICATION_XHTML_XML
.static final MediaType
Public constant media type forapplication/xml
.static final String
A String equivalent ofAPPLICATION_XML
.static final MediaType
Public constant media type forimage/gif
.static final String
A String equivalent ofIMAGE_GIF
.static final MediaType
Public constant media type forimage/jpeg
.static final String
A String equivalent ofIMAGE_JPEG
.static final MediaType
Public constant media type forimage/png
.static final String
A String equivalent ofIMAGE_PNG
.static final MediaType
Public constant media type formultipart/form-data
.static final String
A String equivalent ofMULTIPART_FORM_DATA
.static final MediaType
Public constant media type formultipart/mixed
.static final String
A String equivalent ofMULTIPART_MIXED
.static final MediaType
Public constant media type formultipart/related
.static final String
A String equivalent ofMULTIPART_RELATED
.static final Comparator<MediaType>
Deprecated.As of 6.0, with no direct replacementstatic final Comparator<MediaType>
Deprecated.As of 6.0, with no direct replacementstatic final MediaType
Public constant media type fortext/event-stream
.static final String
A String equivalent ofTEXT_EVENT_STREAM
.static final MediaType
Public constant media type fortext/html
.static final String
A String equivalent ofTEXT_HTML
.static final MediaType
Public constant media type fortext/markdown
.static final String
A String equivalent ofTEXT_MARKDOWN
.static final MediaType
Public constant media type fortext/plain
.static final String
A String equivalent ofTEXT_PLAIN
.static final MediaType
Public constant media type fortext/xml
.static final String
A String equivalent ofTEXT_XML
.Fields inherited from class org.springframework.util.MimeType
WILDCARD_TYPE
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newMediaType
for the given primary type.Create a newMediaType
for the given primary type and subtype.Create a newMediaType
for the given type, subtype, and quality value.Create a newMediaType
for the given type, subtype, and character set.Create a newMediaType
for the given type, subtype, and parameters.Copy-constructor that copies the type, subtype and parameters of the givenMediaType
, and allows to set the specified character set.Copy-constructor that copies the type and subtype of the givenMediaType
, and allows for different parameters.Create a newMediaType
for the givenMimeType
. -
Method Summary
Modifier and TypeMethodDescriptionstatic MediaType
asMediaType
(MimeType mimeType) Re-create the given mime type as a media type.asMediaTypes
(List<MimeType> mimeTypes) Re-create the given mime types as media types.protected void
checkParameters
(String parameter, String value) copyQualityValue
(MediaType mediaType) Return a replica of this instance with the quality value of the givenMediaType
.double
Return the quality factor, as indicated by aq
parameter, if any.boolean
Indicate whether thisMediaType
includes the given media type.boolean
isCompatibleWith
(MediaType other) Indicate whether thisMediaType
is compatible with the given media type.boolean
isLessSpecific
(MimeType other) Indicates whether thisMediaType
more specific than the given type.boolean
isMoreSpecific
(MimeType other) Indicates whether thisMediaType
more specific than the given type.static MediaType
parseMediaType
(String mediaType) Parse the given String into a singleMediaType
.parseMediaTypes
(String mediaTypes) Parse the comma-separated string into a list ofMediaType
objects.parseMediaTypes
(List<String> mediaTypes) Parse the given list of (potentially) comma-separated strings into a list ofMediaType
objects.Return a replica of this instance with its quality value removed.static void
sortByQualityValue
(List<MediaType> mediaTypes) Deprecated.As of 6.0, with no direct replacementstatic void
sortBySpecificity
(List<MediaType> mediaTypes) Deprecated.As of 6.0, in favor ofMimeTypeUtils.sortBySpecificity(List)
static void
sortBySpecificityAndQuality
(List<MediaType> mediaTypes) Deprecated.As of 6.0, in favor ofMimeTypeUtils.sortBySpecificity(List)
static String
toString
(Collection<MediaType> mediaTypes) Return a string representation of the given list ofMediaType
objects.static MediaType
Parse the given String value into aMediaType
object, with this method name following the 'valueOf' naming convention (as supported byConversionService
.Methods inherited from class org.springframework.util.MimeType
appendTo, compareTo, equals, equalsTypeAndSubtype, getCharset, getParameter, getParameters, getSubtype, getSubtypeSuffix, getType, hashCode, includes, isCompatibleWith, isConcrete, isPresentIn, isWildcardSubtype, isWildcardType, toString, unquote
-
Field Details
-
ALL
Public constant media type that includes all media ranges (i.e. "*/*"). -
ALL_VALUE
A String equivalent ofALL
.- See Also:
-
APPLICATION_ATOM_XML
Public constant media type forapplication/atom+xml
. -
APPLICATION_ATOM_XML_VALUE
A String equivalent ofAPPLICATION_ATOM_XML
.- See Also:
-
APPLICATION_CBOR
Public constant media type forapplication/cbor
.- Since:
- 5.2
-
APPLICATION_CBOR_VALUE
A String equivalent ofAPPLICATION_CBOR
.- Since:
- 5.2
- See Also:
-
APPLICATION_FORM_URLENCODED
Public constant media type forapplication/x-www-form-urlencoded
. -
APPLICATION_FORM_URLENCODED_VALUE
A String equivalent ofAPPLICATION_FORM_URLENCODED
.- See Also:
-
APPLICATION_GRAPHQL
Public constant media type forapplication/graphql+json
.- Since:
- 5.3.19
- See Also:
-
APPLICATION_GRAPHQL_VALUE
A String equivalent ofAPPLICATION_GRAPHQL
.- Since:
- 5.3.19
- See Also:
-
APPLICATION_JSON
Public constant media type forapplication/json
. -
APPLICATION_JSON_VALUE
A String equivalent ofAPPLICATION_JSON
. -
APPLICATION_JSON_UTF8
Deprecated.as of 5.2 in favor ofAPPLICATION_JSON
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.Public constant media type forapplication/json;charset=UTF-8
. -
APPLICATION_JSON_UTF8_VALUE
Deprecated.as of 5.2 in favor ofAPPLICATION_JSON_VALUE
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.A String equivalent ofAPPLICATION_JSON_UTF8
.- See Also:
-
APPLICATION_OCTET_STREAM
Public constant media type forapplication/octet-stream
. -
APPLICATION_OCTET_STREAM_VALUE
A String equivalent ofAPPLICATION_OCTET_STREAM
.- See Also:
-
APPLICATION_PDF
Public constant media type forapplication/pdf
.- Since:
- 4.3
-
APPLICATION_PDF_VALUE
A String equivalent ofAPPLICATION_PDF
.- Since:
- 4.3
- See Also:
-
APPLICATION_PROBLEM_JSON
Public constant media type forapplication/problem+json
.- Since:
- 5.0
- See Also:
-
APPLICATION_PROBLEM_JSON_VALUE
A String equivalent ofAPPLICATION_PROBLEM_JSON
.- Since:
- 5.0
- See Also:
-
APPLICATION_PROBLEM_JSON_UTF8
Deprecated.as of 5.2 in favor ofAPPLICATION_PROBLEM_JSON
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.Public constant media type forapplication/problem+json
.- Since:
- 5.0
- See Also:
-
APPLICATION_PROBLEM_JSON_UTF8_VALUE
Deprecated.as of 5.2 in favor ofAPPLICATION_PROBLEM_JSON_VALUE
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.A String equivalent ofAPPLICATION_PROBLEM_JSON_UTF8
.- Since:
- 5.0
- See Also:
-
APPLICATION_PROBLEM_XML
Public constant media type forapplication/problem+xml
.- Since:
- 5.0
- See Also:
-
APPLICATION_PROBLEM_XML_VALUE
A String equivalent ofAPPLICATION_PROBLEM_XML
.- Since:
- 5.0
- See Also:
-
APPLICATION_PROTOBUF
Public constant media type forapplication/x-protobuf
.- Since:
- 6.0
-
APPLICATION_PROTOBUF_VALUE
A String equivalent ofAPPLICATION_PROTOBUF
.- Since:
- 6.0
- See Also:
-
APPLICATION_RSS_XML
Public constant media type forapplication/rss+xml
.- Since:
- 4.3.6
-
APPLICATION_RSS_XML_VALUE
A String equivalent ofAPPLICATION_RSS_XML
.- Since:
- 4.3.6
- See Also:
-
APPLICATION_NDJSON
Public constant media type forapplication/x-ndjson
.- Since:
- 5.3
-
APPLICATION_NDJSON_VALUE
A String equivalent ofAPPLICATION_NDJSON
.- Since:
- 5.3
- See Also:
-
APPLICATION_STREAM_JSON
Deprecated.as of 5.3, see notice onAPPLICATION_STREAM_JSON_VALUE
.Public constant media type forapplication/stream+json
.- Since:
- 5.0
-
APPLICATION_STREAM_JSON_VALUE
Deprecated.as of 5.3 since it originates from the W3C Activity Streams specification which has a more specific purpose and has been since replaced with a different mime type. UseAPPLICATION_NDJSON
as a replacement or any other line-delimited JSON format (e.g. JSON Lines, JSON Text Sequences).A String equivalent ofAPPLICATION_STREAM_JSON
.- Since:
- 5.0
- See Also:
-
APPLICATION_XHTML_XML
Public constant media type forapplication/xhtml+xml
. -
APPLICATION_XHTML_XML_VALUE
A String equivalent ofAPPLICATION_XHTML_XML
.- See Also:
-
APPLICATION_XML
Public constant media type forapplication/xml
. -
APPLICATION_XML_VALUE
A String equivalent ofAPPLICATION_XML
.- See Also:
-
IMAGE_GIF
Public constant media type forimage/gif
. -
IMAGE_GIF_VALUE
A String equivalent ofIMAGE_GIF
.- See Also:
-
IMAGE_JPEG
Public constant media type forimage/jpeg
. -
IMAGE_JPEG_VALUE
A String equivalent ofIMAGE_JPEG
.- See Also:
-
IMAGE_PNG
Public constant media type forimage/png
. -
IMAGE_PNG_VALUE
A String equivalent ofIMAGE_PNG
.- See Also:
-
MULTIPART_FORM_DATA
Public constant media type formultipart/form-data
. -
MULTIPART_FORM_DATA_VALUE
A String equivalent ofMULTIPART_FORM_DATA
.- See Also:
-
MULTIPART_MIXED
Public constant media type formultipart/mixed
.- Since:
- 5.2
-
MULTIPART_MIXED_VALUE
A String equivalent ofMULTIPART_MIXED
.- Since:
- 5.2
- See Also:
-
MULTIPART_RELATED
Public constant media type formultipart/related
.- Since:
- 5.2.5
-
MULTIPART_RELATED_VALUE
A String equivalent ofMULTIPART_RELATED
.- Since:
- 5.2.5
- See Also:
-
TEXT_EVENT_STREAM
Public constant media type fortext/event-stream
.- Since:
- 4.3.6
- See Also:
-
TEXT_EVENT_STREAM_VALUE
A String equivalent ofTEXT_EVENT_STREAM
.- Since:
- 4.3.6
- See Also:
-
TEXT_HTML
Public constant media type fortext/html
. -
TEXT_HTML_VALUE
A String equivalent ofTEXT_HTML
.- See Also:
-
TEXT_MARKDOWN
Public constant media type fortext/markdown
.- Since:
- 4.3
-
TEXT_MARKDOWN_VALUE
A String equivalent ofTEXT_MARKDOWN
.- Since:
- 4.3
- See Also:
-
TEXT_PLAIN
Public constant media type fortext/plain
. -
TEXT_PLAIN_VALUE
A String equivalent ofTEXT_PLAIN
.- See Also:
-
TEXT_XML
Public constant media type fortext/xml
. -
TEXT_XML_VALUE
A String equivalent ofTEXT_XML
.- See Also:
-
QUALITY_VALUE_COMPARATOR
Deprecated.As of 6.0, with no direct replacementComparator used bysortByQualityValue(List)
. -
SPECIFICITY_COMPARATOR
Deprecated.As of 6.0, with no direct replacementComparator used bysortBySpecificity(List)
.
-
-
Constructor Details
-
MediaType
Create a newMediaType
for the given primary type.The subtype is set to "*", parameters empty.
- Parameters:
type
- the primary type- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters
-
MediaType
Create a newMediaType
for the given primary type and subtype.The parameters are empty.
- Parameters:
type
- the primary typesubtype
- the subtype- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters
-
MediaType
Create a newMediaType
for the given type, subtype, and character set.- Parameters:
type
- the primary typesubtype
- the subtypecharset
- the character set- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters
-
MediaType
Create a newMediaType
for the given type, subtype, and quality value.- Parameters:
type
- the primary typesubtype
- the subtypequalityValue
- the quality value- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters
-
MediaType
Copy-constructor that copies the type, subtype and parameters of the givenMediaType
, and allows to set the specified character set.- Parameters:
other
- the other media typecharset
- the character set- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters- Since:
- 4.3
-
MediaType
Copy-constructor that copies the type and subtype of the givenMediaType
, and allows for different parameters.- Parameters:
other
- the other media typeparameters
- the parameters, may benull
- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters
-
MediaType
Create a newMediaType
for the given type, subtype, and parameters.- Parameters:
type
- the primary typesubtype
- the subtypeparameters
- the parameters, may benull
- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters
-
MediaType
Create a newMediaType
for the givenMimeType
. The type, subtype and parameters information is copied andMediaType
-specific checks on parameters are performed.- Parameters:
mimeType
- the MIME type- Throws:
IllegalArgumentException
- if any of the parameters contain illegal characters- Since:
- 5.3
-
-
Method Details
-
checkParameters
- Overrides:
checkParameters
in classMimeType
-
getQualityValue
public double getQualityValue()Return the quality factor, as indicated by aq
parameter, if any. Defaults to1.0
.- Returns:
- the quality factor as double value
-
isMoreSpecific
Indicates whether thisMediaType
more specific than the given type.- if this media type has a quality factor higher than the other,
then this method returns
true
. - if this media type has a quality factor lower than the other,
then this method returns
false
. - if this mime type has a wildcard type,
and the other does not, then this method returns
false
. - if this mime type does not have a wildcard type,
and the other does, then this method returns
true
. - if this mime type has a wildcard type,
and the other does not, then this method returns
false
. - if this mime type does not have a wildcard type,
and the other does, then this method returns
true
. - if the two mime types have identical type and subtype, then the mime type with the most parameters is more specific than the other.
- Otherwise, this method returns
false
.
- Overrides:
isMoreSpecific
in classMimeType
- Parameters:
other
- theMimeType
to be compared- Returns:
- the result of the comparison
- Since:
- 6.0
- See Also:
- if this media type has a quality factor higher than the other,
then this method returns
-
isLessSpecific
Indicates whether thisMediaType
more specific than the given type.- if this media type has a quality factor higher than the other,
then this method returns
false
. - if this media type has a quality factor lower than the other,
then this method returns
true
. - if this mime type has a wildcard type,
and the other does not, then this method returns
true
. - if this mime type does not have a wildcard type,
and the other does, then this method returns
false
. - if this mime type has a wildcard type,
and the other does not, then this method returns
true
. - if this mime type does not have a wildcard type,
and the other does, then this method returns
false
. - if the two mime types have identical type and subtype, then the mime type with the least parameters is less specific than the other.
- Otherwise, this method returns
false
.
- Overrides:
isLessSpecific
in classMimeType
- Parameters:
other
- theMimeType
to be compared- Returns:
- the result of the comparison
- Since:
- 6.0
- See Also:
- if this media type has a quality factor higher than the other,
then this method returns
-
includes
Indicate whether thisMediaType
includes the given media type.For instance,
text/*
includestext/plain
andtext/html
, andapplication/*+xml
includesapplication/soap+xml
, etc. This method is not symmetric.Simply calls
MimeType.includes(MimeType)
but declared with aMediaType
parameter for binary backwards compatibility.- Parameters:
other
- the reference media type with which to compare- Returns:
true
if this media type includes the given media type;false
otherwise
-
isCompatibleWith
Indicate whether thisMediaType
is compatible with the given media type.For instance,
text/*
is compatible withtext/plain
,text/html
, and vice versa. In effect, this method is similar toincludes(org.springframework.http.MediaType)
, except that it is symmetric.Simply calls
MimeType.isCompatibleWith(MimeType)
but declared with aMediaType
parameter for binary backwards compatibility.- Parameters:
other
- the reference media type with which to compare- Returns:
true
if this media type is compatible with the given media type;false
otherwise
-
copyQualityValue
Return a replica of this instance with the quality value of the givenMediaType
.- Returns:
- the same instance if the given MediaType doesn't have a quality value, or a new one otherwise
-
removeQualityValue
Return a replica of this instance with its quality value removed.- Returns:
- the same instance if the media type doesn't contain a quality value, or a new one otherwise
-
valueOf
Parse the given String value into aMediaType
object, with this method name following the 'valueOf' naming convention (as supported byConversionService
.- Parameters:
value
- the string to parse- Throws:
InvalidMediaTypeException
- if the media type value cannot be parsed- See Also:
-
parseMediaType
Parse the given String into a singleMediaType
.- Parameters:
mediaType
- the string to parse- Returns:
- the media type
- Throws:
InvalidMediaTypeException
- if the media type value cannot be parsed
-
parseMediaTypes
Parse the comma-separated string into a list ofMediaType
objects.This method can be used to parse an Accept or Content-Type header.
- Parameters:
mediaTypes
- the string to parse- Returns:
- the list of media types
- Throws:
InvalidMediaTypeException
- if the media type value cannot be parsed
-
parseMediaTypes
Parse the given list of (potentially) comma-separated strings into a list ofMediaType
objects.This method can be used to parse an Accept or Content-Type header.
- Parameters:
mediaTypes
- the string to parse- Returns:
- the list of media types
- Throws:
InvalidMediaTypeException
- if the media type value cannot be parsed- Since:
- 4.3.2
-
asMediaTypes
Re-create the given mime types as media types.- Since:
- 5.0
-
asMediaType
Re-create the given mime type as a media type.- Since:
- 5.0
-
toString
Return a string representation of the given list ofMediaType
objects.This method can be used to for an
Accept
orContent-Type
header.- Parameters:
mediaTypes
- the media types to create a string representation for- Returns:
- the string representation
-
sortBySpecificity
Deprecated.As of 6.0, in favor ofMimeTypeUtils.sortBySpecificity(List)
Sorts the given list ofMediaType
objects by specificity.Given two media types:
- if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
- if the two media types have different types, then they are considered equal and remain their current order.
- if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
- if the two media types have different subtypes, then they are considered equal and remain their current order.
- if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
- if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.
For example:
audio/basic < audio/* < */*
audio/* < audio/*;q=0.7; audio/*;q=0.3
audio/basic;level=1 < audio/basic
audio/basic == text/html
audio/basic == audio/wave
- Parameters:
mediaTypes
- the list of media types to be sorted
-
sortByQualityValue
Deprecated.As of 6.0, with no direct replacementSorts the given list ofMediaType
objects by quality value.Given two media types:
- if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
- if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
- if the two media types have different types, then they are considered equal and remain their current order.
- if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
- if the two media types have different subtypes, then they are considered equal and remain their current order.
- if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.
- Parameters:
mediaTypes
- the list of media types to be sorted- See Also:
-
sortBySpecificityAndQuality
Deprecated.As of 6.0, in favor ofMimeTypeUtils.sortBySpecificity(List)
Sorts the given list ofMediaType
objects by specificity as the primary criteria and quality value the secondary.
-
APPLICATION_JSON
since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring acharset=UTF-8
parameter.