JAX-RS response.getEntity在发布后返回null [英] JAX-RS response.getEntity returns null after post

查看:146
本文介绍了JAX-RS response.getEntity在发布后返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用下一个代码时:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆