配置JAX-RS 2.0客户端API的代理 [英] Configuring proxy for JAX-RS 2.0 client API
问题描述
我有一个在Java EE 7应用程序服务器(WildFly)上运行的应用程序,该应用程序使用REST资源查询另一个服务.
I have an application that is running on a Java EE 7 application server (WildFly), that queries another service using REST resources.
在以前的应用程序中,我使用了Jersey 1.x客户端API.通过Web代理授予对REST服务的访问权限.
In previous applications I have used the Jersey 1.x client API. Access to the REST service is granted through a web proxy.
在泽西岛,我这样创建Client
实例:
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;
}
在Java EE 7应用程序服务器上运行,我想使用应用程序服务器提供的JAX-RS 2.0客户端API.
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.
现在,我很难找到有关如何以独立于平台的方式配置JAX-RS 2.0客户端的信息.设置http.proxyHost
和http.proxyPort
系统属性在WildFly中无效(我还是希望不全局进行配置).
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?
推荐答案
我认为没有独立于供应商的解决方案(至少,我没有在JAX-RS API中找到与代理相关的任何东西).
I think there's no vendor independent solution (at least, I didn't find anything related to proxies in the JAX-RS API).
对于Jersey 2.x,您可以尝试:
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
是来自Jersey API的类.
ClientProperties
is a class from Jersey API.
对于RESTEasy,配置为:
For RESTEasy, the configuration is:
Client client = new ResteasyClientBuilder()
.defaultProxy("192.168.1.254", 8080, "http")
.build();
ResteasyClientBuilder
是RESTEasy API中的类.
ResteasyClientBuilder
is a class from RESTEasy API.
这篇关于配置JAX-RS 2.0客户端API的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!