java 由于输入结束,jackson 无法将空数组映射到没有内容映射到对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11189882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:09:45  来源:igfitidea点击:

Hymanson failing to map empty array with No content to map to Object due to end of input

javajsonHymanson

提问by Paul Taylor

I send a query to an api and map the json results to my classes using Hymanson. When I get some results it works fine, but when there are no results it fails with

我向 api 发送查询并使用 Hymanson 将 json 结果映射到我的类。当我得到一些结果时它工作正常,但是当没有结果时它会失败

java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.Hymanson.map.ObjectMapper._initForReading(ObjectMapper.java:2766)
at org.codehaus.Hymanson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2709)
at org.codehaus.Hymanson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at com.jthink.discogs.query.DiscogsServerQuery.mapQuery(DiscogsServerQuery.java:382)
at com.jthink.discogs.query.SearchQuery.mapQuery(SearchQuery.java:37)*

But the thing is the api isn't returning nothing at all, so I dont see why it is failing.

但问题是 api 根本没有返回任何内容,所以我不明白它为什么失败。

Here is the query:

这是查询:

http://api.discogs.com/database/search?page=1&type=release&release_title=nude+and+rude+the+best+of+iggy+pop

this is what I get back

这就是我回来的

{
    "pagination": {
        "per_page": 50,
        "pages": 1,
        "page": 1,
        "urls": {},
        "items": 0
    },
    "results": []
}

and here is the top level object Im trying to map to

这是我试图映射到的顶级对象

public class Search
{
    private Pagination pagination;
    private Result[] results;

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public Result[] getResults() {
        return results;
    }

    public void setResults(Result[] results) {
        this.results = results;
    }
}

Im guessing the problem is something to do with the results array being returned being blank, but cant see what Im doing wrong

我猜这个问题与返回的结果数组为空有关,但看不出我做错了什么

EDIT: The comment below was correct, although I usually receive

编辑:下面的评论是正确的,虽然我通常会收到

{
    "pagination": {
        "per_page": 50,
        "pages": 1,
        "page": 1,
        "urls": {},
        "items": 0
    },
    "results": []
}

and in these cases there is no problem but sometimes I seem to just get an empty String. Now Im wondering if the problem is how I read from the inputstream

在这些情况下没有问题,但有时我似乎只是得到一个空字符串。现在我想知道问题是否是我如何从输入流中读取

 if (responseCode == HttpURLConnection.HTTP_OK)
        InputStreamReader in= new InputStreamReader(uc.getInputStream());
        BufferedReader    br= new BufferedReader(in);
        while(br.ready())
        {
            String next = br.readLine();
            sb.append(next);
        }
        return sb.toString();
  }

although I dont read until I get the response code, is it possible that the first time I call br.ready() that I call it before it is ready, and therefore I don't read the input

虽然我在得到响应代码之前不会阅读,但有可能我第一次调用 br.ready() 时我在它准备好之前调用它,因此我不阅读输入

EDIT 2:

编辑2:

Changing above code to simply

将上面的代码更改为简单

        String line;
        while ((line = br.readLine()) != null)
        {
            sb.append(line);
        }

resolved the issue.

解决了这个问题。

采纳答案by Paul Taylor

Changing above code to simply

将上面的代码更改为简单

        String line;
        while ((line = br.readLine()) != null)
        {
            sb.append(line);
        }

resolved the issue.

解决了这个问题。

回答by StaxMan

Although there is one solution already, I would suggest alternative where you first create JsonParserand then see if there is any input, and if so, use data bind. This is much faster than reading input line by line, constructing a String, and then handing it to parser.

虽然已经有一个解决方案,但我建议您首先创建替代方案,JsonParser然后查看是否有任何输入,如果有,请使用数据绑定。这比逐行读取输入,构造一个字符串,然后将其交给解析器要快得多。

JsonParser jp = jsonFactory.createJsonParser(uc.getInputStream());
Search result = null;
if (jp.nextToken() != null) { // end-of-input
  result = jp.readValueAs(Search.class);
}
jp.close();
return result;