Search code examples
javahttp3

What changes should be made in Java code in order to migrate to HTTP/3


New HTTP protocol version was proposed in 2022. What changes in our Java code should we made in order to make sure the code is compatible with the new version?

For example request code

var response = HttpClient.newHttpClient()
        .send(HttpRequest.newBuilder(URI.create("https://example.com"))
                        .GET()
                        .build(),
                HttpResponse.BodyHandlers.ofString());

if (response.statusCode() == HttpURLConnection.HTTP_OK) {
    System.out.println(response.body());
}

In java.net.http.HttpClient.Version I see only HTTP_1_1 and HTTP_2, no HTTP_3 in OpenJDK 20, which was released in 2023. HTTP 2 is the default version used by HttpClient and I don't know if they plan to include support for HTTP/3?

Or response code

@GetMapping("/")
public String getHelloWorld() {
    return "Hello World!";
}

From what I understand the recent HTTP/3 changes are made in L3/L4 and my Java code sits in L7 so do I have to change anything in my code after all?

Browsers

The browsers have already caught up in 2023, about 40% of requests are made using HTTP/3 on Chrome desktop and mobile and less than 10% using HTTP1.1 so it is becoming increasingly important to be aware of the changes that have to be changed in the Java code, if any. I'm not sure the changes should be made in JDK itself or is it on the side of the programmer or if it is obvious right now.


Solution

  • Updates to the JDK libraries for new features typically take a long time, as the JDK development process is very cautious (and rightfully so).

    In particular, HTTP/3 is quite different from HTTP/1.1 and HTTP/2 because it is not based on TCP nor TLS, and therefore requires a lot of work to be implemented from zero (in particular, a full QUIC implementation with TLS message layering, flow control and congestion control -- it is a lot of work, and the HTTP/3 on top).

    This OpenJDK issue tracks exactly what you are asking, and it has been filed in 2019, now almost 5 years ago, and still no progress (just to set expectations).

    Your choices are:

    • Forget HTTP/3, and continue using HTTP/1.1 and/or HTTP/2.
    • Use another HTTP client library that supports HTTP/3.

    [Disclaimer, I have implemented HTTP/3 in the Jetty HTTP client].
    The Jetty project provides the Jetty HTTP client, that can be configured to use HTTP/3, see here.

    The Jetty project also provides a low-level HTTP/3 client, but this is typically only used by those applications that want to deal with low-level HTTP/3 protocol details (quite a rare case).

    Other open source projects provide HTTP clients that support HTTP/3, so you have choices if you really want to use HTTP/3, just not with the OpenJDK HTTP client.

    However, bear in mind that HTTP/3 has been designed mostly for browsers and mobile, so if you are using an HTTP client in your application chances are that it will not perform much better than HTTP/1.1 or HTTP/2.

    Bottom line, if you use the OpenJDK HTTP client, you are stuck with the OpenJDK progress, which may happen very slowly or not even happen at all.

    If you use some other library, you are stuck with that library, but chances are that the library will be updated way faster than OpenJDK, and provide in the future also future versions of HTTP.