2017-06-10 14 views
5

POST REST APIが使用されるフィルタがあり、フィルタ内のペイロードの下の部分を抽出したい。フィルタメソッドでContainerRequestContextを使用してREST APIのペイロードを取得する方法

{ 
     "foo": "bar", 
     "hello": "world" 
} 

フィルターコード: -

public class PostContextFilter implements ContainerRequestFilter { 
    @Override 
    public void filter(ContainerRequestContext requestContext) 
      throws IOException { 
     String transactionId = requestContext.getHeaderString("id"); 
    // Here how to get the key value corresponding to the foo. 
     String fooKeyVal = requestContext. ?? 
    } 
} 

私はContainerRequestContextオブジェクトを使用してAPIにペイロードを取得する任意の簡単な方法が表示されません。

私の質問は、私のペイロードのfooキーに対応するキー値を取得する方法です。

答えて

4

フィルタは主に、HTTPヘッダー、URI、HTTPメソッドなどの要求および応答パラメータを操作することを目的としていますが、インターセプタはエンティティの入出力ストリームを操作してエンティティを操作することを意図しています。

ReaderInterceptorあなたはストリームが「ワイヤ」からを来て、つまり、インバウンドエンティティ・ストリームを操作することができます。このanswerこのanswerもあなたの質問に関連している可能性が

@Provider 
public class CustomReaderInterceptor implements ReaderInterceptor { 

    // Create a Jackson ObjectMapper instance (it can be injected instead) 
    private ObjectMapper mapper = new ObjectMapper(); 

    @Override 
    public Object aroundReadFrom(ReaderInterceptorContext context) 
         throws IOException, WebApplicationException { 

     // Parse the request entity into the Jackson tree model 
     JsonNode tree = mapper.readTree(context.getInputStream()); 

     // Extract the values you need from the tree 

     // Proceed to the next interceptor in the chain 
     return context.proceed(); 
    } 
} 

:インバウンドエンティティの流れを解析するためにジャクソンを使用して、あなたのインターセプタは次のようである可能性があります。

関連する問題