How to make PATCH request using Jersey client?

627 Views Asked by At
WebTarget target = client.target(url);            client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND,true);
Response response = target
                    .request(MediaType.APPLICATION_JSON_PATCH_JSON)
                    .header("Authorization", "Bearer " + token)
                    .method("PATCH", Entity.json(JSONUtils.convertToJSON(List.of(startStopPayload))));

I tried making request like, But received an error This is the error:

jakarta.ws.rs.ProcessingException: Unable to make field private final sun.net.www.protocol.https.DelegateHttpsURLConnection sun.net.www.protocol.https.HttpsURLConnectionImpl.delegate accessible: module java.base does not "opens sun.net.www.protocol.https" to unnamed module @47f37ef1
        at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:309)
        at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:662)
        at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:697)
        at org.glassfish.jersey.client.JerseyInvocation.lambda$runInScope$3(JerseyInvocation.java:691)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
        at org.glassfish.jersey.client.JerseyInvocation.runInScope(JerseyInvocation.java:691)
        at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:661)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:439)
        at com.oracle.blockchain.opcp.restserver.client.k8s.K8sBlockchainPlatformInstanceClient.stopInstance(K8sBlockchainPlatformInstanceClient.java:183)
        at com.oracle.blockchain.opcp.restserver.client.k8s.K8sBlockchainPlatformInstanceClient$Proxy$_$$_WeldClientProxy.stopInstance(Unknown Source)
        at com.oracle.blockchain.opcp.restserver.manager.BPServiceManager.stopBlockchainPlatform(BPServiceManager.java:749)
        at com.oracle.blockchain.opcp.restserver.manager.BPServiceManager$Proxy$_$$_WeldSubclass.stopBlockchainPlatform(Unknown Source)
        at com.oracle.blockchain.opcp.restserver.manager.BPServiceManager$Proxy$_$$_WeldClientProxy.stopBlockchainPlatform(Unknown Source)
        at com.oracle.blockchain.opcp.restserver.resources.BlockchainPlatformResource.stopBlockchainPlatform(BlockchainPlatformResource.java:393)
        at com.oracle.blockchain.opcp.restserver.resources.BlockchainPlatformResource$Proxy$_$$_WeldClientProxy.stopBlockchainPlatform(Unknown Source)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:134)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:177)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:81)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:478)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:400)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81)
        at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684)
        at io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$4(JerseySupport.java:334)
        at io.helidon.common.context.Contexts.runInContext(Contexts.java:117)
        at io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$7(ContextAwareExecutorImpl.java:154)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)

I tried adding client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND,true);

I'm using jdk 17

2

There are 2 best solutions below

2
jan.supol On

Starting with JDK 16, it is no longer possible to use reflection on JDK internal classes and add PATCH method to a list of supported HTTP methods by the default HTTPUrlConnector (which is what client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND,true); tries to do.

Please select another connector from the list of supported connectors to support HTTP Patch method.

0
stanley On

Adding ...

.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)

... works for me. I believe the problem is that you first retrieve the target and only then you set the property.