Class MediaType


  • public final class MediaType
    extends java.lang.Object
    Implements a simple media type parser based on the Content-Type grammar defined in RFC 7231.

    The type, subtype, and parameter names are all canonicalized to lowercase strings.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)  
      static MediaType from​(java.lang.String value)
      Create a parsed MediaType from the given string.
      java.util.Map<java.lang.String,​java.lang.String> getParameters()
      Gets the immutable map of parameters.
      java.lang.String getSubtype()
      Gets the "subtype" of the media type.
      java.lang.String getSubtypeWithoutSuffix()
      Gets the "subtype" of the media type with no structured syntax suffix.
      java.util.Optional<java.lang.String> getSuffix()
      Gets the optional structured syntax suffix.
      java.lang.String getType()
      Gets the "type" of the media type.
      int hashCode()  
      static boolean isJson​(java.lang.String mediaType)
      Detects if the given media type string is JSON, meaning it is "application/json" or uses the "+json" structured syntax suffix.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • from

        public static MediaType from​(java.lang.String value)
        Create a parsed MediaType from the given string.
        Parameters:
        value - Media type to parse (e.g., application/json).
        Returns:
        Returns the parsed media type.
        Throws:
        java.lang.RuntimeException - if the media type is invalid.
      • isJson

        public static boolean isJson​(java.lang.String mediaType)
        Detects if the given media type string is JSON, meaning it is "application/json" or uses the "+json" structured syntax suffix.
        Parameters:
        mediaType - Media type to parse and test if it's JSON.
        Returns:
        Returns true if the given media type is JSON.
      • getType

        public java.lang.String getType()
        Gets the "type" of the media type.
        Returns:
        Returns the type (e.g., "application").
      • getSubtype

        public java.lang.String getSubtype()
        Gets the "subtype" of the media type.
        Returns:
        Returns the subtype (e.g., "json", "foo+json").
      • getSubtypeWithoutSuffix

        public java.lang.String getSubtypeWithoutSuffix()
        Gets the "subtype" of the media type with no structured syntax suffix.

        For example given, "application/foo+json", this method returns "foo". Given "application/foo+baz+json", this method returns "foo+baz".

        Returns:
        Returns the subtype (e.g., "json", "foo+json").
      • getParameters

        public java.util.Map<java.lang.String,​java.lang.String> getParameters()
        Gets the immutable map of parameters.
        Returns:
        Returns the parameters.
      • getSuffix

        public java.util.Optional<java.lang.String> getSuffix()
        Gets the optional structured syntax suffix.

        For example given, "application/foo+json", this method returns "json". Given "application/foo+baz+json", this method returns "json". Given "application/json", this method returns an empty Optional.

        Returns:
        Returns the optional structured syntax suffix value with no "+".
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object