HTTP Patch with Jersey Client on JDK 16+

Jakarta REST provides a Client API, implemented by Jersey Client. The default implementation is based on the Java HttpUrlConnection. Unfortunately, the HttpUrlConnection supports only HTTP methods defined in the original HTTP/1.1 RFC 2616.

It will never support for instance HTTP Patch method. Jersey comes with a workaround. When Jersey is told so, it can try to use reflection to modify the list of known HTTP methods for the HttpUrlConnection. For instance:

ClientBuilder.newClient()
        .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)

From JDK 16 on, the reflection is not allowed with JDK classes. When tried, an exception is thrown:

java.lang.reflect.InaccessibleObjectException: Unable to make field protected java.lang.String java.net.HttpURLConnection.method accessible: module java.base does not "opens java.net" to unnamed module

Fortunately, there is another workaround for this workaround. It is possible to start the Java process with opening the Java modules to the customer application (in an unnamed module):

     --add-opens java.base/java.net=ALL-UNNAMED
     --add-opens java.base/sun.net.www.protocol.https=ALL-UNNAMED

The first line helps with HTTP requests, and the second is required in addition to the first one for HTTPS requests.

This entry was posted in Jersey. Bookmark the permalink.