2017-03-28 15 views
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); 
    } 
} 

}

ジャージーがそれを拾うように、どのように私はこのクラスを登録することができますか?

おかげ

答えて

1

は、ライターまたはReaderに応じて、@Producesまたは@Consumesと一緒にあなたの実装クラスに@Providerを置きます。

ここに例があります:http://memorynotfound.com/jax-rs-messagebodywriter/

+0

まだ例外があります。 MessageBodyWriterが見つかりません。 – Mike

+0

@Mikeどのようにプロジェクトを設定しましたか?コンテナや依存関係を意味し、web.xmlを使用してアプリケーションをセットアップするか、 'Application'をサブクラス化し、' @ApplicationPath'を使用しています。 – Ramanlfc

+0

私はすべてのセットアップを得た。 web.xmlには、ジャージーコンテナサーブレットとパッケージがあります。唯一のことは、MessageBodyWriterが見つからないことです。 – Mike

関連する問題