JAX-RS response.getEntity在发布后返回null [英] 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()始终为空.
但是当我调用时:
JsonObject json = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), JsonObject.class);
变量 json 不为空.
我需要使用第一个变体,因为我需要检查响应状态.
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?
推荐答案
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);
请注意,实际上需要提供程序来处理该Java类型和application/json的读取.如果您使用的是Jersey和JSON-P API,请参见这个.另外,有关提供商的一般信息,请参见此
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
这篇关于JAX-RS response.getEntity在发布后返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!