10

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.

Atul Kumar
  • 658
  • 2
  • 6
  • 27

3 Answers3

13

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));
Community
  • 1
  • 1
Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
9

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

Entity<?> empty = Entity.text("");
webTarget.request().put(empty);
Peter Walser
  • 13,460
  • 4
  • 48
  • 68
  • 1
    I personally think this is the cleaner solution than disabling the validation. Who knows what other things are then not checked anymore... – Lonzak May 27 '20 at 09:57
1

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

Badro Niaimi
  • 1,027
  • 1
  • 12
  • 23
Shashi
  • 11
  • 1