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.此内容没有您所选择的语言版本。
Chapter 49. Entity Support
Abstract
The Apache CXF runtime supports a limited number of mappings between MIME types and Java objects out of the box. Developers can extend the mappings by implementing custom readers and writers. The custom readers and writers are registered with the runtime at start-up.
Overview 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The runtime relies on JAX-RS
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.
The runtime provides readers and writers for a number of common mappings. If an application requires more advanced mappings, a developer can provide custom implementations of the
MessageBodyReader interface and/or the MessageBodyWriter interface. Custom readers and writers are registered with the runtime when the application is started.
Natively supported types 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Table 49.1, “Natively supported entity mappings” lists the entity mappings provided by Apache CXF out of the box.
| 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 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Custom entity readers are responsible for mapping incoming HTTP requests into a Java type that a service's implementation can manipulate. They implement the
javax.ws.rs.ext.MessageBodyReader interface.
The interface, shown in Example 49.1, “Message reader interface”, has two methods that need implementing:
Example 49.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 49.2, “Parameters used to determine if a reader can produce an entity” describes theisReadable()method's parameters.Expand Table 49.2. Parameters used to determine if a reader can produce an entity Parameter Type Description typeClass<T>Specifies the actual Java class of the object used to store the entity. genericTypeTypeSpecifies 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.annotationsAnnotation[]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.mediaTypeMediatTypeSpecifies 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 anIOExceptionexception. If an error occurs that requires an HTTP error response, anWebApplicationExceptionwith the HTTP response should be thrown.Table 49.3, “Parameters used to read an entity” describes thereadFrom()method's parameters.Expand Table 49.3. Parameters used to read an entity Parameter Type Description typeClass<T>Specifies the actual Java class of the object used to store the entity. genericTypeTypeSpecifies 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.annotationsAnnotation[]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.mediaTypeMediatTypeSpecifies the MIME type of the HTTP entity. httpHeadersMultivaluedMap<String, String>Specifies the HTTP message headers associated with the entity. entityStreamInputStreamSpecifies the input stream containing the HTTP entity. ImportantThis method should not close the input stream.
Before an
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”.
By default a custom entity provider handles all MIME types. You can limit the MIME types that a custom entity reader will handle using the
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.
Example 49.2, “XML source entity reader” shows an entity reader the consumes XML entities and stores them in a
Source object.
Example 49.2. XML source entity reader
Custom writers 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Custom entity writers are responsible for mapping Java types into HTTP entities. They implement the
javax.ws.rs.ext.MessageBodyWriter interface.
The interface, shown in Example 49.3, “Message writer interface”, has three methods that need implementing:
Example 49.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 49.4, “Parameters used to read an entity” describes theisWritable()method's parameters.Expand Table 49.4. Parameters used to read an entity Parameter Type Description typeClass<T>Specifies the Java class of the object being written. genericTypeTypeSpecifies 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 GenericEntityclass, described in Section 47.4, “Returning entities with generic type information”, provides support for controlling this value.annotationsAnnotation[]Specifies the list of annotations on the method returning the entity. mediaTypeMediatTypeSpecifies 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-Lengthheader.Table 49.5, “Parameters used to read an entity” describes thegetSize()method's parameters.Expand Table 49.5. Parameters used to read an entity Parameter Type Description tgeneric Specifies the instance being written. typeClass<T>Specifies the Java class of the object being written. genericTypeTypeSpecifies 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 GenericEntityclass, described in Section 47.4, “Returning entities with generic type information”, provides support for controlling this value.annotationsAnnotation[]Specifies the list of annotations on the method returning the entity. mediaTypeMediatTypeSpecifies 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 anIOExceptionexception. If an error occurs that requires an HTTP error response, anWebApplicationExceptionwith the HTTP response should be thrown.Table 49.6, “Parameters used to read an entity” describes thewriteTo()method's parameters.Expand Table 49.6. Parameters used to read an entity Parameter Type Description tgeneric Specifies the instance being written. typeClass<T>Specifies the Java class of the object being written. genericTypeTypeSpecifies 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 GenericEntityclass, described in Section 47.4, “Returning entities with generic type information”, provides support for controlling this value.annotationsAnnotation[]Specifies the list of annotations on the method returning the entity. mediaTypeMediatTypeSpecifies the MIME type of the HTTP entity. httpHeadersMultivaluedMap<String, Object>Specifies the HTTP response headers associated with the entity. entityStreamOutputStreamSpecifies the output stream into which the entity is written.
Before a
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”.
By default a custom entity provider handles all MIME types. You can limit the MIME types that a custom entity writer will handle using the
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.
Example 49.4, “XML source entity writer” shows an entity writer that takes
Source objects and produces XML entities.
Example 49.4. XML source entity writer
Registering readers and writers 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Before a JAX-RS application can use any custom entity providers, the custom providers must be registered with the runtime. Providers are registered with the runtime using either the
jaxrs:providers element in the application's configuration file or using the JAXRSServerFactoryBean class.
The
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 49.5, “Registering entity providers with the runtime” show a JAX-RS server configured to use a set of custom entity providers.
Example 49.5. Registering entity providers with the runtime
The
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 49.6, “Programmatically registering an entity provider” shows code for registering an entity provider programmatically.
Example 49.6. Programmatically registering an entity provider