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.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


nine + 9 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books