How call PUT through Jersy REST client with null entity

Atul Kumar picture Atul Kumar · Jan 13, 2016 · Viewed 11k times · Source

I want to call some "upgrade" REST API through Jersy client, which is declared as PUT and does not require any body content.

But when I request this API as below:

webTarget.request().put(Entity.json(null));

It gives error as below:

Entity must not be null for http method PUT.

So need to know, is there any way in jersy client to call PUT method with null Entity.

Answer

Paul Samsotha picture Paul Samsotha · Jan 13, 2016

You can configure the client property

SUPPRESS_HTTP_COMPLIANCE_VALIDATION

By default, Jersey client runtime performs certain HTTP compliance checks (such as which HTTP methods can facilitate non-empty request entities etc.) in order to fail fast with an exception when user tries to establish a communication non-compliant with HTTP specification. Users who need to override these compliance checks and avoid the exceptions being thrown by Jersey client runtime for some reason, can set this property to true. As a result, the compliance issues will be merely reported in a log and no exceptions will be thrown.

[...]

Client client = ClientBuilder.newClient();
client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
WebTarget target = client.target("...");
Response response = target.request().put(Entity.json(null));