Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

invocationBuilder 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.

How can I patch this?

Update

After some research, 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 I'll need to find an alternative library, method and code to call patch, but it needs to support OATH 2.0 also. Does such a library exist, and if so, are examples available?

I am using Java 1.8.0_131-b11.

like image 349
John Little Avatar asked Aug 28 '26 17:08

John Little


2 Answers

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)));
like image 196
John Little Avatar answered Aug 31 '26 17:08

John Little


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);
    };
}
like image 30
cellepo Avatar answered Aug 31 '26 17:08

cellepo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!