2016-10-20 10 views
4

私は記事の本文のEntityを見て特定の統計を計算するはずのdropwizardを使用してRequestDispatcherを実装しようとしています。jersey requestdispatcher実行命令

ので、私は、私はすでに(理解)本文を読んでいるので、上記のコードは、例外をスローし、

private static class InspectRequestDispatcher implements RequestDispatcher { 

    private final RequestDispatcher dispatcher; 

    private InspectRequestDispatcher(RequestDispatcher dispatcher) { 
     this.dispatcher = dispatcher; 
    } 

    @Override 
    public void dispatch(final Object resource, final HttpContext context) { 
     final Saying day = context.getRequest().getEntity(Saying.class); 
     dispatcher.dispatch(resource, context); // this throws ConstraintViolationException 
    } 
} 

ResourceMethodDispatchAdapterを実施し、ResourceMethodDispatchProviderと私は正常に私のRequestDispatcherを注入して呼び出すことができています私はストリームをリセットすることができますが、私は体を2回読むためのペナルティを支払うことになります。

AFTERパラメータを注入したメソッド呼び出しをインターセプトすることは可能ですか?どういうわけかこのインターセプタを最後のものにスケジュールしますか?

あなたの代わりにRequestDispatcherContainerRequestFilterを使用した場合dropwizard 7バージョン

答えて

2

を使用すると、あなたはまさにこのためのものだCachedEntityContainerRequestの使用を作ることができます。

適応コンテナ要求から取得したエンティティインスタンスをキャッシュするキャッシュされたエンティティインバウンドHTTPリクエスト。

フィルタは、特定のタイプのエンティティを必要とし、同じタイプがリソースメソッドによっても利用される場合、このクラスを利用できます。

あなたは基本的にそうようにそれを使用したい:

@Provider 
public class StatsFilter implements ContainerRequestFilter { 
    @Override 
    public ContainerRequest filter(ContainerRequest request) { 
     final CachedEntityContainerRequest cachedRequest 
       = new CachedEntityContainerRequest(request); 

     final Saying saying = cachedRequest.getEntity(Saying.class); 

     return cachedRequest; 
    } 
} 

は、それからちょうどフィルタを登録します。

関連する問題