Answering my own question, I just looked at the RESTful Web Services Cookbook,
and they recommend approach #2, using a multipart/mixed POST. So I've downloaded
the Jersey Multipart stuff and I'm starting to play with it.
I think I would also like to preserve the non-mulitpart POST behavior. Will it
be valid to have two methods for the same path, with different @Consumes? Like
this:
@POST
@Path("/invoices")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response createInvoice(InvoiceType invoiceType...
and
@POST
@Path("/invoices")
@Consumes(MultiPartMediaTypes.MULTIPART_MIXED)
public Response createInvoice(MultiPart multiPart...
I know I'll find this out as I develop it, but I was hoping I could get a quick
answer beforehand.
Thanks,
Charles
________________________________
From: Charles Overbeck <coverbec_at_pacbell.net>
To: users_at_jersey.dev.java.net
Sent: Fri, September 17, 2010 4:52:42 PM
Subject: [Jersey] Best Way to Upload Binary Data and XML
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