javax.ws.rs.core

Class MediaType


  • public class MediaType
    extends Object
    An abstraction for a media type. Instances are immutable.
    Since:
    1.0
    Author:
    Paul Sandoz, Marc Hadley
    See Also:
    HTTP/1.1 section 3.7
    • Constructor Detail

      • MediaType

        public MediaType(String type,
                         String subtype,
                         Map<String,String> parameters)
        Creates a new instance of MediaType with the supplied type, subtype and parameters.
        Parameters:
        type - the primary type, null is equivalent to MEDIA_TYPE_WILDCARD.
        subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD.
        parameters - a map of media type parameters, null is the same as an empty map.
      • MediaType

        public MediaType(String type,
                         String subtype)
        Creates a new instance of MediaType with the supplied type and subtype.
        Parameters:
        type - the primary type, null is equivalent to MEDIA_TYPE_WILDCARD
        subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD
      • MediaType

        public MediaType(String type,
                         String subtype,
                         String charset)
        Creates a new instance of MediaType with the supplied type, subtype and ""charset"" parameter.
        Parameters:
        type - the primary type, null is equivalent to MEDIA_TYPE_WILDCARD
        subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD
        charset - the ""charset"" parameter value. If null or empty the ""charset"" parameter will not be set.
      • MediaType

        public MediaType()
        Creates a new instance of MediaType, both type and subtype are wildcards. Consider using the constant WILDCARD_TYPE instead.
    • Method Detail

      • valueOf

        public static MediaType valueOf(String type)
        Creates a new instance of MediaType by parsing the supplied string.
        Parameters:
        type - the media type string.
        Returns:
        the newly created MediaType.
        Throws:
        IllegalArgumentException - if the supplied string cannot be parsed or is null.
      • getType

        public String getType()
        Getter for primary type.
        Returns:
        value of primary type.
      • isWildcardType

        public boolean isWildcardType()
        Checks if the primary type is a wildcard.
        Returns:
        true if the primary type is a wildcard.
      • getSubtype

        public String getSubtype()
        Getter for subtype.
        Returns:
        value of subtype.
      • isWildcardSubtype

        public boolean isWildcardSubtype()
        Checks if the subtype is a wildcard.
        Returns:
        true if the subtype is a wildcard.
      • getParameters

        public Map<String,String> getParameters()
        Getter for a read-only parameter map. Keys are case-insensitive.
        Returns:
        an immutable map of parameters.
      • withCharset

        public MediaType withCharset(String charset)
        Create a new MediaType instance with the same type, subtype and parameters copied from the original instance and the supplied ""charset"" parameter.
        Parameters:
        charset - the ""charset"" parameter value. If null or empty the ""charset"" parameter will not be set or updated.
        Returns:
        copy of the current MediaType instance with the ""charset"" parameter set to the supplied value.
        Since:
        2.0
      • isCompatible

        public boolean isCompatible(MediaType other)
        Check if this media type is compatible with another media type. E.g. image/* is compatible with image/jpeg, image/png, etc. Media type parameters are ignored. The function is commutative.
        Parameters:
        other - the media type to compare with.
        Returns:
        true if the types are compatible, false otherwise.
      • equals

        public boolean equals(Object obj)
        Compares obj to this media type to see if they are the same by comparing type, subtype and parameters. Note that the case-sensitivity of parameter values is dependent on the semantics of the parameter name, see HTTP/1.1. This method assumes that values are case-sensitive.

        Note that the equals(...) implementation does not perform a class equality check (this.getClass() == obj.getClass()). Therefore any class that extends from MediaType class and needs to override one of the equals(...) and hashCode() methods must always override both methods to ensure the contract between Object.equals(java.lang.Object) and Object.hashCode() does not break.

        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to.
        Returns:
        true if the two media types are the same, false otherwise.
      • hashCode

        public int hashCode()
        Generate a hash code from the type, subtype and parameters.

        Note that the equals(java.lang.Object) implementation does not perform a class equality check (this.getClass() == obj.getClass()). Therefore any class that extends from MediaType class and needs to override one of the equals(Object) and hashCode() methods must always override both methods to ensure the contract between Object.equals(java.lang.Object) and Object.hashCode() does not break.

        Overrides:
        hashCode in class Object
        Returns:
        a generated hash code.
      • toString

        public String toString()
        Convert the media type to a string suitable for use as the value of a corresponding HTTP header.
        Overrides:
        toString in class Object
        Returns:
        a string version of the media type.

Traduction non disponible

Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Version en cache

21/08/2025 13:04:22 Cette version de la page est en cache (à la date du 21/08/2025 13:04:22) afin d'accélérer le traitement.
Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la version plus récente de la page.

Document créé le 11/06/2005, dernière modification le 18/08/2025
Source du document imprimé : https://www.gaudry.be/java-api-javaee-rf-javax/ws/rs/core/MediaType.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :fr Manuel PHP : https://docs.oracle.com, MediaType (Java(TM) EE 7 Specification APIs)

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.