Re: Replacing XMLJAXBElementProvider

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Replacing XMLJAXBElementProvider

Jakub Podlesak

Hi Tim,

All you need is to provide a custom JAXBContext resolver.
See [1] for details. Then you can implement a wrapper
for the default JAXBContext, returning Marshaller with
certain properties preset.

e.g. st. like:

public final class YourJAXBContextWrapper extends JAXBContext {


    private final JAXBContext defaultContext;

    public JSONJAXBContext(Class... classes) throws JAXBException {
        this.defaultContext = JAXBContext.newInstance(classes); // or you can use the one from Jersey
    }

    @Override
    public Marshaller createMarshaller() throws JAXBException {
        Marshaller marshaller = defaultContext.createMarshaller();
        marhaller.setProperty("KEY","VAL");
        return marshaller;
    }

    // ... etc

}

The wrapper will be then returned by your custom JAXBContext resolver.

I think, i need to provide a direct way to set custom properties
on Marshaller/Unmarshaller without a need to implement a custom
JAXBContext wrapper. For the time being i hope the above
mentioned solution would work for you.

~Jakub

[1]http://blogs.sun.com/japod/entry/better_json_available_in_jersey

On Mon, Jul 21, 2008 at 02:55:53PM -0700, Tim McCune wrote:

> As a follow-up to my "how do I specify xml namespace prefixes" question, I'm
> trying to provide my own XMLJAXBElementProvider that lets me set properties
> on the Marshaller.  I need to replace
> com.sun.jersey.impl.provider.entity.XMLJAXBElementProvider with this
> implementation.  So far, the only way I've found to do this it to remove
> META-INF/services/javax.ws.rs.ext.MessageBodyWriter from the jersey jar, so
> that jersey will find my javax.ws.rs.ext.MessageBodyWriter file instead.  Is
> this the only way to do this?  Seems like it should be possible to do
> something like this without having to build a custom jar.
>
> Thanks.
>
> --Tim

--
http://blogs.sun.com/japod

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Replacing XMLJAXBElementProvider

Jakub Podlesak

Just fixing a small typo in line...

On Tue, Jul 22, 2008 at 10:34:12AM +0200, Jakub Podlesak wrote:

>
> Hi Tim,
>
> All you need is to provide a custom JAXBContext resolver.
> See [1] for details. Then you can implement a wrapper
> for the default JAXBContext, returning Marshaller with
> certain properties preset.
>
> e.g. st. like:
>
> public final class YourJAXBContextWrapper extends JAXBContext {
>
>
>     private final JAXBContext defaultContext;
>
>     public JSONJAXBContext(Class... classes) throws JAXBException {
             ^^^^^^^^^^^^^^
this should be YourJAXBContextWrapper of course.

>         this.defaultContext = JAXBContext.newInstance(classes); // or you can use the one from Jersey
>     }
>
>     @Override
>     public Marshaller createMarshaller() throws JAXBException {
>         Marshaller marshaller = defaultContext.createMarshaller();
>         marhaller.setProperty("KEY","VAL");
>         return marshaller;
>     }
>
>     // ... etc
>
> }
>
> The wrapper will be then returned by your custom JAXBContext resolver.
>
> I think, i need to provide a direct way to set custom properties
> on Marshaller/Unmarshaller without a need to implement a custom
> JAXBContext wrapper. For the time being i hope the above
> mentioned solution would work for you.
>
> ~Jakub
>
> [1]http://blogs.sun.com/japod/entry/better_json_available_in_jersey
>
> On Mon, Jul 21, 2008 at 02:55:53PM -0700, Tim McCune wrote:
> > As a follow-up to my "how do I specify xml namespace prefixes" question, I'm
> > trying to provide my own XMLJAXBElementProvider that lets me set properties
> > on the Marshaller.  I need to replace
> > com.sun.jersey.impl.provider.entity.XMLJAXBElementProvider with this
> > implementation.  So far, the only way I've found to do this it to remove
> > META-INF/services/javax.ws.rs.ext.MessageBodyWriter from the jersey jar, so
> > that jersey will find my javax.ws.rs.ext.MessageBodyWriter file instead.  Is
> > this the only way to do this?  Seems like it should be possible to do
> > something like this without having to build a custom jar.
> >
> > Thanks.
> >
> > --Tim
>
> --
> http://blogs.sun.com/japod
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

--
http://blogs.sun.com/japod

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Replacing XMLJAXBElementProvider

Paul Sandoz
Administrator
In reply to this post by Jakub Podlesak
Jakub Podlesak wrote:
> I think, i need to provide a direct way to set custom properties
> on Marshaller/Unmarshaller without a need to implement a custom
> JAXBContext wrapper.

We need to support ContextResolver for Marshaller/Unmarshaller types.

For the latest API there have been some changes to ContextResolver
(related to enabled resolvers to be scoped by media type). As part of
this implementation i can investigate support for resolving
Marshaller/Unmarshaller.

Paul.

> For the time being i hope the above
> mentioned solution would work for you.
>
> ~Jakub
>
> [1]http://blogs.sun.com/japod/entry/better_json_available_in_jersey
>
> On Mon, Jul 21, 2008 at 02:55:53PM -0700, Tim McCune wrote:
>> As a follow-up to my "how do I specify xml namespace prefixes" question, I'm
>> trying to provide my own XMLJAXBElementProvider that lets me set properties
>> on the Marshaller.  I need to replace
>> com.sun.jersey.impl.provider.entity.XMLJAXBElementProvider with this
>> implementation.  So far, the only way I've found to do this it to remove
>> META-INF/services/javax.ws.rs.ext.MessageBodyWriter from the jersey jar, so
>> that jersey will find my javax.ws.rs.ext.MessageBodyWriter file instead.  Is
>> this the only way to do this?  Seems like it should be possible to do
>> something like this without having to build a custom jar.
>>
>> Thanks.
>>
>> --Tim
>

--
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...