Java 从发布请求读取 JAX-RS 客户端中的响应正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18086621/
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
Read response body in JAX-RS client from a post request
提问by user2657714
Having some sort of proxy between a mobile app and a web-service, we are puzzled by the response when issuing a post request. We receive response with status 200: OK. But we can not find/extract the JSON response body.
在移动应用程序和网络服务之间有某种代理,我们对发出 post 请求时的响应感到困惑。我们收到状态为 200 的响应:OK。但是我们无法找到/提取 JSON 响应正文。
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(WEBSERVICE_BASE_LOCATION + "mobileDevices?operatorCode=KPNSCP");
String jsonString = "{\"osVersion\":\"4.1\",\"apiLevel\":16,\"devicePlatform\":\"ANDROID\"}";
Builder builder = webTarget.request();
Response response = builder.post(Entity.json(jsonString));
We are using JAX-RS.
Can someone please provide some hints to extract the JSON body (String
) from the server response?
我们正在使用 JAX-RS。有人可以提供一些提示以String
从服务器响应中提取 JSON 正文 ( ) 吗?
采纳答案by Juned Ahsan
Try this:
尝试这个:
String output = response.getEntity(String.class);
EDIT
编辑
Thanks to @Martin Spamerto mention that it will work for Jersey 1.x jars only. For Jersey 2.x use
感谢@Martin Spamer提到它仅适用于 Jersey 1.x 罐子。对于 Jersey 2.x 使用
String output = response.readEntity(String.class);
回答by col.panic
I just found a solution for jaxrs-ri-2.16 - simply use
我刚刚找到了 jaxrs-ri-2.16 的解决方案 - 只需使用
String output = response.readEntity(String.class)
String output = response.readEntity(String.class)
this delivers the content as expected.
这将按预期交付内容。
回答by entpnerd
For my use case, none of the previous answers worked because I was writing a server-side unit test which was failing due the following error message as described in the Unable to Mock Glassfish Jersey Client Response Objectquestion:
对于我的用例,以前的答案都不起作用,因为我正在编写服务器端单元测试,该测试由于以下错误消息而失败,如无法模拟 Glassfish Jersey 客户端响应对象问题中所述:
java.lang.IllegalStateException: Method not supported on an outbound message.
at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:145)
at ...
This exception occurred on the following line of code:
此异常发生在以下代码行上:
String actJsonBody = actResponse.readEntity(String.class);
The fix was to turn the problem line of code into:
解决方法是将问题代码行变成:
String actJsonBody = (String) actResponse.getEntity();
回答by Cheefachi
I also had the same issue, trying to run a unit test calling code that uses readEntity
. Can't use getEntity
in production code because that just returns a ByteInputStream
and not the content of the body and there is no way I am adding production code that is hit only in unit tests.
我也遇到了同样的问题,试图运行一个单元测试调用使用readEntity
. 不能getEntity
在生产代码中使用,因为ByteInputStream
它只返回 a而不是正文的内容,我无法添加仅在单元测试中命中的生产代码。
My solution was to create a response and then use a Mockito spy to mock out the readEntity
method:
我的解决方案是创建一个响应,然后使用 Mockito 间谍来模拟该readEntity
方法:
Response error = Response.serverError().build();
Response mockResponse = spy(error);
doReturn("{jsonbody}").when(mockResponse).readEntity(String.class);
Note that you can't use the when(mockResponse.readEntity(String.class)
option because that throws the same IllegalStateException
.
请注意,您不能使用该when(mockResponse.readEntity(String.class)
选项,因为它会抛出相同的IllegalStateException
.
Hope this helps!
希望这可以帮助!
回答by MonkeySoft Colombia
Acording with the documentation, the method getEntity in Jax rs 2.0 return a InputStream. If you need to convert to InputStream to String with JSON format, you need to cast the two formats. For example in my case, I implemented the next method:
根据文档,Jax rs 2.0 中的 getEntity 方法返回一个 InputStream。如果需要将 InputStream 转换为 JSON 格式的 String,则需要对这两种格式进行转换。例如,在我的情况下,我实现了下一个方法:
private String processResponse(Response response) {
if (response.getEntity() != null) {
try {
InputStream salida = (InputStream) response.getEntity();
StringWriter writer = new StringWriter();
IOUtils.copy(salida, writer, "UTF-8");
return writer.toString();
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
return null;
}
why I implemented this method. Because a read in differets blogs that many developers they have the same problem whit the version in jaxrs using the next methods
为什么我实现了这个方法。因为在不同的博客中阅读了许多开发人员,他们在使用下一个方法的 jaxrs 版本中遇到了相同的问题
String output = response.readEntity(String.class)
and
和
String output = response.getEntity(String.class)
The first works using jersey-client from com.sun.jersey library and the second found using the jersey-client from org.glassfish.jersey.core.
第一个使用来自 com.sun.jersey 库的 jersey-client,第二个使用来自 org.glassfish.jersey.core 的 jersey-client。
This is the error that was being presented to me: org.glassfish.jersey.client.internal.HttpUrlConnector$2 cannot be cast to java.lang.String
这是向我呈现的错误:org.glassfish.jersey.client.internal.HttpUrlConnector$2 cannot be cast to java.lang.String
I use the following maven dependency:
我使用以下 Maven 依赖项:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.28</version>
What I do not know is why the readEntity method does not work.I hope you can use the solution.
我不知道的是为什么readEntity方法不起作用。希望您可以使用该解决方案。
Carlos Cepeda
卡洛斯·塞佩达
回答by MonkeySoft Colombia
Realizing the revision of the code I found the cause of why the reading method did not work for me. The problem was that one of the dependencies that my project used jersey 1.x. Update the version, adjust the client and it works.
意识到代码的修订,我找到了阅读方法对我不起作用的原因。问题是我的项目使用 jersey 1.x 的依赖项之一。更新版本,调整客户端就可以了。
I use the following maven dependency:
我使用以下 Maven 依赖项:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.28</version>
Regards
问候
Carlos Cepeda
卡洛斯·塞佩达