users@jersey.java.net

Re: [Jersey] Need UriService object in XmlAdapter classes

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 08 Feb 2010 11:12:06 +0100

Hi Babu,

In your ContextResolver<Unmarshaller> implementation create an
instance of JSONJAXBContext and return the Unmarshaller created from
that. Jersey will work out what to do when JSON is serialized.

When you instantiate the the Unmarshaller in your
ContextResolver<Unmarshaller> implementation you can register
instances of XmlAdapter:

   http://java.sun.com/javase/6/docs/api/javax/xml/bind/Unmarshaller.html#setAdapter%28java.lang.Class,%20A%29

You can inject the required information on your
ContextResolver<Unmarshaller> implementation then pass that
information to the constructors of your XmlAdapter implementations to
be registered.

Hth,
Paul.


On Feb 5, 2010, at 12:39 AM, Babu Naidu wrote:

> Hi,
>
> I have IFoo interface that is being implemented by Foo and RFoo. The
> RFoo class contains JAXB annotations and it is returned by resource
> classes. I also have IFooXmlAdapter class that marshalls/unmarshalls
> IFoo and RFoo. RFoo object also contains hypermedia (links) and I'll
> be creating instances of it in the IFooXmlAdapter class, I need
> access to UriService (we have a custom UriService injectable
> provider which injects custom UriService, which works for resource
> and provider classes).
>
> I was thinking that I could do the following, however they don't
> seem feasible or clear to me.
>
> 1) I can set an instance of UriService as property in HttpContext in
> my resource class and access it in IFooXmlAdapter by accessing
> HttpContext, however I am not sure if it is feasible and how to do
> this.
> 2) Paul answered this question already with the following response,
> however I am confused as to how to create UnMarshaller and
> Marshaller and whether it works for both application/xml and
> application/json media types. Can anyone please elaborate on how to
> accomplish or solve this problem? any pointers to classes or
> articles or both is greatly appreciated.
>
>
> Please let me know if you've any questions.
>
> Thanks
> Babu
>
> =====================
> Paul's response is here http://markmail.org/search/?q=jersey+XmlAdapter#query
> :jersey%20XmlAdapter+page:2+mid:2j3j2zgvxntywgvk+state:results
>
> I think it is possible to set adapter instances in the un/marshaller.
> This means it should be possible to inject on those instances. You
> could use a ContextResolver<Unmarshaller> and
> ContextResolver<Unmarshaller> to return a Un/marsahller for the
> required JAXB type. The resolver instances can have UriInfo injected
> on it, and this can be passed to the adapters.
> public class XXXContextResolver<Unmarshaller> { public
> XXXContextResolver(@Context UriInfo ui) { // initiate adapters ... }
> ... }
> Paul.
> ========================
>