JAX-RS response.getEntity returns null after post

心不动则不痛 提交于 2019-12-23 11:53:16

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/32104388/jax-rs-response-getentity-returns-null-after-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!