Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register MessageBodyWriter and MessageBodyReader in Jersey 2.x

Tags:

java

xml

jersey

I am implementing RESTful services using jersey api and I've come know that I need to register the custom xmlWriter and xmlReader. Client code implements MessageBodyWriter and I need to know ho0w to register it in server side because I am getting MessageBodyProviderNotFoundException for media type application/xml.

MessageBodyWriter code

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

}

How can I register this class so that Jersey picks it up?

Thanks

like image 820
Mike Avatar asked Oct 18 '22 15:10

Mike


1 Answers

put @Provider on your implementation class along with @Produces or @Consumes depending on Writer or Reader.

Here's a example:http://memorynotfound.com/jax-rs-messagebodywriter/

like image 86
Ramanlfc Avatar answered Oct 21 '22 04:10

Ramanlfc