Class MediaType
- All Implemented Interfaces:
Serializable, Comparable<MimeType>
MimeType that adds support for quality parameters
as defined in the HTTP specification.
This class is meant to reference media types supported by Spring Framework.
If your application or library relies on other media types defined in RFCs,
please use parseMediaType(String) or a custom utility class.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev, Sebastien Deleuze, Kazuki Shimizu, Sam Brannen, Hyoungjune Kim
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MediaTypeMedia type for "*/*", including all media ranges.static final StringA String equivalent ofALL.static final MediaTypeMedia type forapplication/atom+xml.static final StringA String equivalent ofAPPLICATION_ATOM_XML.static final MediaTypeMedia type forapplication/cbor.static final StringA String equivalent ofAPPLICATION_CBOR.static final MediaTypeMedia type forapplication/x-www-form-urlencoded.static final StringA String equivalent ofAPPLICATION_FORM_URLENCODED.static final MediaTypeMedia type forapplication/graphql-response+json.static final StringA String equivalent ofAPPLICATION_GRAPHQL_RESPONSE.static final MediaTypeMedia type forapplication/json.static final StringA String equivalent ofAPPLICATION_JSON.static final MediaTypeMedia type forapplication/x-ndjson.static final StringA String equivalent ofAPPLICATION_NDJSON.static final MediaTypeMedia type forapplication/octet-stream.static final StringA String equivalent ofAPPLICATION_OCTET_STREAM.static final MediaTypeMedia type forapplication/pdf.static final StringA String equivalent ofAPPLICATION_PDF.static final MediaTypeMedia type forapplication/problem+json.static final StringA String equivalent ofAPPLICATION_PROBLEM_JSON.static final MediaTypeMedia type forapplication/problem+xml.static final StringA String equivalent ofAPPLICATION_PROBLEM_XML.static final MediaTypeMedia type forapplication/x-protobuf.static final StringA String equivalent ofAPPLICATION_PROTOBUF.static final MediaTypeMedia type forapplication/rss+xml.static final StringA String equivalent ofAPPLICATION_RSS_XML.static final MediaTypeMedia type forapplication/xhtml+xml.static final StringA String equivalent ofAPPLICATION_XHTML_XML.static final MediaTypeMedia type forapplication/xml.static final StringA String equivalent ofAPPLICATION_XML.static final MediaTypeMedia type forapplication/yaml.static final StringA String equivalent ofAPPLICATION_YAML.static final MediaTypeMedia type forimage/gif.static final StringA String equivalent ofIMAGE_GIF.static final MediaTypeMedia type forimage/jpeg.static final StringA String equivalent ofIMAGE_JPEG.static final MediaTypeMedia type forimage/png.static final StringA String equivalent ofIMAGE_PNG.static final MediaTypeMedia type formultipart/form-data.static final StringA String equivalent ofMULTIPART_FORM_DATA.static final MediaTypeMedia type formultipart/mixed.static final StringA String equivalent ofMULTIPART_MIXED.static final MediaTypeMedia type formultipart/related.static final StringA String equivalent ofMULTIPART_RELATED.static final MediaTypeMedia type fortext/event-stream.static final StringA String equivalent oftext_EVENT_STREAM.static final MediaTypeMedia type fortext/html.static final StringA String equivalent oftext_HTML.static final MediaTypeMedia type fortext/markdown.static final StringA String equivalent oftext_MARKDOWN.static final MediaTypeMedia type fortext/plain.static final StringA String equivalent oftext_PLAIN.static final MediaTypeMedia type fortext/xml.static final StringA String equivalent oftext_XML.Fields inherited from class MimeType
WILDCARD_TYPE -
Constructor Summary
ConstructorsConstructorDescriptionCreate a newMediaTypefor the given primary type.Create a newMediaTypefor the given primary type and subtype.Create a newMediaTypefor the given type, subtype, and quality value.Create a newMediaTypefor the given type, subtype, and character set.Create a newMediaTypefor 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 newMediaTypefor the givenMimeType. -
Method Summary
Modifier and TypeMethodDescriptionstatic MediaTypeasMediaType(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 voidcheckParameters(String parameter, String value) copyQualityValue(MediaType mediaType) Return a replica of this instance with the quality value of the givenMediaType.doubleReturn the quality factor, as indicated by aqparameter, if any.booleanIndicate whether thisMediaTypeincludes the given media type.booleanisCompatibleWith(@Nullable MediaType other) Indicate whether thisMediaTypeis compatible with the given media type.booleanisLessSpecific(MimeType other) Indicates whether thisMediaTypemore specific than the given type.booleanisMoreSpecific(MimeType other) Indicates whether thisMediaTypemore specific than the given type.static MediaTypeparseMediaType(String mediaType) Parse the given String into a singleMediaType.parseMediaTypes(@Nullable String mediaTypes) Parse the comma-separated string into a list ofMediaTypeobjects.parseMediaTypes(@Nullable List<String> mediaTypes) Parse the given list of (potentially) comma-separated strings into a list ofMediaTypeobjects.Return a replica of this instance with its quality value removed.static StringtoString(Collection<MediaType> mediaTypes) Return a string representation of the given list ofMediaTypeobjects.static MediaTypeParse the given String value into aMediaTypeobject, with this method name following the 'valueOf' naming convention (as supported byConversionService).Methods inherited from class MimeType
appendTo, compareTo, equals, equalsTypeAndSubtype, getCharset, getParameter, getParameters, getSubtype, getSubtypeSuffix, getType, hashCode, includes, isCompatibleWith, isConcrete, isPresentIn, isWildcardSubtype, isWildcardType, toString, unquote
-
Field Details
-
ALL
Media type for "*/*", including all media ranges. -
ALL_VALUE
-
APPLICATION_ATOM_XML
Media type forapplication/atom+xml. -
APPLICATION_ATOM_XML_VALUE
A String equivalent ofAPPLICATION_ATOM_XML.- See Also:
-
APPLICATION_CBOR
-
APPLICATION_CBOR_VALUE
A String equivalent ofAPPLICATION_CBOR.- Since:
- 5.2
- See Also:
-
APPLICATION_FORM_URLENCODED
Media type forapplication/x-www-form-urlencoded. -
APPLICATION_FORM_URLENCODED_VALUE
A String equivalent ofAPPLICATION_FORM_URLENCODED.- See Also:
-
APPLICATION_GRAPHQL_RESPONSE
Media type forapplication/graphql-response+json.- Since:
- 6.0.3
- See Also:
-
APPLICATION_GRAPHQL_RESPONSE_VALUE
A String equivalent ofAPPLICATION_GRAPHQL_RESPONSE.- Since:
- 6.0.3
- See Also:
-
APPLICATION_JSON
Media type forapplication/json. -
APPLICATION_JSON_VALUE
A String equivalent ofAPPLICATION_JSON.- See Also:
-
APPLICATION_OCTET_STREAM
Media type forapplication/octet-stream. -
APPLICATION_OCTET_STREAM_VALUE
A String equivalent ofAPPLICATION_OCTET_STREAM.- See Also:
-
APPLICATION_PDF
-
APPLICATION_PDF_VALUE
A String equivalent ofAPPLICATION_PDF.- Since:
- 4.3
- See Also:
-
APPLICATION_PROBLEM_JSON
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_XML
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
Media type forapplication/x-protobuf.- Since:
- 6.0
-
APPLICATION_PROTOBUF_VALUE
A String equivalent ofAPPLICATION_PROTOBUF.- Since:
- 6.0
- See Also:
-
APPLICATION_RSS_XML
-
APPLICATION_RSS_XML_VALUE
A String equivalent ofAPPLICATION_RSS_XML.- Since:
- 4.3.6
- See Also:
-
APPLICATION_NDJSON
-
APPLICATION_NDJSON_VALUE
A String equivalent ofAPPLICATION_NDJSON.- Since:
- 5.3
- See Also:
-
APPLICATION_XHTML_XML
Media type forapplication/xhtml+xml. -
APPLICATION_XHTML_XML_VALUE
A String equivalent ofAPPLICATION_XHTML_XML.- See Also:
-
APPLICATION_XML
Media type forapplication/xml. -
APPLICATION_XML_VALUE
-
APPLICATION_YAML
-
APPLICATION_YAML_VALUE
A String equivalent ofAPPLICATION_YAML.- Since:
- 6.2
- See Also:
-
IMAGE_GIF
Media type forimage/gif. -
IMAGE_GIF_VALUE
-
IMAGE_JPEG
Media type forimage/jpeg. -
IMAGE_JPEG_VALUE
-
IMAGE_PNG
Media type forimage/png. -
IMAGE_PNG_VALUE
-
MULTIPART_FORM_DATA
Media type formultipart/form-data. -
MULTIPART_FORM_DATA_VALUE
A String equivalent ofMULTIPART_FORM_DATA.- See Also:
-
MULTIPART_MIXED
-
MULTIPART_MIXED_VALUE
A String equivalent ofMULTIPART_MIXED.- Since:
- 5.2
- See Also:
-
MULTIPART_RELATED
-
MULTIPART_RELATED_VALUE
A String equivalent ofMULTIPART_RELATED.- Since:
- 5.2.5
- See Also:
-
text_EVENT_STREAM
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
Media type fortext/html. -
text_HTML_VALUE
-
text_MARKDOWN
-
text_MARKDOWN_VALUE
A String equivalent oftext_MARKDOWN.- Since:
- 4.3
- See Also:
-
text_PLAIN
Media type fortext/plain. -
text_PLAIN_VALUE
-
text_XML
Media type fortext/xml. -
text_XML_VALUE
-
-
Constructor Details
-
MediaType
Create a newMediaTypefor 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 newMediaTypefor 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 newMediaTypefor 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 newMediaTypefor 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 newMediaTypefor 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 newMediaTypefor 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:
checkParametersin classMimeType
-
getQualityValue
public double getQualityValue()Return the quality factor, as indicated by aqparameter, if any. Defaults to1.0.- Returns:
- the quality factor as double value
-
isMoreSpecific
Indicates whether thisMediaTypemore 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:
isMoreSpecificin classMimeType- Parameters:
other- theMimeTypeto 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 thisMediaTypemore 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:
isLessSpecificin classMimeType- Parameters:
other- theMimeTypeto 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 thisMediaTypeincludes the given media type.For instance,
text/*includestext/plainandtext/html, andapplication/*+xmlincludesapplication/soap+xml, etc. This method is not symmetric.Simply calls
MimeType.includes(MimeType)but declared with aMediaTypeparameter for binary backwards compatibility.- Parameters:
other- the reference media type with which to compare- Returns:
trueif this media type includes the given media type;falseotherwise
-
isCompatibleWith
Indicate whether thisMediaTypeis 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(MediaType), except that it is symmetric.Simply calls
MimeType.isCompatibleWith(MimeType)but declared with aMediaTypeparameter for binary backwards compatibility.- Parameters:
other- the reference media type with which to compare- Returns:
trueif this media type is compatible with the given media type;falseotherwise
-
copyQualityValue
-
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 aMediaTypeobject, 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 ofMediaTypeobjects.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 ofMediaTypeobjects.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
-
asMediaType
-
toString
Return a string representation of the given list ofMediaTypeobjects.This method can be used to for an
AcceptorContent-Typeheader.- Parameters:
mediaTypes- the media types to create a string representation for- Returns:
- the string representation
-