users@jersey.java.net

[Jersey] Re: JAXB Based JSON support not working?

From: Cyril Bouteille <cyril_at_RewardsPay.com>
Date: Wed, 08 May 2013 16:08:20 -0700

Hi Christian, it doesn't seem like your client is setting the accept and
content-type headers properly...
WebResource methods to set those use a builder pattern. They return a
RequestBuilder object which you're supposed to re-use to add additional
configuration.
E.g. when you do resource.accept(MediaType.APPLICATION_JSON), I'm not
100% sure, but I believe that the setting essentially does not take
effect as you do not capture the returned object and use it for your post.
See the content type interpreted by Jersey in the error of your original
message highlighted in red below.

Try the following instead:

ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                                   .type(MediaType.APPLICATION_JSON)
                                   .post(ClientResponse.class, pair);

Cyril

On 05/03/2013 12:00 AM, Christian Bachmaier wrote:
> Hi all,
>
> it would be nice if some of you could help me to solve the following
> problem.
>
> If think that JAXB based JSON support with the Jersey 1.17 Client API
> does not work while posting Parameter objects.
> The code in the attachement delivers
>
> 415
> [text/html;charset=utf-8]
> Mai 02, 2013 10:11:13 AM com.sun.jersey.api.client.ClientResponse
> getEntity
> SEVERE: A message body reader for Java class common.Pair, and Java
> type class common.Pair, and MIME media type text/html; charset=utf-8
> was not found
> Mai 02, 2013 10:11:13 AM com.sun.jersey.api.client.ClientResponse
> getEntity
> SEVERE: The registered message body readers compatible with the MIME
> media type are:
> / ->
> com.sun.jersey.core.impl.provider.entity.FormProvider
> com.sun.jersey.core.impl.provider.entity.StringProvider
> com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
> com.sun.jersey.core.impl.provider.entity.FileProvider
> com.sun.jersey.core.impl.provider.entity.InputStreamProvider
> com.sun.jersey.core.impl.provider.entity.DataSourceProvider
> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
> com.sun.jersey.core.impl.provider.entity.ReaderProvider
> com.sun.jersey.core.impl.provider.entity.DocumentProvider
> com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
>
> com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
> com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
> com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
> com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
> com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
> com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
> com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
> com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
> com.sun.jersey.core.impl.provider.entity.EntityHolderReader
> com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
> com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
> com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
>
> Exception in thread "main"
> com.sun.jersey.api.client.ClientHandlerException: A message body
> reader for Java class common.Pair, and Java type class common.Pair,
> and MIME media type text/html; charset=utf-8 was not found
> at
> com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561)
> at
> com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
> at client.ComputeConcat.main(ComputeConcat.java:41)
>
>
> Line 41 is the getEntity call.
>
>
> If changing @Consumes({MediaType.APPLICATION_JSON}) to
> @Consumes({MediaType.APPLICATION_XML}) for the concat method, then the
> code operates and delivers
>
> 200
> [application/json]
> Hello, World!
>
> However, then the input pair object is sent as XML and not as JSON
> serialization. It seems like the Jersy Client does not automatically
> serialize the pair object to JSON but only to XML, although imho it
> should.
>
> Please note, while having all set JSON as in the attached code the
> commented curl call operates as desired, sends JSON and receives JSON.
>
> Do I make any mistakes? Btw., currently I have all jars of Jersey in
> the classpath.
>
> Many thanks for any suggestions,
>