简体   繁体   English

JAX-RS response.getEntity在post之后返​​回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() is always null. response.getEntity()始终为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. 变量json不为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? 为什么第一个代码不起作用?我怎样才能获得状态代码呢?

You need to use response.readEntity(Your.class) to return the instance of the type you want. 您需要使用response.readEntity(Your.class)来返回所需类型的实例。 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. 请注意,实际上需要一个提供程序来处理读取Java类型和application / json。 If you are using the Jersey and the JSON-P API, see this . 如果您使用的是Jersey和JSON-P API,请参阅此内容 Also for general information about providers, see this 有关提供商的一般信息,请参阅此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM