JAX-RS : Support for Multiparts
Individual parts can be mapped to StreamSource, InputStream, DataSource or custom Java types for which message body readers are available.
Note that in this example it's expected that the root part named 'rootPart' is a text-xml Book representation, while a part named 'book2' is a Book JSON sequence.
All attachment parts can be accessed as a list of Attachment with Attachment making it easy to deal with a given part:
For example, Attachment class can be used to get to a Content-Disposition header, when dealing with the form submission of files.
Similarly, the whole request body can be represented as a MultipartBody:
When handling complex multipart/form-data submissions (such as those containing files) MultipartBody (and Attachment) need to be used directly. In simpler cases, when every form part can be captured by a String, the following code will suffice:
When working with either List of Attachments or MultipartBody, one may want to process the individual parts with the help of some custom procedures. Starting from CXF 2.3.0 it is also possible to do the following:
Multipart annotation and Optional attachments
When you write the code like this
the runtime will return a 400 status if either "rootPart" or "book2" parts can not be found in the multipart payload.
Starting from 2.5.1 it is possible to request the runtime to report a null value in case of missing parts:
The above code requires the "rootPart" part be available and can handle the case where the "book2" part is missing.
Starting from 2.2.4 it is also possible to write attachments to the output stream, both on the client and server sides.
On the server side it is sufficient to update the @Produces value for a given method:
Note that a 'type' parameter of the 'multipart/mixed' media type indicates that all parts in the multiparts response should have a Content-Type header set to 'text/xml' for both getBooksAsMultipart() and getBookAsMultipart() method responses. The getBooksAsMultipart() response will have 3 parts, the first part will have its Content-ID header set to "email@example.com", the next parts will have '1' and '2' ids. The getBookAsMultipart() response will have a single part only with its Content-ID header set to "firstname.lastname@example.org".
When returning mixed multiparts containing objects of different types, you can either return a Map with the media type string value to Object pairs or MultipartBody:
Similarly to the method returning a list in a previous code fragment, getBooks() will have the response serialized as multiparts, where the first part will have its Content-ID header set to "email@example.com", the next parts will have ids like '1', '2', etc.
In getBooks2() one can control the content ids of individual parts.
You can also control the contentId and the media type of the root attachment by using a Multipart annotation:
One can also have lists or maps of DataHandler, DataSource, Attachment, byte arrays or InputStreams handled as multiparts.
On the client side multiparts can be written the same way. For example:
Note a new WebClient.postAndGetCollection which can be used for a type-safe retrieval of collections. A similar WebClient.getCollection has also been added.
When using proxies, a Multipart annotation attached to a method parameter can also be used to set the root contentId and media type. Proxies do not support at the moment multiple method parameters annotated with Multipart (as opposed to the server side) but only a single multipart parameter:
A method-level Multipart annotation will affect the writing on the server side and the reading on the client side. A parameter-level Multipart annotation will affect writing on the client (proxy) side and reading on the server side. You don't have to use Multipart annotations.
Uploading files with Client API
At the moment the only way to upload a file is to use a MultipartBody, Attachment or File:
Using File provides a simpler way as the runtime can figure out how to create a ContentDisposition from a File.
Reading large attachments
One can use the following properties to set up folder, memory threshold and max size (from CXF 2.4.4 and 2.5) values when dealing with large attachments:
Note that such properties can be set up on a per-endpoint basis. Alternatively you can set "attachmentDirectory", "attachmentThreshold" and "attachmentMaxSize" properties directly on either org.apache.cxf.jaxrs.provider.MultipartProvider or, when dealing with multipart/form-data payloads, org.apache.cxf.jaxrs.provider.FormEncodingProvider.
Alternatively, you might want to set the following system properties which will apply to all endpoints:
Starting from CXF 2.5.0 and 2.4.4:
Note that if a given attachment exceeds the maximum size property (default is no-limit) then HTTP 413 status will be returned.
Forms and multiparts
As mentioned in the previous section, one way to deal with multipart/form-data submissions is to deal directly with a CXF JAXRS Attachment class and get a Content-Disposition header and/or the underlying input stream.
It is now possible (since 2.2.5) to have individual multipart/form-data parts read by registered JAX-RS MessageBodyReaders, something that is already possible to do for types like multipart/mixed or multipart/related.
For example this request can be handled by a method with the following signature:
Similarly, this request can be handled by a method with the following signature:
Note that once a request has more than two parts then one needs to start using @Mutipart, the values can refer to either ContentId header or to ContentDisposition/name. Note that at the moment using @Multipart is preferred to using @FormParam unless a plain name/value submission is dealt with. The reason is that @Multipart can also specify an expected media type of the individual part and thus act similarly to a @Consume annotation.
When dealing with multiple parts one can avoid using @Multipart and just use List, ex, List\<Atachment\>, List\<Book\>, etc.
Finally, multipart/form-data requests with multiple files (file uploads) can be supported too. For example, this request can be handled by a method with the signature like :
If you need to know the names of the individual file parts embedded in a "files" outer part (such as "book1" and "book2"), then please use List<Attachment> instead. It is currently not possible to use a Multipart annotation to refer to such inner parts but you can easily get the names from the individual Attachment instances representing these inner parts.
Note that it is only the last request which has been structured according to the recommendation on how to upload multiple files but it is more complex than the other simpler requests linked to in this section.
Please note that using JAX-RS FormParams is recommended for dealing with plain application/www-url-encoded submissions consisting of name/value pairs only.
Content-Disposition UTF-8 file names
Starting from CXF 3.0.4 it is possible to specify a Content-Disposition file names in a UTF-8 format, using a "filename*" Content-Disposition extension parameter as opposed to the "filename" one.
CXF JAXRS clients and endpoints can support XML-binary Optimized Packaging (XOP).
What it means at a practical level is that a JAXB bean containing binary data is serialized using a multipart packaging, with the root part containing non-binary data only but also linking to co-located parts containing the actual binary payloads. Next it is deserialized into a JAXB bean on the server side.
If you'd like to experiment with XOP then you need to set an "mtom-enabled" property on CXF jaxrs endpoints and clients.
Please see JAXRSMultipartTest (testXopWebClient) and MultipartStore (addBookXop) for more details.
Note about Struts
If you are using CXF and Struts2 within the same application and expecting CXF to process multipart/form-data payloads then you need to make sure Struts2 dispatcher is not consuming the request input stream.
One option is to let Struts2 handle URIs matching some specific patterns only, for example:
Alternatively,disabling a "struts.multipart.parser" property might help.