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
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With