Using JiBX with Jersey
After searching around for this for a while without much luck, I post it here hoping to save you some time. Jersey, the reference implementation of JAX-RS, typically uses JAXB to marshal and unmarshal XML. If you want to use JiBX instead, you should provide custom providers:
@Provider
public class JIBXBodyReader implements MessageBodyReader<Object> {
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
try {
BindingDirectory.getFactory( type );
} catch (JiBXException e) {
return false;
}
return true;
}
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
try {
IBindingFactory factory = BindingDirectory.getFactory( type );
IUnmarshallingContext context = factory.createUnmarshallingContext();
return context.unmarshalDocument( entityStream, null );
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@Provider
public class JIBXBodyWriter implements MessageBodyWriter<Object> {
public long getSize(Object obj, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType ) {
return -1;
}
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType ) {
try {
BindingDirectory.getFactory( type );
} catch (JiBXException e) {
return false;
}
return true;
}
public void writeTo(Object obj, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> headers, OutputStream outputStream)
throws IOException, WebApplicationException {
try {
IBindingFactory factory = BindingDirectory.getFactory( type );
IMarshallingContext context = factory.createMarshallingContext();
context.marshalDocument( obj, "UTF-8", null, outputStream );
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}
No comments yet.
Leave a Reply
-
Recent
- Call of Cthulhu character generator for Android
- Using JiBX with Jersey
- Preventing session expiration with AJAX
- Migrating from VirtualBox to VMWare in Linux
- Fully disabling touchpad in Ubuntu 9.10 Karmic Koala
- Fixing sound after upgrading to Ubuntu 9.10 Karmic Koala
- Dealing with mouse and touchpad freezes in Linux
- Selecting language of multilingual web sites
- 4 + 1 ways to celebrate the Software Freedom Day
- OLPC Deployment in the Greek village of Sminthi
- Building and deploying Seam/JBoss applications with Intellij IDEA 8.1
- Free Software in Education
-
Links
-
Archives
- August 2011 (1)
- November 2010 (1)
- June 2010 (1)
- February 2010 (1)
- January 2010 (1)
- December 2009 (1)
- September 2009 (3)
- August 2009 (2)
- May 2009 (2)
-
Categories
-
RSS
Entries RSS
Comments RSS

