users@jersey.java.net

Re: [Jersey] ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.MultiPart, and MIME media type, application/octet-stream, was not found

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Thu, 11 Dec 2008 23:10:10 -0800

Gili wrote:
> Chris, this code is giving me the exception:
>
> MultiPart multipart = new MultiPart();
> WebResource videos = client.resource(videosUri);
> ClientResponse response = videos.post(ClientResponse.class, multipart);
>
> Clearly I am not doing anything fishy here. It almost sounds to me like
> something is wrong in Jersey itself, not my code. How do I know what
> providers jersey-client has registered? The server lists them on startup but
> the client does not.
>
>
First, a double check ... you're using Jersey 1.0.1, right? And the
1.0.1 version of Jersey Multipart as well?

The first observation I have is that you *should* really be setting a
media type on the body part (there is no default in BodyPart itself).
The fact that you don't, but that your entity is of type "byte[]", says
you should be invoking the MessageBodyWriter instance that Jersey is
required by the spec to provide (see section 4.2.4) ... but the standard
one is based on matching a provided media type, and null probably won't
match.

Could you try two experiments for me (separately)?

* Explicitly set the content type on your body part:

    dataPart.setMediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE);

  My gut says this should cause the correct matching to occur, and
should be the
  simplest solution. It's also a best practice for the client to
explicitly declare what
  it is sending, rather than relying on a framework default.

* Set up a test scenario where you are uploading *just* the data by
itself, leaving
  jersey-multipart out of the picture (to see if we can isolate where
the problem lies).

Craig (not Chris :-)

> Gili
>
>
> Gili wrote:
>
>> This doesn't make any sense. I ran a diff and as far as I can tell I
>> didn't change anything substantial in in my code. And no, I am not
>> setting the media type on any body-part, I am just invoking:
>>
>> BodyPart dataPart = new BodyPart();
>> dataPart.getHeaders().add("Content-Disposition", "inline; name=data");
>> dataPart.setEntity(videoVersion.getData());
>> multipart.bodyPart(dataPart);
>>
>> and getData() in this case returns byte[].
>>
>> Gili
>>
>> Craig McClanahan (via Nabble) wrote:
>>
>>> Gili wrote:
>>>
>>> > I don't remember what I changed in the past two days but all of a
>>> sudden I am
>>> > getting this exception:
>>> >
>>> > ClientHandlerException: A message body writer for Java type, class
>>> > com.sun.jersey.multipart.MultiPart, and MIME media type,
>>> > application/octet-stream, was not found
>>> >
>>> > when invoking WebResource.post(ClientResponse.class, multipart). This
>>> should
>>> > be working, right?
>>> >
>>> > Gili
>>> >
>>> Only if you provide a MessageBodyWriter that knows how to handle
>>> "application/octet-stream" (I presume you must be setting this media
>>> type on one of your body parts). None of the default ones do.
>>>
>>> Craig
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@...
>>> <http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1646581&i=0>
>>> For additional commands, e-mail: users-help@...
>>> <http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1646581&i=1>
>>>
>>>
>>>
>>> ------------------------------------------------------------------------
>>> This email is a reply to your post @
>>> http://n2.nabble.com/ClientHandlerException%3A-A-message-body-writer-for-Java-type%2C-class-com.sun.jersey.multipart.MultiPart%2C-and-MIME-media-type%2C-application-octet-stream%2C-was-not-found-tp1646521p1646581.html
>>> You can reply by email or by visting the link above.
>>>
>>>
>>
>
>