Class MediaType

java.lang.Object
org.springframework.util.MimeType
org.springframework.http.MediaType
All Implemented Interfaces:
Serializable, Comparable<MimeType>

public class MediaType extends MimeType implements Serializable
A subclass of 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, Hyoungjune Kim
See Also:
  • Field Details

  • Constructor Details

    • MediaType

      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
    • MediaType

      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
    • MediaType

      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
    • MediaType

      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
    • MediaType

      public MediaType(MediaType other, Charset charset)
      Copy-constructor that copies the type, subtype and parameters of the given MediaType, and allows to set the specified character set.
      Parameters:
      other - the other media type
      charset - the character set
      Throws:
      IllegalArgumentException - if any of the parameters contain illegal characters
      Since:
      4.3
    • MediaType

      public MediaType(MediaType other, @Nullable Map<String,String> parameters)
      Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameters.
      Parameters:
      other - the other media type
      parameters - the parameters, may be null
      Throws:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(String type, String subtype, @Nullable Map<String,String> 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
    • MediaType

      public MediaType(MimeType mimeType)
      Create a new MediaType for the given MimeType. The type, subtype and parameters information is copied and MediaType-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

      protected void checkParameters(String parameter, String value)
      Overrides:
      checkParameters in class MimeType
    • getQualityValue

      public double getQualityValue()
      Return the quality factor, as indicated by a q parameter, if any. Defaults to 1.0.
      Returns:
      the quality factor as double value
    • isMoreSpecific

      public boolean isMoreSpecific(MimeType other)
      Indicates whether this MediaType more specific than the given type.
      1. if this media type has a quality factor higher than the other, then this method returns true.
      2. if this media type has a quality factor lower than the other, then this method returns false.
      3. if this mime type has a wildcard type, and the other does not, then this method returns false.
      4. if this mime type does not have a wildcard type, and the other does, then this method returns true.
      5. if this mime type has a wildcard type, and the other does not, then this method returns false.
      6. if this mime type does not have a wildcard type, and the other does, then this method returns true.
      7. if the two mime types have identical type and subtype, then the mime type with the most parameters is more specific than the other.
      8. Otherwise, this method returns false.
      Overrides:
      isMoreSpecific in class MimeType
      Parameters:
      other - the MimeType to be compared
      Returns:
      the result of the comparison
      Since:
      6.0
      See Also:
    • isLessSpecific

      public boolean isLessSpecific(MimeType other)
      Indicates whether this MediaType more specific than the given type.
      1. if this media type has a quality factor higher than the other, then this method returns false.
      2. if this media type has a quality factor lower than the other, then this method returns true.
      3. if this mime type has a wildcard type, and the other does not, then this method returns true.
      4. if this mime type does not have a wildcard type, and the other does, then this method returns false.
      5. if this mime type has a wildcard type, and the other does not, then this method returns true.
      6. if this mime type does not have a wildcard type, and the other does, then this method returns false.
      7. if the two mime types have identical type and subtype, then the mime type with the least parameters is less specific than the other.
      8. Otherwise, this method returns false.
      Overrides:
      isLessSpecific in class MimeType
      Parameters:
      other - the MimeType to be compared
      Returns:
      the result of the comparison
      Since:
      6.0
      See Also:
    • includes

      public boolean includes(@Nullable MediaType other)
      Indicate whether this MediaType includes the given media type.

      For instance, text/* includes text/plain and text/html, and application/*+xml includes application/soap+xml, etc. This method is not symmetric.

      Simply calls MimeType.includes(MimeType) but declared with a MediaType 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

      public boolean isCompatibleWith(@Nullable 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(org.springframework.http.MediaType), except that it is symmetric.

      Simply calls MimeType.isCompatibleWith(MimeType) but declared with a MediaType 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

      public MediaType copyQualityValue(MediaType mediaType)
      Return a replica of this instance with the quality value of the given MediaType.
      Returns:
      the same instance if the given MediaType doesn't have a quality value, or a new one otherwise
    • removeQualityValue

      public MediaType 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

      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).
      Parameters:
      value - the string to parse
      Throws:
      InvalidMediaTypeException - if the media type value cannot be parsed
      See Also:
    • parseMediaType

      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:
      InvalidMediaTypeException - if the media type value cannot be parsed
    • parseMediaTypes

      public static List<MediaType> parseMediaTypes(@Nullable String mediaTypes)
      Parse the comma-separated 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:
      InvalidMediaTypeException - if the media type value cannot be parsed
    • parseMediaTypes

      public static List<MediaType> parseMediaTypes(@Nullable List<String> mediaTypes)
      Parse the given list of (potentially) comma-separated strings 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:
      InvalidMediaTypeException - if the media type value cannot be parsed
      Since:
      4.3.2
    • asMediaTypes

      public static List<MediaType> asMediaTypes(List<MimeType> mimeTypes)
      Re-create the given mime types as media types.
      Since:
      5.0
    • asMediaType

      public static MediaType asMediaType(MimeType mimeType)
      Re-create the given mime type as a media type.
      Since:
      5.0
    • toString

      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 media types to create a string representation for
      Returns:
      the string representation
    • sortBySpecificity

      @Deprecated(since="6.0", forRemoval=true) public static void sortBySpecificity(List<MediaType> mediaTypes)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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
    • sortByQualityValue

      @Deprecated(since="6.0", forRemoval=true) public static void sortByQualityValue(List<MediaType> mediaTypes)
      Deprecated, for removal: This API element is subject to removal in a future version.
      As of 6.0, with no direct replacement
      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
      See Also:
    • sortBySpecificityAndQuality

      @Deprecated(since="6.0") public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes)
      Deprecated.
      Sorts the given list of MediaType objects by specificity as the primary criteria and quality value the secondary.