Java com.fasterxml.jackson.databind.JsonMappingException:由于输入结束,没有要映射的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27104197/
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
com.fasterxml.Hymanson.databind.JsonMappingException: No content to map due to end-of-input
提问by Ram Dutt Shukla
I need to map a JSON array object with java POJO class. I wrote the code like this:
我需要用 java POJO 类映射一个 JSON 数组对象。我写了这样的代码:
// execute the client with get method
InputStream inputStream = getMethod.getResponseBodyAsStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
ObjectMapper objectMapper = new ObjectMapper();
JsonFactory jsonFactory = new JsonFactory();
List<OwnerDetail> owners = new ArrayList<>();
JsonParser jsonParser = jsonFactory.createJsonParser(inputStream);
if (jsonParser.nextToken() != null && jsonParser.)
{ // end-of-input
owners = objectMapper.readValue(bufferedReader, TypeFactory.defaultInstance().constructCollectionType(List.class, OwnerDetail.class));
}
The above block is giving me following error:
上面的块给了我以下错误:
com.fasterxml.Hymanson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.BufferedReader@5e66c5fc; line: 1, column: 1]
at com.fasterxml.Hymanson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.Hymanson.databind.ObjectMapper._initForReading(ObjectMapper.java:3029)
at com.fasterxml.Hymanson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2971)
at com.fasterxml.Hymanson.databind.ObjectMapper.readValue(ObjectMapper.java:2128)
Any help would be appreciated. Thanks.
任何帮助,将不胜感激。谢谢。
回答by DearDhruv
After reading response the data from the response is consumed. If your code is in interceptor you could try creating response again and return as below:
读取响应后,将消耗响应中的数据。如果您的代码在拦截器中,您可以尝试再次创建响应并返回如下:
Request request = chain.request();
Response originalResponse = chain.proceed(request);
final ResponseBody original = originalResponse.body();
// if(request.url().toString().equalsIgnoreCase(string)){
if (originalResponse.code() == HttpURLConnection.HTTP_OK) {
try {
String response = originalResponse.body().string();
JSONObject mainObject = new JSONObject(response);
// your mapping - manipulation code here.
originalResponse = originalResponse.newBuilder()
.header("Cache-Control", "max-age=60")
.body(ResponseBody.create(original.contentType(),
mainObject.toString().getBytes()))
.build();
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
return originalResponse;
Here response is created again and returned.
这里再次创建响应并返回。
Do let me know any update.
请让我知道任何更新。
回答by araqnid
The use of both inputStream andbufferedReader seems wrong. If you've made a JsonParser for the input, it is probably best to continue using it for the object mapping. There might be a problem with the buffered reader eagerly reading all the content from the stream, leaving nothing for the JSON parser to read.
inputStream和bufferedReader 的使用似乎是错误的。如果您已为输入创建了 JsonParser,则最好继续将其用于对象映射。缓冲读取器急切地从流中读取所有内容可能存在问题,而没有留下任何内容供 JSON 解析器读取。
There also looks to be a section missing from your code, in the if
condition.
在if
条件中,您的代码中似乎还缺少一个部分。
Something like this:
像这样的东西:
if (parser.isExpectedStartArrayToken()) {
owners = mapper.readValue(parser, mapper.getTypeFactory().constructCollectionType(List.class, OwnerDetail.class));
}
Extra notes:
额外说明:
- It's recommended notto wrap streams in buffering/decoding (InputStreamReader) before passing to Hymanson as Hymanson's parsers have heavily optimised this process already
- Actually, wrapping InputStreamReader in BufferedReader is practically always redundant as InputStreamReader does buffer content
- TypeFactory and JsonFactory can both be obtained from an ObjectMapper instance
- No need to initialise owners to an empty list that will get overridden anyway-- declare it without initialisation and use an
else
to set the default only when necessary
- 建议在传递给 Hymanson 之前不要将流包装在缓冲/解码 (InputStreamReader) 中,因为 Hymanson 的解析器已经对这个过程进行了大量优化
- 实际上,在 BufferedReader 中包装 InputStreamReader 实际上总是多余的,因为 InputStreamReader 会缓冲内容
- TypeFactory 和 JsonFactory 都可以从 ObjectMapper 实例中获取
- 无需将所有者初始化为一个无论如何都会被覆盖的空列表 - 声明它而不初始化并
else
仅在必要时使用 an来设置默认值
回答by bademba
I guess you should use OutputStreamWriter
to deserialize your response.
我想你应该OutputStreamWriter
用来反序列化你的响应。
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(auth.toString());
wr.flush();
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(auth.toString());
wr.flush();