Represents a message entity of a generic type {@code T}.
Normally type erasure removes generic type information such that a {@link Response} instance that contains, e.g., an entity of type{@code List} appears to contain a raw {@code List>} at runtime.When the generic type is required to select a suitable {@link javax.ws.rs.ext.MessageBodyWriter}, 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 {@link Response} containing an entity of type {@code List} whosegeneric type will be available at runtime for selection of a suitable {@link javax.ws.rs.ext.MessageBodyWriter}:
List<String> list = new ArrayList<String>(); GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {}; Response response = Response.ok(entity).build();
where {@code list} is the instance of {@code List}that will form the response body and entity is an instance of an anonymous subclass of {@code GenericEntity}.
- 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.
@param < T> response entity instance type
@author Paul Sandoz
@author Marc Hadley
@see GenericType
@since 1.0