About Prasanth Gullapalli

Prasanth is a Principal Engineer with more than 7 yrs experience in JEE architecture. He has always been fascinated by the new technologies and emerging trends in software development.

Jibx Jersey2 Integration

Jersey2 provides inbuilt support for Jackson and JAXB. But Jibx is not supported by default. To use Jibx in conjunction with Jersey2, we are taking the XML input as a stream and after receiving the request, we are parsing it using Jibx. But there is actually a better way of achieving the same using MessageBodyReader and MessageBodyWriter APIs. Here is how this can be achieved:
 
 
 
 
 
 

  1. Define a new provider for consuming XML which uses Jibx
  2. Register it with Jersey ResourceConfig
@Provider
public class JibxXmlProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object>  {

	public boolean isReadable(Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType) {     

		if(!MediaType.APPLICATION_XML_TYPE.equals(mediaType)){
			return false;
		}

		try {
			BindingDirectory.getFactory( type );
		} catch (JiBXException e) {
			return false;
		}
		return true;
	}
	
	public boolean isWriteable(Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType ) {      
		
		if(!MediaType.APPLICATION_XML_TYPE.equals(mediaType)){
			return false;
		}
		
		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;
	}
	
	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();
		}              
	}
	
	public long getSize(Object obj, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType ) {
		return -1;
	}

}

Once this class is defined, register it with Jersey as follows:

public class JerseyResourceInitializer extends ResourceConfig {

    public JerseyResourceInitializer() {
        packages(true, "com.adaequare.processing.service");
		
		// This line registers JibxXmlProvider as a new provider.
        register(JibxXmlProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
    }

}

After this configuration, when ever a new request comes, isReadable method of JibxXmlProvider is called. If it evaluates to true, then readFrom is called for object conversion.

Hope this helps!

Reference: Jibx Jersey2 Integration from our JCG partner Prasanth Gullapalli at the prasanthnath blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


6 × = six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close