java 无法在响应输出中读取应用程序/json 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32823501/
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
Unable to read application/json message in Response output
提问by TDHM
I'm testing REST API and while I make GET call to retrieve resources, it's resulting into 500 Internal Server Error and in output it's returning message which has media type application/json
:
我正在测试 REST API,当我进行 GET 调用以检索资源时,它导致 500 Internal Server Error 并且在输出中它返回具有媒体类型的消息application/json
:
[
{
"messageType": "Some error type",
"messageText": "Some message text",
"moreInfo": "Some info"
}
]
Please make note that in above output, Json is inside []
请注意,在上面的输出中,Json 在里面 []
I want to read value of messageText
from above output response. I tried with -
我想messageText
从上面的输出响应中读取值。我试过 -
JsonObject jsonObject = response.readEntity(JsonObject.class);
but it results in following error:
但它导致以下错误:
java.lang.IllegalStateException: Entity input stream has already been closed.
at org.glassfish.jersey.message.internal.EntityInputStream.ensureNotClosed(EntityInputStream.java:225)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:830)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:783)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.InboundJaxrsResponse.call(InboundJaxrsResponse.java:111)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:399)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:108)
Could you please help me how can I read the message in output? I'm using Jersy library.
你能帮我如何阅读输出中的消息吗?我正在使用 Jersy 库。
回答by Tamara Aviv
I am writing automated tests for my Jersey services, which return XML objects, and I was getting this error occasionally too.
我正在为我的 Jersey 服务编写自动化测试,它返回 XML 对象,我偶尔也会收到这个错误。
According to javaDoc, a call to readEntity closes the response entity, so when you make another readEntity call you get IllegalStateException.
根据javaDoc,对readEntity的调用会关闭响应实体,因此当您进行另一个 readEntity 调用时,您会收到 IllegalStateException。
Unless the supplied entity type is an input stream, this method automatically closes the an unconsumed original response entity data stream if open.
除非提供的实体类型是输入流,否则此方法会在打开时自动关闭未使用的原始响应实体数据流。
In my case, having the expression response.readEntity(String.class)
in the Expressions pane in the Debug perspective caused this exception when I ran the code in Debug mode. The evaluation of the expression consumed the entity and caused it to close.
就我而言,response.readEntity(String.class)
当我在 Debug 模式下运行代码时,在 Debug 透视图中的 Expressions 窗格中的表达式会导致此异常。表达式的计算消耗了实体并导致它关闭。
回答by John Williams
I solved this by first doing the readEntity to a String entity and then using the Hymanson ObjectMapper to actually deserialize to the target class.
我通过首先对 String 实体执行 readEntity 然后使用 Hymanson ObjectMapper 实际反序列化到目标类来解决这个问题。
Problematic code:
有问题的代码:
Transactions transObj = response.readEntity(Transactions.class);
Solution:
解决方案:
String stringEntity = response.readEntity(String.class);
Transactions transObj = objectMapper.readValue(stringEntity, Transactions.class);
It seems this problem arises when the JSON string in the response entity stream is very long or complex possibly requiring multiple interactions thereon. 'Deserializing' to a string seems to only require a single bite. Once you have the string (and Hymanson or GSON) de-serialization to target entity takes place without touching the response.
当响应实体流中的 JSON 字符串很长或很复杂可能需要对其进行多次交互时,似乎会出现此问题。对字符串进行“反序列化”似乎只需要咬一口。一旦你有字符串(和 Hymanson 或 GSON)反序列化到目标实体就会在不触及响应的情况下发生。
回答by ashish gupta
Actually it is a issue with how are we defining the reference of Response object . Solution is weird
实际上这是我们如何定义 Response 对象的引用的问题。解决方法很奇怪
Not Working one :
不工作一:
Response response;
if (condition) {
response =
} else {
response =
}
String resp = response.readEntity(String.class);
if (response.getStatus() == 200) {}
Working One
工作一
Response response = null;
if (condition) {
response =
} else {
response =
}
String resp = response.readEntity(String.class);
if (response.getStatus() == 200) {}
So basically if we wont assign anything to response initially , Stream will be closed
所以基本上如果我们最初不给响应分配任何东西,Stream 将被关闭
回答by Aurasphere
When you get your error 500 you probably get another exception in the console/log. You should start checking that and then trying to resolve this one. If you don't find anything in the log, could you please post more code?
当您收到错误 500 时,您可能会在控制台/日志中收到另一个异常。你应该开始检查,然后尝试解决这个问题。如果您在日志中没有找到任何内容,能否请您发布更多代码?