简体   繁体   English

Jersey Client with Client Filter

[英]Jersey Client with Client Filter

I am using Jersey Client. 我正在使用Jersey客户端。

Client client = Client.create();
client.addFilter(new Myfilter());
// do some other things

And Myfilter Class 和Myfilter类

public class Myfilter extends ClientFilter {

    public ClientResponse handle(ClientRequest cr) {
         System.out.println("called");
         ClientResponse resp = getNext().handle(cr);
         return resp;
    }
}

I am not able to call handle method of Myfilter. 我无法调用Myfilter的句柄方法。 Can anybody help me out in this? 有人可以帮助我吗?

This Article explains how to add a filter for jersey client. 文章介绍了如何添加一个过滤器的球衣客户端。

Hope it helps 希望能帮助到你

PS : in case you forgot to add it to your web.xml PS:如果您忘记将其添加到您的web.xml

<init-param>
  <param-name>your.package.ClientFilter</param-name >
</init-param>

Example

@Override
    public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
        request.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, "application/json");
        return getNext().handle(request);
    }

Here is simple example (using jersey-client version 1.x) which results in printing "called" to console: 这是一个简单的例子(使用jersey-client版本1.x),导致打印“调用”到控制台:

import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.filter.ClientFilter;

public class JerseyClientFilterExample {
    public static void main(String[] args) {
        Client client = Client.create();
        client.addFilter(new MyClientFilter());
        client.resource("http://google.com").get(ClientResponse.class);
    }
}

class MyClientFilter extends ClientFilter {
    @Override
    public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
        System.out.println("called");
        return getNext().handle(cr);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM