users@jersey.java.net

Best Way to Upload Binary Data and XML

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Fri, 17 Sep 2010 16:52:42 -0700 (PDT)

Hello,

I'm using Jersey 1.1.5.1 and JAXB. I have an API for, among other things,
creating invoices, and have an XML representation of the Invoice defined in an
XSD, with a corresponding generated Invoice JAXB bean. All works great.

Now, we want to add the ability to attach arbitrary documents when creating new
invoices. I was thinking I would just attach the document in a separate REST
call, i.e., REST invocation #1 creates the invoice, REST invocation #2 adds an
attachment to the newly created invoice. I posted a message a few days ago about
how to implement #2, which Paul helpfully answered.

But now I realize that is the wrong approach; when the invoice is created,
certain business logic happens, e.g., the invoice might be sent to the customer.
So I have to create the invoice with the attachment(s) right up front.

I think there are two ways I can implement this:

1) Change my XML definition of Invoice, adding a new element of type
xsd:base64binary, and the submitted XML will have everything.

2) Leave my XML definition the same, but do a multipart POST, where one part is
the XML, and the other part(s) is/are the attachment(s).

In either case, I'm going to expose a new URI for the attachments.

../invoices/<id>
../invoices/<id>/attachments

I guess one advantage of approach #1 is that I will have the option to retrieve
the attachments at the same time as I retrieve the rest of the invoice; if I go
with approach #2, I will always have to make two calls to get the attachments.

Is there a best practice here? Or does anybody have thoughts in general on this?

Thanks,

Charles