第28章 インターセプタ


RESTEasy は JAX-RS 呼び出しを傍受し、インターセプタ と呼ばれるリスナと似たオブジェクトを介してルーティングすることができます。 サーバー側には 4 つの傍受ポイントがあります。
  • MessageBodyWriter 呼び出しのラッピング
  • MessageBodyReader 呼び出しのラッピング
  • アンマーシャリングの前に受信要求を傍受する プリプロセッサ を経由
  • JAX-RS メソッドの終了直後に呼び出される ポストプロセッサ を経由
MessageBodyReaderMessageBodyWriter、 クライアント側でのサーバーへのリモート呼び出しを傍受することもできます。

28.1. MessageBodyReader/Writer インターセプタ

MessageBodyReader インターセプタと MessageBodyWriter インターセプタは、 MessageBodyReader.readFrom()MessageBodyWriter.writeTo() の呼び出しをラッピングします。 これらは OutputInputStream をラッピングするために使用されます。 例えば、 GZIP エンコーディングが動作するよう、 RESTEasy の GZIP サポートには GzipOutputStreamGzipInputStream を用いてデフォルトの OutputInputStream を作成しオーバーライドするインターセプタが含まれています。 応答 (クライアント側では送信要求) にヘッダを追加するためインターセプタを使用することもできます。
インターセプタを使用するには、 org.jbos.resteasy.spi.interception.MessageBodyReaderInterceptorMessageBodyWriterInterceptor を実装します。
public interface MessageBodyReaderInterceptor
{
   Object read(MessageBodyReaderContext context) throws IOException, WebApplicationException;

}

public interface MessageBodyWriterInterceptor
{
   void write(MessageBodyWriterContext context) throws IOException, WebApplicationException;

}
Copy to Clipboard Toggle word wrap
インターセプタは MessageBodyWriterContextMessageBodyReaderContext によって操作されます。 インターセプタは Java コールスタックで一緒に呼び出されます。 後続のインターセプタを追加するには、 MessageBodyReaderContext.proceed() または MessageBodyWriterContext.proceed() を呼び出す必要があります。 呼び出すインターセプタがなくなったら、 MessageBodyReader または MessageBodyWriterreadFrom()writeTo() メソッドを呼び出します。 このラッピングにより、 リーダーやライターに達する前にオブジェクトを変更でき、 proceed() が返信する時にクリーンアップを行うことができます。 Context オブジェクトは、 リーダーやライターに送信されたパラメータを変更するメソッドも持っています。
public interface MessageBodyReaderContext
{
   Class getType();

   void setType(Class type);

   Type getGenericType();

   void setGenericType(Type genericType);

   Annotation[] getAnnotations();

   void setAnnotations(Annotation[] annotations);

   MediaType getMediaType();

   void setMediaType(MediaType mediaType);

   MultivaluedMap<String, String> getHeaders();

   InputStream getInputStream();

   void setInputStream(InputStream is);

   Object proceed() throws IOException, WebApplicationException;
}

public interface MessageBodyWriterContext
{
   Object getEntity();

   void setEntity(Object entity);

   Class getType();

   void setType(Class type);

   Type getGenericType();

   void setGenericType(Type genericType);

   Annotation[] getAnnotations();

   void setAnnotations(Annotation[] annotations);

   MediaType getMediaType();

   void setMediaType(MediaType mediaType);

   MultivaluedMap<String, Object> getHeaders();

   OutputStream getOutputStream();

   public void setOutputStream(OutputStream os);

   void proceed() throws IOException, WebApplicationException;
}
Copy to Clipboard Toggle word wrap
MessageBodyReaderInterceptorMessageBodyWriterInterceptor はサーバー側かクライアント側で使用することができます。 RESTeasy によって正しいインターセプタリストに追加されるよう、 @org.jboss.resteasy.annotations.interception.ServerInterceptor または @org.jboss.resteasy.annotations.interception.ClientInterceptor アノテーションを付けなければなりません。 これらのアノテーションの両方かいずれかがインターセプタクラスに付けられていないと、 デプロイメントエラーが発生します。 また、 インターセプタは次のように @Provider アノテーションが付けられていなければなりません。
@Provider
@ServerInterceptor
public class MyHeaderDecorator implements MessageBodyWriterInterceptor {

    public void write(MessageBodyWriterContext context) throws IOException, WebApplicationException
    {
       context.getHeaders().add("My-Header", "custom");
       context.proceed();
    }
}
Copy to Clipboard Toggle word wrap
これは、 応答にヘッダ値を追加するサーバー側のインターセプタになります。 @Provider@ServerInterceptor アノテーションが付けられています。 MessageBodyReader の実行後に応答がコミットされるため、 context.proceed() を呼び出す前にヘッダを変更する必要があります。

重要

context.proceed() を呼び出さないと呼び出しができません。
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat