Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Questo contenuto non è disponibile nella lingua selezionata.
47.4. Returning entities with generic type information
Overview Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
There are occasions where the application needs more control over the MIME type of the returned object or the entity provider used to serialize the response. The JAX-RS
javax.ws.rs.core.GenericEntity<T>
class provides finer control over the serializing of entities by providing a mechanism for specifying the generic type of the object representing the entity.
Using a GenericEntity object Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
One of the criteria used for selecting the entity provider that serializes a response is the generic type of the object. The generic type of an object represents the Java type of the object. When a common Java type or a JAXB object is returned, the runtime can use Java reflection to determine the generic type. However, when a JAX-RS
Response
object is returned, the runtime cannot determine the generic type of the wrapped entity and the actual Java class of the object is used as the Java type.
To ensure that the entity provider is provided with correct generic type information, the entity can be wrapped in a
GenericEntity<T>
object before being added to the Response
object being returned.
Resource methods can also directly return a
GenericEntity<T>
object. In practice, this approach is rarely used. The generic type information determined by reflection of an unwrapped entity and the generic type information stored for an entity wrapped in a GenericEntity<T>
object are typically the same.
Creating a GenericEntity object Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
There are two ways to create a
GenericEntity<T>
object:
- Create a subclass of the
GenericEntity<T>
class using the entity being wrapped. Example 47.11, “Creating a GenericEntity<T> object using a subclass” shows how to create aGenericEntity<T>
object containing an entity of typeList<String>
whose generic type will be available at runtime.Example 47.11. Creating a GenericEntity<T> object using a subclass
Copy to Clipboard Copied! Toggle word wrap Toggle overflow TipThe subclass used to create aGenericEntity<T>
object is typically anonymous. - Create an instance directly by supplying the generic type information with the entity. Example 47.12, “Directly instantiating a GenericEntity<T> object” shows how to create a response containing an entity of type
AtomicInteger
.Example 47.12. Directly instantiating a GenericEntity<T> object
Copy to Clipboard Copied! Toggle word wrap Toggle overflow