Configuring proxy for JAX-RS 2.0 client API

╄→гoц情女王★ 提交于 2019-11-30 17:50:15

问题


I have an application that is running on a Java EE 7 application server (WildFly), that queries another service using REST resources.

In previous applications I have used the Jersey 1.x client API. Access to the REST service is granted through a web proxy.

In Jersey I create the Client instance like this:

public Client create() {

    Client client;
    if ( proxyConfiguration != null && proxyConfiguration.getHost() != null && !proxyConfiguration.getHost().trim().isEmpty() ) {
        HttpURLConnectionFactory urlConnectionFactory = new ProxyUrlConnectionFactory( proxyConfiguration );
        client = new Client( new URLConnectionClientHandler( urlConnectionFactory ), clientConfig );
    } else {
        client = Client.create( clientConfig );
    }

    return client;
}

Running on a Java EE 7 application server I wanted to use the JAX-RS 2.0 client API which is provided by the application server.

Now I am having a really hard time to find information on how to configure the JAX-RS 2.0 client in a platform independent way. Setting the http.proxyHost and http.proxyPort system properties had no effect in WildFly (I would prefer to not configure it globally anyway).

Does anyone know how to solve this?


回答1:


I think there's no vendor independent solution (at least, I didn't find anything related to proxies in the JAX-RS API).

For Jersey 2.x, you can try:

ClientConfig config = new ClientConfig();
config.property(ClientProperties.PROXY_URI, "192.168.1.254:8080");  
Client client = ClientBuilder.withConfig(config).build();

ClientProperties is a class from Jersey API.


For RESTEasy, the configuration is:

Client client = new ResteasyClientBuilder()
                   .defaultProxy("192.168.1.254", 8080, "http")
                   .build();

ResteasyClientBuilder is a class from RESTEasy API.



来源:https://stackoverflow.com/questions/33872492/configuring-proxy-for-jax-rs-2-0-client-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!