users@jersey.java.net

[Jersey] Re: How to obtain the default JAXBContext?

From: Markus Karg <markus.karg_at_gmx.net>
Date: Tue, 24 May 2011 19:38:56 +0200

Pavel,

thank you for your ideas.

Seems to be rather complex, maybe we should add some facility in JAX RS 2 to
obtain the context.

Regards
Markus

> -----Original Message-----
> From: Pavel Bucek [mailto:pavel.bucek_at_oracle.com]
> Sent: Dienstag, 24. Mai 2011 14:19
> To: users_at_jersey.java.net
> Subject: [Jersey] Re: How to obtain the default JAXBContext?
>
> Hello,
>
> you can take advantage of AbstractJAXBProvider from jersey-core and do
> this:
>
> @Provider
> public static class FooWriter extends AbstractJAXBProvider<Foo> {
> public FooWriter(@Context Providers providers) {
> super(providers, MediaType.APPLICATION_XML_TYPE);
> }
>
>
> @Override
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> return type.equals(Foo.class);
> }
>
> @Override
> public long getSize(Foo foo, Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> return -1;
> }
>
> @Override
> public void writeTo(Foo foo, Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,
> Object> httpHeaders, OutputStream entityStream) throws IOException,
> WebApplicationException {
> try {
> System.out.println("### " +
> getStoredJAXBContext(Foo.class));
> } catch (Exception e) {
> //...
> }
>
> }
>
> @Override
> public boolean isReadable(Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> return false;
> }
>
> @Override
> public Foo readFrom(Class<Foo> type, Type genericType,
> Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,
> String> httpHeaders, InputStream entityStream) throws IOException,
> WebApplicationException {
> return null; // won't be called (isReadable returns false)
> }
> }
>
> or you can always create JAXBContext by yourself and store it
> somewhere..
>
> Regards,
> Pavel
>
> On 5/20/11 3:54 AM, volker_at_dirtmine.com wrote:
> > Bump. I'm running into the same issue. Does anyone have a solution?
> I'm
> > on Jersey 1.5.
> >
> > Here's some of my relevant code:
> >
> > @Provider
> > @Produces(MediaType.APPLICATION_XML)
> > public class XmlWriter implements MessageBodyWriter {
> >
> > @Context
> > private Providers providers;
> >
> > @Override
> > public void writeTo( Object t, Class arg1, Type arg2,
> > Annotation[] arg3, MediaType arg4, MultivaluedMap arg5, OutputStream
> os
> > ){
> > ContextResolver<JAXBContext> cr =
> > providers.getContextResolver( JAXBContext.class,
> > MediaType.APPLICATION_XML_TYPE );
> >
> > // at this point cs is null :(
> > // .. etc etc etc
> > }
> > }
> >