Jibx Jersey2集成

Jersey2为Jackson和JAXB提供内置支持。 但是默认情况下不支持Jibx。 要将Jibx与Jersey2结合使用,我们将XML输入作为流,并在接收到请求之后,使用Jibx对其进行解析。 但是实际上,有更好的方法可以使用MessageBodyReader和MessageBodyWriter API来实现相同目的。 这是可以实现的方式:

  1. 为使用Jibx的XML定义新的提供程序
  2. 向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;
	}

}

定义了此类后,请按照以下步骤在Jersey上注册:

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

}

完成此配置后,每当有新请求出现时,就会调用JibxXmlProvider的isReadable方法。 如果计算结果为true,则将调用readFrom进行对象转换。

希望这可以帮助!

翻译自: https://www.javacodegeeks.com/2014/04/jibx-jersey2-integration.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值