Search code examples
patchjersey-client

how to use PATCH method with Jersey Invocation Builder?


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


Solution

  • 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)));