Free Software

Software Engineering with FOSS and Linux

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();
				}				
	}
}

November 25, 2010 - Posted by | Programming, Uncategorized, web | , , , , , , , ,

1 Comment »

  1. That is very interesting, You are a very skilled blogger.
    I’ve joined your rss feed and look forward to searching for more of your wonderful post.
    Additionally, I have shared your web site
    in my social networks

    Comment by Bee Control Products Vanderbijlpark | April 30, 2014 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: