JAX-RS response.getEntity returns null after message

When I call the following 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 call:

 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 the first option, because I need to check the status of the response.

Why does the first code not work? And how can I get a status code?

+5
source share
1 answer

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

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

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

+6
source

All Articles