Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.



Span
stylefont-size:2em;font-weight:bold
JAX-RS : Support for Multiparts



 

Table of Contents

Reading attachments

...

Code Block
java
java
WebClient client = WebClient.create("http://books");t
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("root", imageInputStream, cd);
client.post(new MultipartBody(att));

// or just post the attachment if it's a single part request only
client.post(att);

// or just use a file
client.post(getClass().getResource("image.png").getFile());

...

Note that if a given attachment exceeds the maximum size property (default is no-limit) then HTTP 413 status will be returned. For more information on these configuration properties, please see the documentation on Securing CXF Services.

Forms and multiparts

The Forms in HTML documents recommendation suggests that multipart/form-data requests should mainly be used to upload files.

...

Note that once a request has more than two parts then one needs to start using @Mutipart@Multipart, 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.

...

Please see RFC 6266 and this unit test for more information. 

Content-Type

If the content type (Content-Type) of the attachment is not set, it is assumed to be "application/octet-stream". This default could be overridden.

Property
org.apache.cxf.attachment.content-type

The default value for AttachmentDataSource content type in case when "Content-Type" header is not present.

XOP support

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.

Multipart Filters

...


It is possible to intercept the attachment write or read process starting from CXF 3.1.12.

...