简体   繁体   English

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

[英]How to Parse javax.ws.rs.core.Response

I am having trouble understanding how to parse javax.ws.rs.core.Response.我无法理解如何解析 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.有些人指出使用 InputStream,但我不明白它是如何工作的,因为 response.getEntity() 的返回类型是 Object 类型。 For example:例如:

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. NetBeans 抱怨并说我需要将类型 Object 强制转换为 InputStream。 The response is going to consist of XML and I just want to be able to parse it with DOM.响应将由 XML 组成,我只想能够用 DOM 解析它。 I am having trouble getting from javax.ws.rs.core.Response to anything useful.我无法从 javax.ws.rs.core.Response 获取任何有用的信息。

Any ideas?有任何想法吗?

For JAX-RS 2.x Client API, use Response.readEntity(InputStream.class) .对于 JAX-RS 2.x 客户端 API,请使用Response.readEntity(InputStream.class) Alternatively, is you don't need any specific information from the Response object, you can simple do或者,您是否不需要来自Response对象的任何特定信息,您可以简单地做

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

也有效:

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

InputStream responseBody = (InputStream) response.getEntity();

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

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