2

I am writing a Java client that needs to upload a file to a server using a REST post. I have a schema for information that is to be sent along with the file, but the file itself is to be sent as an Attachment to the message. The server is not written in Java and I don't have (easy) access to the source.

How do I go about creating the post message in CXF? I found a similar SO question but it seems to use Jersey-specific classes that I can't find in CXF. CXF is already being used in the project so I'd prefer to use it, but I could use another library if required.

In case it's not obvious this is the first time I've worked with a REST service.

Community
  • 1
  • 1
jwaddell
  • 1,892
  • 1
  • 23
  • 32

1 Answers1

1

Did you see the bit in the Apache CXF User Guide wherein WebClient is used with MultipartBody, Attachment or File? The example code excerpt is shamelessly copied below:

WebClient client = WebClient.create("http://books");
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());
David J. Liszewski
  • 10,959
  • 6
  • 44
  • 57
  • Thanks - it was the section just above that in the user guide that finally made it click, that the XML file (represented in my code by a generated Java class) and the file to upload should both be treated as attachments. – jwaddell May 10 '12 at 05:17