6
WebTarget webTarget = httpClient.target(url);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON)
                .header(HttpUtils.AUTHORISATION_HEADER_NAME, "Bearer " + theAccessToken);
response = invocationBuilder.put(Entity.json(objectMapper.writeValueAsString(payload)));

httpClient is of type javax.ws.rs.client.Client and gets injected.

invocatioBuilder implements javax.ws.rs.client.Invocation.Builder, but is defined in package org.glassfish.jersey.client

invocationBuilder.put, invocationBuilder.post, invocationBuilder.get all exist and work, but here is no invocationBuilder.patch - it's missing.

Any suggestions on how to patch?

==== UPDATE ====

After some googling, it seems that jersey client has no support for patch. As all our apps API calls are made using jersey client, this is a bit of a problem. I assume Ill need to find an alternative library, method and code to call patch, but it needs to support OATH 2.0 also. Any ideas if such a library exists, and, ideally has some examples?

FYI, using Java 1.8.0_131-b11

John Little
  • 10,707
  • 19
  • 86
  • 158
  • `method("PATCH")` – Paul Samsotha Apr 21 '19 at 06:36
  • Hi, in my IDE, I only have post, get and put, no patch. – John Little Apr 21 '19 at 06:51
  • 2
    [`method()`](https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/SyncInvoker.html#method-java.lang.String-). https://stackoverflow.com/a/26341128/2587435 – Paul Samsotha Apr 21 '19 at 09:53
  • 1
    Nice, I didnt know method() was a direct substitue for put(), post() etc. I tried it, and now get "javax.ws.rs.ProcessingException: java.net.ProtocolException: Invalid HTTP method: PATCH" on the line with "response = invocationBuilder.method("PATCH", Entity.json(...)". Googling this error, some suggest "Client jerseyClient = ClientBuilder.newClient() .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)" but I dont have a Client object, only a WebTarget and a Invocation.Builder object. – John Little Apr 21 '19 at 10:48

2 Answers2

13

Thanks to @Paul Samsotha, the working solution is this:

WebTarget webTarget = httpClient.target(url);
webTarget.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);

Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON)
                .header(HttpUtils.AUTHORISATION_HEADER_NAME, "Bearer " + theAccessToken);
response = invocationBuilder.method(HttpMethod.PATCH, Entity.json(objectMapper.writeValueAsString(payload)));
Rajat
  • 2,467
  • 2
  • 29
  • 38
John Little
  • 10,707
  • 19
  • 86
  • 158
0

I found @Paul Samsotha, @John Little Accepted Answer to be correct.

If you don't find javax.ws.rs.client.Client to be directly injectable, a Bean method similar to this (if you have a Bean that extends extends Consumer<ClientBuilder>) may further help to access it:

import java.util.function.Consumer;

import javax.ws.rs.client.ClientBuilder;

@Bean
public Consumer<ClientBuilder> patchOverrideEnhancer() {
    return (client) -> {
        client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
    };
}
cellepo
  • 4,001
  • 2
  • 38
  • 57