users@jersey.java.net

Re: [Jersey] Jersey client accessing Jesery REST server message writer issue

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Tue, 10 Aug 2010 17:20:37 +0200

On Aug 10, 2010, at 5:13 PM, Jimi Hullegård wrote:

> Ah, yes, of course! The object I tried to send is not a root
> element. When I marshalled it manually I had to wrap it in a
> JAXBElement, I have no idea why I didn't think of doing the same
> thing here. I guess I just thought that if it was needed it would do
> it automatically for me.
>

Jersey currently does not derive a XML root element from a JAXB type.
We do it for lists of JAXB types using a pluralizer but for the case
of JAXB types of think it is best to be explicit and closer to how
JAXB behaves (it cannot marshall a JAXB type).


> It now works just great (after also updating the server side code to
> take a JAXBElement instead).
>

Great.


> Thanks for leading me in the right direction.
>

Np.

Paul.

> Regards
> /Jimi
>
> ________________________________________
> Från: Paul Sandoz [Paul.Sandoz_at_oracle.com]
> Skickat: den 10 augusti 2010 14:35
> Till: users_at_jersey.dev.java.net
> Ämne: Re: [Jersey] Jersey client accessing Jesery REST server
> message writer issue
>
> Hi Jimi,
>
> If it works for GET and not POST it could signal a different issue,
> perhaps something to do with the definition of the JAXB beans.
>
> From the list you send it all looks OK.
>
> Is the class of the JAXB bean you are sending annotated with
> @XmlRootElement?
>
> Would it be possible to share your code? (send privately if disclosure
> is an issue?)
>
> Paul.
>
> On Aug 10, 2010, at 1:38 PM, Jimi Hullegård wrote:
>
>> I must say that I don't quite follow you now, Paul.... You say "you
>> definitely need to tell Jersey
>> what the JAXBContext should be, and that is what ContextResolver is
>> for", but then why doesn't it work for me? I already have registered
>> the resolver using
>> "clientConfig.getClasses().add(JAXBContextResolver.class);" and that
>> worked fine when jersey was fetching jaxb objects using GET, but
>> sending objects (using POST) doesn't work.
>>
>> Is there something else I have been missing?
>>
>> In debug mode in Eclipse I managed to see all the message body
>> writers registered in the init() method of MessageBodyFactory, and
>> here they are, with their corresponding types:
>>
>> com.sun.jersey.core.impl.provider.entity.SourceProvider
>> $SourceWriter_at_16ade67 [application/xml, text/xml, */*]
>> com.sun.jersey.core.impl.provider.entity.StringProvider_at_10df1a3
>> [text/plain, */*]
>> com.sun.jersey.core.impl.provider.entity.ByteArrayProvider_at_16d392f
>> [application/octet-stream, */*]
>> com.sun.jersey.core.impl.provider.entity.FileProvider_at_1ad09a1
>> [application/octet-stream, */*]
>> com.sun.jersey.core.impl.provider.entity.InputStreamProvider_at_115a135
>> [application/octet-stream, */*]
>> com.sun.jersey.core.impl.provider.entity.DataSourceProvider_at_1541633
>> [application/octet-stream, */*]
>> com
>> .sun.jersey.core.impl.provider.entity.RenderedImageProvider_at_5c8344
>> [image/*]
>> com
>> .sun.jersey.core.impl.provider.entity.MimeMultipartProvider_at_be19bd
>> [*/*]
>> com.sun.jersey.core.impl.provider.entity.FormProvider_at_169cceb
>> [application/x-www-form-urlencoded, */*]
>> com
>> .sun
>> .jersey.core.impl.provider.entity.FormMultivaluedMapProvider_at_149ad2f
>> [application/x-www-form-urlencoded]
>> com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider
>> $App_at_42c7ff [application/xml]
>> com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider
>> $Text_at_133bce4 [text/xml]
>> com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider
>> $General_at_140284d [*/*]
>> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider
>> $App_at_115bb4f [application/xml]
>> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider
>> $Text_at_12f69d [text/xml]
>> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider
>> $General_at_8e4fe4 [*/*]
>> com.sun.jersey.core.impl.provider.entity.XMLListElementProvider
>> $App_at_1f8d56 [application/xml]
>> com.sun.jersey.core.impl.provider.entity.XMLListElementProvider
>> $Text_at_8e78a1 [text/xml]
>> com.sun.jersey.core.impl.provider.entity.XMLListElementProvider
>> $General_at_191bf72 [*/*]
>> com.sun.jersey.core.impl.provider.entity.ReaderProvider_at_1b00f9e
>> [text/plain, */*]
>> com.sun.jersey.core.impl.provider.entity.DocumentProvider_at_1d15a38
>> [application/xml, text/xml, */*]
>> com
>> .sun
>> .jersey.core.impl.provider.entity.StreamingOutputProvider_at_155d3c5
>> [application/octet-stream, */*]
>> com
>> .sun.jersey.atom.rome.impl.provider.entity.AtomFeedProvider_at_176acb1
>> [*/*]
>> com
>> .sun.jersey.atom.rome.impl.provider.entity.AtomEntryProvider_at_17283b5
>> [*/*]
>> com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider
>> $App_at_124eb13 [application/json]
>> com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider
>> $General_at_c53530 [*/*]
>> com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider
>> $App_at_1c55158 [application/json]
>> com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider
>> $General_at_1b85b9f [*/*]
>> com.sun.jersey.json.impl.provider.entity.JSONListElementProvider
>> $App_at_87632e [application/json]
>> com.sun.jersey.json.impl.provider.entity.JSONListElementProvider
>> $General_at_806354 [*/*]
>> com
>> .sun
>> .jersey.json.impl.provider.entity.JSONWithPaddingProvider_at_1fbffde [*/
>> *]
>> com
>> .sun
>> .jersey
>> .fastinfoset
>> .impl.provider.entity.FastInfosetRootElementProvider_at_b4040d
>> [application/fastinfoset]
>> com
>> .sun
>> .jersey
>> .fastinfoset
>> .impl.provider.entity.FastInfosetJAXBElementProvider_at_762e5d
>> [application/fastinfoset]
>>
>> Does this look OK in your eyes? Is there something else I can debug
>> in order to get closer to solving this mystery?
>>
>> /Jimi
>>
>>
>> ________________________________________
>> Från: Paul Sandoz [Paul.Sandoz_at_oracle.com]
>> Skickat: den 10 augusti 2010 12:45
>> Till: users_at_jersey.dev.java.net
>> Ämne: Re: SV: [Jersey] Jersey client accessing Jesery REST server
>> message writer issue
>>
>> By default Jersey creates a JAXBContext using:
>>
>> JAXBCOntext.newInstance(type)
>>
>> http://download.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html#newInstance%28java.lang.Class...%29
>>
>> protected JAXBContext getStoredJAXBContext(Class type) throws
>> JAXBException {
>> synchronized (jaxbContexts) {
>> JAXBContext c = jaxbContexts.get(type);
>> if (c == null) {
>> c = JAXBContext.newInstance(type);
>> jaxbContexts.put(type, c);
>> }
>> return c;
>> }
>> }
>>
>> There is no class loader to specify as the type (the type of the JAXB
>> bean to parse or serialize) is already loaded. You only need to
>> specify the class loader when creating a JAXBContext and declaring a
>> location for the context path.
>>
>> By default Jersey only has the type to go on. I suspect in your case
>> the type was not sufficient and you definitely need to tell Jersey
>> what the JAXBContext should be, and that is what ContextResolver is
>> for (which can also declare the Marshaller or Unmarshaller too).
>>
>> Hth,
>> Paul.
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>