java 如何解析 javax.ws.rs.core.Response

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27393098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:43:09  来源:igfitidea点击:

How to Parse javax.ws.rs.core.Response

javajakarta-eedomjax-rs

提问by Geoff Penny

I am having trouble understanding how to parse javax.ws.rs.core.Response. Some people have pointed to using an InputStream, but I am not understanding how that works since the return type of response.getEntity() is of type Object. For example:

我无法理解如何解析 javax.ws.rs.core.Response。有些人指出使用 InputStream,但我不明白它是如何工作的,因为 response.getEntity() 的返回类型是 Object 类型。例如:

Response response = client.target(enpoint).request(MediaType.APPLICATION_XML).get();
InputStream is = response.getEntity();

NetBeans complains and says I will need to cast type Object to InputStream. The response is going to consist of XML and I just want to be able to parse it with DOM. I am having trouble getting from javax.ws.rs.core.Response to anything useful.

NetBeans 抱怨并说我需要将类型 Object 强制转换为 InputStream。响应将由 XML 组成,我只想能够用 DOM 解析它。我无法从 javax.ws.rs.core.Response 获取任何有用的信息。

Any ideas?

有任何想法吗?

回答by Paul Samsotha

For JAX-RS 2.x Client API, use Response.readEntity(InputStream.class). Alternatively, is you don't need any specific information from the Responseobject, you can simple do

对于 JAX-RS 2.x 客户端 API,请使用Response.readEntity(InputStream.class). 或者,您是否不需要来自Response对象的任何特定信息,您可以简单地做

InputStream is = client.target(enpoint).request(
                            MediaType.APPLICATION_XML).get(InputStream.class);

回答by Justas

Also works:

也有效:

MyResponse myResponse = response.readEntity(MyResponse.class);