Home » Java » Enterprise Java » Jibx Jersey2 Integration

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


one × 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close