VOOZH about

URL: https://www.javacodegeeks.com/2014/04/jibx-jersey2-integration.html

⇱ 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 ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Jersey
πŸ‘ Photo of Prasanth Gullapalli
Prasanth Gullapalli
April 28th, 2014Last Updated: April 27th, 2014
0 86 1 minute read

Prasanth Gullapalli

Prasanth is passionated about technology and specializes in application development in distributed environments. He has always been fascinated by the new technologies and emerging trends in software development.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz