- java.lang.Object
-
- javax.ws.rs.core.GenericEntity<T>
-
- Type Parameters:
T
- response entity instance type
public class GenericEntity<T> extends Object
Represents a message entity of a generic typeT
.Normally type erasure removes generic type information such that a
Response
instance that contains, e.g., an entity of typeList<String>
appears to contain a rawList<?>
at runtime. When the generic type is required to select a suitableMessageBodyWriter
, this class may be used to wrap the entity and capture its generic type.There are two ways to create an instance:
- 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 typeList<String>
whose generic type will be available at runtime for selection of a suitableMessageBodyWriter
: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 ofList<String>
that will form the response body and entity is an instance of an anonymous subclass ofGenericEntity
. - 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();
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 Summary
Constructors Modifier Constructor and Description protected
GenericEntity(T entity)
Constructs a new generic entity.GenericEntity(T entity, Type genericType)
Create a new instance of GenericEntity, supplying the generic type information.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description boolean
equals(Object obj)
T
getEntity()
Get the enclosed entity.Class<?>
getRawType()
Gets the raw type of the enclosed entity.Type
getType()
Gets underlyingType
instance.int
hashCode()
String
toString()
-
-
-
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 benull
.- Throws:
IllegalArgumentException
- if entity isnull
.
-
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. ifentity
is an instance ofArrayList<String>
thengenericType
could be the same or a superclass ofArrayList
with the same generic type likeList<String>
.- Parameters:
entity
- the entity instance, must not benull
.genericType
- the generic type, must not benull
.- 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 isArrayList
notList
.- Returns:
- the raw type.
-
getType
public final Type getType()
Gets underlyingType
instance. Note that this is derived from the type parameter, not the enclosed instance. I.e. in the example in the introduction, the type isList<String>
notArrayList<String>
.- Returns:
- the type
-
getEntity
public final T getEntity()
Get the enclosed entity.- Returns:
- the enclosed entity.
-
-
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 20:15:27 Cette version de la page est en cache (à la date du 21/08/2025 20:15:27) 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/genericentity.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
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.