public class

MediaType

extends Object
implements Comparable<T>
java.lang.Object
   ↳ org.springframework.http.MediaType

Class Overview

Represents an Internet Media Type, as defined in the HTTP specification.

Consists of a type and a subtype. Also has functionality to parse media types from a string using parseMediaType(String), or multiple comma-separated media types using parseMediaTypes(String).

Summary

Fields
public static final MediaType ALL Public constant media type that includes all media ranges (i.e.
public static final MediaType APPLICATION_ATOM_XML Public constant media type for application/atom+xml.
public static final MediaType APPLICATION_FORM_URLENCODED Public constant media type for application/x-www-form-urlencoded.
public static final MediaType APPLICATION_JSON Public constant media type for application/json.
public static final MediaType APPLICATION_OCTET_STREAM Public constant media type for application/octet-stream.
public static final MediaType APPLICATION_XHTML_XML Public constant media type for application/xhtml+xml.
public static final MediaType APPLICATION_XML Public constant media type for application/xml.
public static final MediaType IMAGE_GIF Public constant media type for image/gif.
public static final MediaType IMAGE_JPEG Public constant media type for image/jpeg.
public static final MediaType IMAGE_PNG Public constant media type for image/png.
public static final MediaType MULTIPART_FORM_DATA Public constant media type for multipart/form-data.
public static final MediaType TEXT_HTML Public constant media type for text/html.
public static final MediaType TEXT_PLAIN Public constant media type for text/plain.
public static final MediaType TEXT_XML Public constant media type for text/xml.
Public Constructors
MediaType(String type)
Create a new MediaType for the given primary type.
MediaType(String type, String subtype)
Create a new MediaType for the given primary type and subtype.
MediaType(String type, String subtype, Charset charSet)
Create a new MediaType for the given type, subtype, and character set.
MediaType(String type, String subtype, double qualityValue)
Create a new MediaType for the given type, subtype, and quality value.
MediaType(MediaType other, Map<StringString> parameters)
Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameter.
MediaType(String type, String subtype, Map<StringString> parameters)
Create a new MediaType for the given type, subtype, and parameters.
Public Methods
int compareTo(MediaType other)
Compares this MediaType to another alphabetically.
boolean equals(Object other)
Charset getCharSet()
Return the character set, as indicated by a charset parameter, if any.
String getParameter(String name)
Return a generic parameter value, given a parameter name.
double getQualityValue()
Return the quality value, as indicated by a q parameter, if any.
String getSubtype()
Return the subtype.
String getType()
Return the primary type.
int hashCode()
boolean includes(MediaType other)
Indicate whether this MediaType includes the given media type.
boolean isCompatibleWith(MediaType other)
Indicate whether this MediaType is compatible with the given media type.
boolean isWildcardSubtype()
Indicate whether the subtype is the wildcard character * or not.
boolean isWildcardType()
Indicate whether the type is the wildcard character * or not.
static MediaType parseMediaType(String mediaType)
Parse the given String into a single MediaType.
static List<MediaType> parseMediaTypes(String mediaTypes)
Parse the given, comma-seperated string into a list of MediaType objects.
static void sortByQualityValue(List<MediaType> mediaTypes)
Sorts the given list of MediaType objects by quality value.
static void sortBySpecificity(List<MediaType> mediaTypes)
Sorts the given list of MediaType objects by specificity.
static String toString(Collection<MediaType> mediaTypes)
Return a string representation of the given list of MediaType objects.
String toString()
static MediaType valueOf(String value)
Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention (as supported by ConversionService.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Comparable

Fields

public static final MediaType ALL

Public constant media type that includes all media ranges (i.e. */*).

public static final MediaType APPLICATION_ATOM_XML

Public constant media type for application/atom+xml.

public static final MediaType APPLICATION_FORM_URLENCODED

Public constant media type for application/x-www-form-urlencoded.

public static final MediaType APPLICATION_JSON

Public constant media type for application/json.

public static final MediaType APPLICATION_OCTET_STREAM

Public constant media type for application/octet-stream.

public static final MediaType APPLICATION_XHTML_XML

Public constant media type for application/xhtml+xml.

public static final MediaType APPLICATION_XML

Public constant media type for application/xml.

public static final MediaType IMAGE_GIF

Public constant media type for image/gif.

public static final MediaType IMAGE_JPEG

Public constant media type for image/jpeg.

public static final MediaType IMAGE_PNG

Public constant media type for image/png.

public static final MediaType MULTIPART_FORM_DATA

Public constant media type for multipart/form-data.

public static final MediaType TEXT_HTML

Public constant media type for text/html.

public static final MediaType TEXT_PLAIN

Public constant media type for text/plain.

public static final MediaType TEXT_XML

Public constant media type for text/xml.

Public Constructors

public MediaType (String type)

Create a new MediaType 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

public MediaType (String type, String subtype)

Create a new MediaType for the given primary type and subtype.

The parameters are empty.

Parameters
type the primary type
subtype the subtype
Throws
IllegalArgumentException if any of the parameters contain illegal characters

public MediaType (String type, String subtype, Charset charSet)

Create a new MediaType for the given type, subtype, and character set.

Parameters
type the primary type
subtype the subtype
charSet the character set
Throws
IllegalArgumentException if any of the parameters contain illegal characters

public MediaType (String type, String subtype, double qualityValue)

Create a new MediaType for the given type, subtype, and quality value.

Parameters
type the primary type
subtype the subtype
qualityValue the quality value
Throws
IllegalArgumentException if any of the parameters contain illegal characters

public MediaType (MediaType other, Map<StringString> parameters)

Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameter.

Parameters
other the other media type
parameters the parameters, may be null
Throws
IllegalArgumentException if any of the parameters contain illegal characters

public MediaType (String type, String subtype, Map<StringString> parameters)

Create a new MediaType for the given type, subtype, and parameters.

Parameters
type the primary type
subtype the subtype
parameters the parameters, may be null
Throws
IllegalArgumentException if any of the parameters contain illegal characters

Public Methods

public int compareTo (MediaType other)

Compares this MediaType to another alphabetically.

Parameters
other media type to compare to

public boolean equals (Object other)

public Charset getCharSet ()

Return the character set, as indicated by a charset parameter, if any.

Returns
  • the character set; or null if not available

public String getParameter (String name)

Return a generic parameter value, given a parameter name.

Parameters
name the parameter name
Returns
  • the parameter value; or null if not present

public double getQualityValue ()

Return the quality value, as indicated by a q parameter, if any. Defaults to 1.0.

Returns
  • the quality factory

public String getSubtype ()

Return the subtype.

public String getType ()

Return the primary type.

public int hashCode ()

public boolean includes (MediaType other)

Indicate whether this MediaType includes the given media type.

For instance, text/* includes text/plain, text/html, and application/*+xml includes application/soap+xml, etc. This method is non-symmetic.

Parameters
other the reference media type with which to compare
Returns
  • true if this media type includes the given media type; false otherwise

public boolean isCompatibleWith (MediaType other)

Indicate whether this MediaType is compatible with the given media type.

For instance, text/* is compatible with text/plain, text/html, and vice versa. In effect, this method is similar to includes(MediaType), except that it's symmetric.

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

public boolean isWildcardSubtype ()

Indicate whether the subtype is the wildcard character * or not.

Returns
  • whether the subtype is *

public boolean isWildcardType ()

Indicate whether the type is the wildcard character * or not.

public static MediaType parseMediaType (String mediaType)

Parse the given String into a single MediaType.

Parameters
mediaType the string to parse
Returns
  • the media type
Throws
IllegalArgumentException if the string cannot be parsed

public static List<MediaType> parseMediaTypes (String mediaTypes)

Parse the given, comma-seperated string into a list of MediaType 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
IllegalArgumentException if the string cannot be parsed

public static void sortByQualityValue (List<MediaType> mediaTypes)

Sorts the given list of MediaType objects by quality value.

Given two media types:

  1. if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
  2. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
  3. if the two media types have different types, then they are considered equal and remain their current order.
  4. if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
  5. if the two media types have different subtypes, then they are considered equal and remain their current order.
  6. 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

public static void sortBySpecificity (List<MediaType> mediaTypes)

Sorts the given list of MediaType objects by specificity.

Given two media types:

  1. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
  2. if the two media types have different types, then they are considered equal and remain their current order.
  3. if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
  4. if the two media types have different subtypes, then they are considered equal and remain their current order.
  5. if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
  6. 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

public static String toString (Collection<MediaType> mediaTypes)

Return a string representation of the given list of MediaType objects.

This method can be used to for an Accept or Content-Type header.

Parameters
mediaTypes the string to parse
Returns
  • the list of media types
Throws
IllegalArgumentException if the String cannot be parsed

public String toString ()

public static MediaType valueOf (String value)

Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention (as supported by ConversionService.