users@jersey.java.net

[Jersey] Pojo based multipart support

From: John Zajac <jzajac_at_expedia.com>
Date: Tue, 17 Feb 2015 00:47:23 +0000

I see the support for multipart as specified in https://jersey.java.net/nonav/documentation/latest/user-guide.html#multipart

In this you can manually add bodyParts to a MultiPart instance and post it.

However, is there support for multipart via an xsd generated pojo that has multiple binary fields that I want transported as such, not base64, as attachments? I don't see any evidence of support for this in the doc etc, but want to ask before I give up on it.

What I have in mind is defining a request in xsd to include lists of items where each item contains, among other things, a binary field such as this:

                  <xs:element name="ProductBin">
                        <xs:complexType>
                              <xs:sequence>
                                    <xs:element name="d" minOccurs="0" type="xs:base64Binary"
                                     xmime:expectedContentTypes="application/octet-stream"/>
                              </xs:sequence>
                        </xs:complexType>
                  </xs:element>


The expectation is that xmine:expectedContentTypes means that the data not be base64 encoded but included as an attachment in binary. The generated code then includes the annotation:

        @XmlMimeType("application/octet-stream")
        protected DataHandler d;

to instruct Jaxb on this.

Jaxb supports this, with some work by someone, via AttachmentMarshaller. Does Jersey support this and if so what would some sample client and server code look like keeping in mind I do not want to manually add bodyparts, but want the invocation to look something like the following where I just provide the pojo (request) as I would if binary/multipart was not involved?

         Invocation.Builder builder = target.request(mediaType); // want to support both xml and fastinfoset (I have my own fi provider)

         MyResponse response = builder.post(Entity.entity(request, mediaType), MyResponse.class);