5

When i invoke next code:

Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));

response.getEntity();

response.getEntity() is always null.

But when i invoke:

JsonObject json = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), JsonObject.class);

variable json is not null.

I need to use first variant because i need to check response status.

Why the first code is not working?And how can i get status code then?

user3245504
  • 85
  • 2
  • 11

1 Answers1

6

You need to use response.readEntity(Your.class) to return the instance of the type you want. For example

String rawJson = response.readEntity(String.class);
// or
JsonObject jsonObject = response.readEntity(JsonObject.class);

Note that there actually needs to be a provider to handle reading that Java type and application/json. If you are using the Jersey and the JSON-P API, see this. Also for general information about providers, see this

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720