1
jersey
apiを使用してRESTfulサービスを実装しています。私はカスタムxmlWriterとxmlReaderを登録する必要があることを知りました。クライアントコードはMessageBodyWriterを実装しており、メディアタイプapplication/xmlのためにMessageBodyProviderNotFoundException
を取得しているので、サーバ側で登録するにはho0wを知る必要があります。Jersey 2.xでMessageBodyWriterとMessageBodyReaderを登録する方法
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);
}
}
}
ジャージーがそれを拾うように、どのように私はこのクラスを登録することができますか?
おかげ
まだ例外があります。 MessageBodyWriterが見つかりません。 – Mike
@Mikeどのようにプロジェクトを設定しましたか?コンテナや依存関係を意味し、web.xmlを使用してアプリケーションをセットアップするか、 'Application'をサブクラス化し、' @ApplicationPath'を使用しています。 – Ramanlfc
私はすべてのセットアップを得た。 web.xmlには、ジャージーコンテナサーブレットとパッケージがあります。唯一のことは、MessageBodyWriterが見つからないことです。 – Mike