Nicolas Henneaux Nicolas Henneaux - 1 month ago 21
Java Question

Use HTTP/2 with JAX-RS client

I am trying to works with a JAX-RS HTTP/2 server and client. The server side works but I cannot make the client working properly using JAX-RS abstractions. I use Jersey2 JAX-RS implementation and embedded Jetty HTTP server. I also use Jersey proxy client to use directly the API through an interface. The method to construct the client is below.

import org.glassfish.jersey.client.proxy.WebResourceFactory;
import javax.ws.rs.client.ClientBuilder;

(...)
<T> T getClient(int port, Class<T> resourceInterface) {
return WebResourceFactory.newResource(
resourceInterface,
ClientBuilder.newBuilder()
.build()
.target("https://localhost:" + port)
);
}


Do you have a solution using the same level of abstraction ?

EDIT

Following the answer from @sbordet, I have implemented a client connector supporting HTTP/2 based on JettyClient. Code is on GitHub.

Answer

Jersey allows to customize the transport using transport connectors.

There already is a Jersey client transport that uses Jetty's HttpClient, but it is configured to use HTTP/1.1, not HTTP/2, see https://github.com/jersey/jersey/blob/master/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java.

I guess that your option would be to either implement a Jersey ConnectorProvider and a Connector yourself, largely based on those already available, but configuring the HTTP/2 transport as explained in the Jetty documentation; or ask the Jersey developers to make the creation of the HttpClient instance in the existing JettyConnector overridable, so that you can customize the HttpClient transport with the HTTP/2 one.

Note that if you use HTTP/2 you will need support for ALPN as explained in this documentation.