user3245504
user3245504

Reputation: 85

JAX-RS response.getEntity returns null after post

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?

Upvotes: 5

Views: 8253

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209072

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

Upvotes: 6

Related Questions