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

How can I hook up a filter in CXF Jaxrs?

我想在客户端调用之前和之后拦截请求。

鉴于:

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));

我写了一个像这样的 class 我认为可行的:

    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 集群中执行此操作,因此无法使用注释。我需要以编程方式将其连接起来。我正在使用 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>

要在客户端拦截请求和响应,请使用ClientRequestFilter and ClientResponseFilter

要在服务器端拦截请求和响应,请使用ContainerRequestFilter and ContainerResponseFilter