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.Este conteúdo não está disponível no idioma selecionado.
Chapter 48. Entity Support
Abstract
Overview Copiar o linkLink copiado para a área de transferência!
MessageBodyReader
and MessageBodyWriter
implementations to serialize and de-serialize data between the HTTP messages and their Java representations. The readers and writers can restrict the MIME types they are capable of processing.
MessageBodyReader
interface and/or the MessageBodyWriter
interface. Custom readers and writers are registered with the runtime when the application is started.
Natively supported types Copiar o linkLink copiado para a área de transferência!
Java Type | MIME Type |
---|---|
primitive types | text/plain |
java.lang.Number | text/plain |
byte[] | */* |
java.lang.String | */* |
java.io.InputStream | */* |
java.io.Reader | */* |
java.io.File | */* |
javax.activation.DataSource | */* |
javax.xml.transform.Source | text/xml , application/xml , application/*+xml |
javax.xml.bind.JAXBElement | text/xml , application/xml , application/*+xml |
JAXB annotated objects | text/xml , application/xml , application/*+xml |
javax.ws.rs.core.MultivaluedMap<String, String> | application/x-www-form-urlencoded [a] |
javax.ws.rs.core.StreamingOutput | */* [b] |
[a]
This mapping is used for handling HTML form data.
[b]
This mapping is only supported for returning data to a consumer.
|
Custom readers Copiar o linkLink copiado para a área de transferência!
javax.ws.rs.ext.MessageBodyReader
interface.
Example 48.1. Message reader interface
isReadable()
- The
isReadable()
method determines if the reader is capable of reading the data stream and creating the proper type of entity representation. If the reader can create the proper type of entity the method returnstrue
.Table 48.2, “Parameters used to determine if a reader can produce an entity” describes theisReadable()
method's parameters.Expand Table 48.2. Parameters used to determine if a reader can produce an entity Parameter Type Description type
Class<T>
Specifies the actual Java class of the object used to store the entity. genericType
Type
Specifies the Java type of the object used to store the entity. For example, if the message body is to be converted into a method parameter, the value will be the type of the method parameter as returned by the Method.getGenericParameterTypes()
method.annotations
Annotation[]
Specifies the list of annotations on the declaration of the object created to store the entity. For example if the message body is to be converted into a method parameter, this will be the annotations on that parameter returned by the Method.getParameterAnnotations()
method.mediaType
MediatType
Specifies the MIME type of the HTTP entity. readFrom()
- The
readFrom()
method reads the HTTP entity and coverts it into the desired Java object. If the reading is successful the method returns the created Java object containing the entity. If an error occurs when reading the input stream the method should throw anIOException
exception. If an error occurs that requires an HTTP error response, anWebApplicationException
with the HTTP response should be thrown.Table 48.3, “Parameters used to read an entity” describes thereadFrom()
method's parameters.Expand Table 48.3. Parameters used to read an entity Parameter Type Description type
Class<T>
Specifies the actual Java class of the object used to store the entity. genericType
Type
Specifies the Java type of the object used to store the entity. For example, if the message body is to be converted into a method parameter, the value will be the type of the method parameter as returned by the Method.getGenericParameterTypes()
method.annotations
Annotation[]
Specifies the list of annotations on the declaration of the object created to store the entity. For example if the message body is to be converted into a method parameter, this will be the annotations on that parameter returned by the Method.getParameterAnnotations()
method.mediaType
MediatType
Specifies the MIME type of the HTTP entity. httpHeaders
MultivaluedMap<String, String>
Specifies the HTTP message headers associated with the entity. entityStream
InputStream
Specifies the input stream containing the HTTP entity. ImportantThis method should not close the input stream.
MessageBodyReader
implementation can be used as an entity reader, it must be decorated with the javax.ws.rs.ext.Provider
annotation. The @Provider
annotation alerts the runtime that the supplied implementation provides additional functionality. The implementation must also be registered with the runtime as described in the section called “Registering readers and writers”.
javax.ws.rs.Consumes
annotation. The @Consumes
annotation specifies a comma separated list of MIME types that the custom entity provider reads. If an entity is not of a specified MIME type, the entity provider will not be selected as a possible reader.
Source
object.
Example 48.2. XML source entity reader
Custom writers Copiar o linkLink copiado para a área de transferência!
javax.ws.rs.ext.MessageBodyWriter
interface.
Example 48.3. Message writer interface
isWriteable()
- The
isWriteable()
method determines if the entity writer can map the Java type to the proper entity type. If the writer can do the mapping, the method returnstrue
.Table 48.4, “Parameters used to read an entity” describes theisWritable()
method's parameters.Expand Table 48.4. Parameters used to read an entity Parameter Type Description type
Class<T>
Specifies the Java class of the object being written. genericType
Type
Specifies the Java type of object to be written, obtained either by reflection of a resource method return type or via inspection of the returned instance. The GenericEntity
class, described in Section 45.4, “Returning entities with generic type information”, provides support for controlling this value.annotations
Annotation[]
Specifies the list of annotations on the method returning the entity. mediaType
MediatType
Specifies the MIME type of the HTTP entity. getSize()
- The
getSize()
method is called before thewriteTo()
. It returns the length, in bytes, of the entity being written. If a positive value is returned the value is written into the HTTP message'sContent-Length
header.Table 48.5, “Parameters used to read an entity” describes thegetSize()
method's parameters.Expand Table 48.5. Parameters used to read an entity Parameter Type Description t
generic Specifies the instance being written. type
Class<T>
Specifies the Java class of the object being written. genericType
Type
Specifies the Java type of object to be written, obtained either by reflection of a resource method return type or via inspection of the returned instance. The GenericEntity
class, described in Section 45.4, “Returning entities with generic type information”, provides support for controlling this value.annotations
Annotation[]
Specifies the list of annotations on the method returning the entity. mediaType
MediatType
Specifies the MIME type of the HTTP entity. writeTo()
- The
writeTo()
method converts a Java object into the desired entity type and writes the entity to the output stream. If an error occurs when writing the entity to the output stream the method should throw anIOException
exception. If an error occurs that requires an HTTP error response, anWebApplicationException
with the HTTP response should be thrown.Table 48.6, “Parameters used to read an entity” describes thewriteTo()
method's parameters.Expand Table 48.6. Parameters used to read an entity Parameter Type Description t
generic Specifies the instance being written. type
Class<T>
Specifies the Java class of the object being written. genericType
Type
Specifies the Java type of object to be written, obtained either by reflection of a resource method return type or via inspection of the returned instance. The GenericEntity
class, described in Section 45.4, “Returning entities with generic type information”, provides support for controlling this value.annotations
Annotation[]
Specifies the list of annotations on the method returning the entity. mediaType
MediatType
Specifies the MIME type of the HTTP entity. httpHeaders
MultivaluedMap<String, Object>
Specifies the HTTP response headers associated with the entity. entityStream
OutputStream
Specifies the output stream into which the entity is written.
MessageBodyWriter
implementation can be used as an entity writer, it must be decorated with the javax.ws.rs.ext.Provider
annotation. The @Provider
annotation alerts the runtime that the supplied implementation provides additional functionality. The implementation must also be registered with the runtime as described in the section called “Registering readers and writers”.
javax.ws.rs.Produces
annotation. The @Produces
annotation specifies a comma separated list of MIME types that the custom entity provider generates. If an entity is not of a specified MIME type, the entity provider will not be selected as a possible writer.
Source
objects and produces XML entities.
Example 48.4. XML source entity writer
Registering readers and writers Copiar o linkLink copiado para a área de transferência!
jaxrs:providers
element in the application's configuration file or using the JAXRSServerFactoryBean
class.
jaxrs:providers
element is a child of the jaxrs:server
element and contains a list of bean
elements. Each bean
element defines one entity provider.
Example 48.5. Registering entity providers with the runtime
JAXRSServerFactoryBean
class is a Apache CXF extension that provides access to the configuration APIs. It has a setProvider()
method that allows you to add instantiated entity providers to an application. Example 48.6, “Programmatically registering an entity provider” shows code for registering an entity provider programmatically.
Example 48.6. Programmatically registering an entity provider