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
> }
> }
>