java – com.fasterxml.jackson.databind.JsonMappingException:由于输入结束没有要映射的内容

前端之家收集整理的这篇文章主要介绍了java – com.fasterxml.jackson.databind.JsonMappingException:由于输入结束没有要映射的内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用 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));
}

以上块给出了以下错误

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.BufferedReader@5e66c5fc; line: 1,column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3029)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2971)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2128)

任何帮助,将不胜感激.谢谢.

解决方法

读取响应后,将消耗响应中的数据.
如果您的代码拦截器,您可以尝试再次创建响应并返回如下:
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;

这里再次创建响应并返回.

请告诉我任何更新.

猜你在找的Java相关文章