javax.ws.rs.core

Class GenericEntity<T>

  • Type Parameters:
    T - response entity instance type

    public class GenericEntity<T>
    extends Object
    Represents a message entity of a generic type T.

    Normally type erasure removes generic type information such that a Response instance that contains, e.g., an entity of type List<String> appears to contain a raw List<?> at runtime. When the generic type is required to select a suitable MessageBodyWriter, this class may be used to wrap the entity and capture its generic type.

    There are two ways to create an instance:

    1. Create a (typically anonymous) subclass of this class which enables retrieval of the type information at runtime despite type erasure. For example, the following code shows how to create a Response containing an entity of type List<String> whose generic type will be available at runtime for selection of a suitable MessageBodyWriter:
      List<String> list = new ArrayList<String>();
       GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {};
       Response response = Response.ok(entity).build();

      where list is the instance of List<String> that will form the response body and entity is an instance of an anonymous subclass of GenericEntity.

    2. Create an instance directly by supplying the generic type information with the entity. For example the following code shows how to create a response containing the result of a method invoked via reflection:
      Method method = ...;
       GenericEntity<Object> entity = new GenericEntity<Object>(
          method.invoke(...), method.getGenericReturnType());
       Response response = Response.ok(entity).build();
    3. The above obtains the generic type from the return type of the method, the raw type is the class of entity.

    Since:
    1.0
    Author:
    Paul Sandoz, Marc Hadley
    See Also:
    GenericType
    • Constructor Detail

      • GenericEntity

        protected GenericEntity(T entity)
        Constructs a new generic entity. Derives represented class from type parameter. Note that this constructor is protected, users should create a (usually anonymous) subclass as shown above.
        Parameters:
        entity - the entity instance, must not be null.
        Throws:
        IllegalArgumentException - if entity is null.
      • GenericEntity

        public GenericEntity(T entity,
                             Type genericType)
        Create a new instance of GenericEntity, supplying the generic type information. The entity must be assignable to a variable of the supplied generic type, e.g. if entity is an instance of ArrayList<String> then genericType could be the same or a superclass of ArrayList with the same generic type like List<String>.
        Parameters:
        entity - the entity instance, must not be null.
        genericType - the generic type, must not be null.
        Throws:
        IllegalArgumentException - if the entity is not assignable to a variable of the supplied generic type or if entity or genericType is null.
    • Method Detail

      • getRawType

        public final Class<?> getRawType()
        Gets the raw type of the enclosed entity. Note that this is the raw type of the instance, not the raw type of the type parameter. I.e. in the example in the introduction, the raw type is ArrayList not List.
        Returns:
        the raw type.
      • getType

        public final Type getType()
        Gets underlying Type instance. Note that this is derived from the type parameter, not the enclosed instance. I.e. in the example in the introduction, the type is List<String> not ArrayList<String>.
        Returns:
        the type
      • getEntity

        public final T getEntity()
        Get the enclosed entity.
        Returns:
        the enclosed entity.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 11/06/2005, zuletzt geändert 18/08/2025
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-javaee-rf-javax/ws/rs/core/GenericEntity.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:fr Manuel PHP : https://docs.oracle.com, GenericEntity (Java(TM) EE 7 Specification APIs)

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor dieser Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.