users@jersey.java.net

Problems with Jersey client and multipart/mixed POST

From: Mark Petrovic <mark_at_petrovic.org>
Date: Thu, 28 May 2009 18:47:51 -0700

Good day.

This feels like a FAQ question, but I've banged my head on this long
enough and have concluded it's time to ask for help.



Craig posted an example of exactly what I want to do: use multipart/
mixed to post a JAXB object and an attendant binary stream in a single
POST operation:

https://jersey.dev.java.net/servlets/ReadMsg?listName=users&msgNo=3579

However, when I try this same technique in a client I wrote, the
client throws an exception

Exception in thread "main"
com.sun.jersey.api.client.ClientHandlerException:
com.sun.jersey.api.client.ClientHandlerException: A message body
writer for Java type, class com.sun.jersey.multipart.MultiPart, and
MIME media type, multipart/mixed, was not found
        at
com
.sun
.jersey
.client
.urlconnection
.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128)
        at com.sun.jersey.api.client.Client.handle(Client.java:397)
        at com.sun.jersey.api.client.WebResource.voidHandle(WebResource.java:
579)
        at com.sun.jersey.api.client.WebResource.access$500(WebResource.java:
69)
        at com.sun.jersey.api.client.WebResource
$Builder.post(WebResource.java:487)
        at
org
.carpediem.conman.client.ConmanClient.createContact(ConmanClient.java:
36)
        at org.carpediem.conman.client.ConmanClient.run(ConmanClient.java:21)
        at org.carpediem.conman.client.ConmanClient.main(ConmanClient.java:15)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message
body writer for Java type, class com.sun.jersey.multipart.MultiPart,
and MIME media type, multipart/mixed, was not found
        at
com
.sun
.jersey
.api
.client
.TerminatingClientHandler
.writeRequestEntity(TerminatingClientHandler.java:308)
        at
com
.sun
.jersey
.client
.urlconnection
.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:179)
        at
com
.sun
.jersey
.client
.urlconnection
.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:126)
        ... 7 more


Here is the client code:

package org.carpediem.conman.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.multipart.MultiPart;
import org.apache.commons.cli.CommandLine;
import org.carpediem.schema.conman.ContactInfo;

import javax.ws.rs.core.MediaType;
import java.io.FileInputStream;

public class ConmanClient extends Base {

     public static void main(String[] argv) {
         ... // setup
     }

     private void createContact(String emailAddress, String
documentFile, String mimeType) throws Exception {
         // A JAXB object
         ContactInfo cInfo = cInfo(emailAddress, mimeType);

         Client client = Client.create();
         WebResource service = client.resource("http://localhost:8080/conman/services
");

         MultiPart mp = new MultiPart();
         mp.bodyPart(cInfo, MediaType.APPLICATION_XML_TYPE)
                 .bodyPart(new FileInputStream(documentFile),
MediaType.APPLICATION_OCTET_STREAM_TYPE);

         service.path("/contacts").type("multipart/mixed").post(mp);
     }

}


The base class "Base" contains only uninteresting non-JAX-WS-related
helper methods.

I'm using maven to drive the build and exec; the pom.xml file
references these dependencies related to Jersey:

         <dependency>
             <groupId>com.sun.jersey</groupId>
             <artifactId>jersey-client</artifactId>
             <version>1.1.0-ea</version>
         </dependency>
         <dependency>
             <groupId>com.sun.jersey.contribs</groupId>
             <artifactId>jersey-multipart</artifactId>
             <version>1.1.0-ea</version>
         </dependency>


I read the "example upload" code that Paul posted sometime back, which
is similar and I thought instructive:

https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=5738

...and I'm still baffled as to how my code differs from either Craig's
or Paul's such that the exception obtains. And I haven't even gotten
to debugging the REST resource handler in the server :-)


Can someone please help clear up why the exception obtains? I tried
writing my own MessageBodyWriter, but I didn't have much luck with
that. Besides, for a JAXB object and a binary stream, I didn't think
I needed one??

Thanks.

Mark



--
Mark Petrovic
mark_at_petrovic.org
http://www.petrovic.org