Lately I came across the problem that I needed to call some REST resources from within an Jakarta EE 7 application running on a JBoss EAP 7.2 application server inside a company network. For this purpose proxies are used to permit access to specific URLs but block all other request to foreign servers due to security policies. However, the obvious ways to make JAX-RS use a proxy wouldn’t work.
Here a some of my approaches that didn’t work. It should be mentioned that JAX-RS is just a specification and during runtime an implementation is used based on the application server. JBoss and WildFly are using RESTEasy, another well-known implementations is Jersey.
- I tried to set environment variables for
HTTP_PROXY
andHTTPS_PROXY
on the server where the application was running. This approach worked for requests I issued viacurl
but the setting was ignored by the JVM and JAX-RS. - The RESTEasy documentation stated that it should be possible to define properties via the
javax.ws.rs.client.ClientBuilder
but again the proxy settings were ignored:ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder.property("org.jboss.resteasy.jaxrs.client.proxy.host", "hostname");
clientBuilder.property("org.jboss.resteasy.jaxrs.client.proxy.port", "8888";); - The RESTEasy implementation of the ClientBuilder for JAX-RS (
org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
) contains a function calleddefaultProxy(String hostname, int port)
which can only be called directly if there is a specific dependency on the RESTEasy library. This makes the application reliant to this implementation but I still tried it. But now dependency errors came up during runtime and the application wouldn’t even start successfully.
After many other trial-and-error cycles I stumbled across this question in the Red Hat Customer Portal which is hidden behind a membership barrier. It links to a JIRA bug tracker related to this issue. It seems that prior to JBoss EAP 7.3 or WildFly 18 the ClientBuilder
property was ignored and there is a need to use the defaultProxy
method.
Knowing that JBoss and WildFly use the RESTEasy implementation and without adding a dependeny on RESTEasy the solution is to use reflection to call the defaultProxy
method even though it is not part on the JAX-RS specification. Here’s the final solution:
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder =
(ClientBuilder) (clientBuilder.getClass().getMethod("defaultProxy", String.class, int.class))
.invoke(clientBuilder, "hostname", 8888);