如何在Jersey 2.x中注册MessageBodyWriter和MessageBodyReader

时间:2017-03-28 21:52:51

标签: java xml jersey

我正在使用jersey api实现RESTful服务,并且我知道我需要注册自定义xmlWriter和xmlReader。客户端代码实现MessageBodyWriter,我需要知道ho0w在服务器端注册它,因为我得到MessageBodyProviderNotFoundException媒体类型application / xml。

MessageBodyWriter代码

public class SendDocumentsServiceRequestXMLWriter extends BaseMessageBodyWriter implements MessageBodyWriter<SendDocumentsRequest> {

public boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) {
    return type == SendDocumentsRequest.class && !mediaType.isWildcardType() 
            && !mediaType.isWildcardSubtype() && mediaType.isCompatible( MediaType.valueOf( "application/xml" ) );
}

public long getSize( SendDocumentsRequest t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) {
    return 0;
}

public void writeTo( SendDocumentsRequest t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
    MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, WebApplicationException {
    try {
        ESignatureClientJAXBContextFactory.getMarshaller( SendDocumentsRequest.class ).marshal( t, entityStream );
    } catch (Exception e) {
        throw new ESignatureClientException( e );
    }
}

}

我如何注册这个课程以便泽西捡起它?

由于

1 个答案:

答案 0 :(得分:2)

@Provider@Produces@Consumes一起放在您的实施类上,具体取决于Writer或Reader。

以下是一个例子:http://memorynotfound.com/jax-rs-messagebodywriter/

相关问题