Atul Kumar
Atul Kumar

Reputation: 749

How call PUT through Jersy REST client with null entity

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.

Upvotes: 10

Views: 16046

Answers (3)

Shashi
Shashi

Reputation: 11

You can use webTarget.request().put(Entity.json(""));
It works for me.

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 209052

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

Upvotes: 14

Peter Walser
Peter Walser

Reputation: 15706

I ran into the same error, and solved it by using an empty text entity:

Entity<?> empty = Entity.text("");
webTarget.request().put(empty);

Upvotes: 9

Related Questions