webResource.get(String.class) 返回 null

问题描述 投票:0回答:2

我创建了一个 Jersey 客户端程序来使用返回 XML 的 REST Web 服务。

Client client = Client.create();
WebResource webResource = client.resource("http://xyz:abc/myRestservice/getval");

我使用 webResource get 方法将返回值存储在字符串变量中:

String s = webResource.get(String.class);

我没有收到任何错误。但变量“s”显示 null 作为输出:

System.out.println("- "+s);

输出:

- 
Process exited with exit code 0.

当我在本地测试相同的 Web 服务时(使用没有客户端程序的 JDeveloper IDE),它会返回值。


更新:

我发现变量“s”显示为 null,因为 Web 服务程序中出现异常(如下所述)。

Web 服务程序使用不透明 (OracleTypes.OPAQUE) 变量来存储从 ORACLE 数据库中的存储函数检索到的 XMLTYPE 值。那么不透明变量是 使用强制转换分配给新的 XMLType。这在 JDeveloper IDE 内部 weblogic 服务器中进行测试时以某种方式起作用。 但当我在远程 weblogic 服务器中部署此 Web 服务并尝试使用客户端程序使用时,它不起作用。 我收到一个异常 - “oracle.sql.OPAQUE 无法转换为 oracle.xdb.XMLType”。

我猜很可能是由于远程 weblogic 服务器中缺少一些 Jar,但不确定是哪个 jar。

java web-services rest jersey webresource
2个回答
1
投票

你就快到了。您真正应该做的是获取 ClientResponse.class 而不是 String.class。

这是一个示例:

ClientResponse response = webResource.get(ClientResponse.class);
String responseString = response.getEntity(String.class);

一旦您开始使用此球衣休息客户端,您就会对每个 REST api 合约了解更多,您还必须将“requestType”和“responseAccept”设置为 webResource 的一部分。如果没有这个,您最终可能还会遇到一些其他奇怪的错误。

这是另一个示例:

ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
String responseStringXML = response.getEntity(String.class);

地点:

请求类型(REST API 在正文中期望的数据格式)是:“application/json”并且 响应类型(REST API返回的数据格式)是:“application/xml”

请注意,我使用了

POST
(.post 方法)而不是 get,因为在正常的
GET
请求中,不需要正文(因此不需要请求类型)。
POST
示例仅供参考。对于
GET
,您仍然需要一个响应类型,类似于:

ClientResponse response = webResource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
String responseStringXML = response.getEntity(String.class);

希望有帮助!


0
投票

什么是网络资源。我被收取 14.95 美元的费用......2023 年 1 月 3 日。再一次 2023 年 1 月 6 日。我不知道这是什么。 因此,请告诉我我被收取的费用。谢谢 [电子邮件受保护]。 2024 年 1 月 6 日 我不记得了,所以如果我不知道这是什么,请立即取消。

© www.soinside.com 2019 - 2024. All rights reserved.