如何在CXF Jaxrs中连接过滤器?

时间:2017-02-14 18:36:28

标签: java rest cxfrs

我想在客户来电之前和之后拦截请求。

假设:

Form formData = new Form();
    formData.param("grant_type", "client_credentials");

    List<Object> providers = new ArrayList();
    providers.add(new GsonMessageBodyProvider());
    providers.add(new RestLogger ()); <--doesn't work

    WebClient client = WebClient.create("https://blah.com", providers);

    // sets timeouts.
    HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
    conduit.getClient().setReceiveTimeout(1000);
    conduit.getClient().setConnectionTimeout(1000);

    client.path("token.oauth2");
    client.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).accept(MediaType.APPLICATION_JSON);

    WebClient wc = WebClient.fromClient(client);// thread safe
    BearerTokenResponse r = wc.post(formData, BearerTokenResponse.class);
    System.out.println(ToStringBuilder.reflectionToString(r));

我写了一个这样的课程,我觉得它会起作用:

    public class RestLogger implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        System.out.println("request");
    }
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        System.out.println("response");

    }
}

但它没有发射。有帮助吗?我在Storm集群中执行此操作,因此无法使用Annotations。我需要以编程方式挂钩。我使用3.1.10(最新)

    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-client -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.1.10</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

要拦截客户端上的请求和响应,请使用ClientRequestFilterClientResponseFilter

要拦截服务器端上的请求和响应,请使用ContainerRequestFilterContainerResponseFilter