Java 由于输入结束杰克逊解析器,没有要映射的内容

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

No content to map due to end-of-input Hymanson parser

javaandroidjsonHymanson

提问by Swapnil

I am getting this response from the server {"status":"true","msg":"success"}

我从服务器收到此响应 {"status":"true","msg":"success"}

I am trying to parse this json string using Hymanson parser library but somehow I am facing mapping-exception stating

我正在尝试使用 Hymanson 解析器库解析这个 json 字符串,但不知何故我正面临映射异常说明

com.fasterxml.Hymanson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: java.io.StringReader@421ea4c0; line: 1, column: 1]

Why do we get this kind of exceptions?

为什么我们会遇到这种异常?

How to understand what is causing this exception?

如何理解导致此异常的原因?

I am trying to parse using following way:

我正在尝试使用以下方式解析:

StatusResponses loginValidator = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);

try {
    String res = result.getResponseAsString();//{"status":"true","msg":"success"}
    loginValidator = objectMapper.readValue(result.getResponseAsString(), StatusResponses.class);
} catch (Exception e) {
    e.printStackTrace();
}

StatusResponse class

状态响应类

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status","msg" })
public class StatusResponses {

    @JsonProperty("status")
    public String getStatus() {
        return status;
    }

    @JsonProperty("status")
    public void setStatus(String status) {
        this.status = status;
    }

    @JsonProperty("msg")
    public String getMessage() {
        return message;
    }

    @JsonProperty("msg")
    public void setMessage(String message) {
        this.message = message;
    }

    @JsonProperty("status")
    private String status;

    @JsonProperty("msg")
    private String message;

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonGetter
    public Map<String, Object> getAdditionalProperties() {
        return additionalProperties;
    }

    @JsonSetter
    public void setAdditionalProperties(Map<String, Object> additionalProperties) {
        this.additionalProperties = additionalProperties;
    }
}

采纳答案by Swapnil

import com.fasterxml.Hymanson.core.JsonParser.Feature;
import com.fasterxml.Hymanson.databind.ObjectMapper;

StatusResponses loginValidator = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);

try {
    String res = result.getResponseAsString();//{"status":"true","msg":"success"}
    loginValidator = objectMapper.readValue(res, StatusResponses.class);//replaced result.getResponseAsString() with res
} catch (Exception e) {
    e.printStackTrace();
}

Don't know how it worked and why it worked? :( but it worked

不知道它是如何工作的以及它为什么工作?:(但它起作用了

回答by Katerina A.

For one, @JsonProperty("status")and @JsonProperty("msg")should only be there only when declaring the fields, not on the setters and geters.

首先,@JsonProperty("status")@JsonProperty("msg")只能在声明字段时,不会对setter和geters在那里而已。

In fact, the simplest way to parse this would be

事实上,解析这个最简单的方法是

@JsonAutoDetect  //if you don't want to have getters and setters for each JsonProperty
public class StatusResponses {

   @JsonProperty("status")
   private String status;

   @JsonProperty("msg")
   private String message;

}

回答by Amol

I could fix this error. In my case, the problem was at client side. By mistake I did not close the stream that I was writing to server. I closed stream and it worked fine. Even the error sounds like server was not able to identify the end-of-input.

我可以修复这个错误。就我而言,问题出在客户端。我错误地没有关闭我正在写入服务器的流。我关闭了流,它运行良好。即使错误听起来像服务器无法识别输入结束。

OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(jsonstring.getBytes());
out.close() ; //This is what I did

回答by jdolds

I had a similar error today and the issue was the content-type header of the post request. Make sure the content type is what you expect. In my case a "multipart/form-data" content-type header was being sent to the API instead of "application/json".

我今天遇到了类似的错误,问题是发布请求的内容类型标头。确保内容类型符合您的预期。在我的情况下,“multipart/form-data”内容类型标头被发送到 API 而不是“application/json”。

回答by Omar Abdel Bari

In my case I was reading the stream in a jersey RequestEventListener I created on the server side to log the request body prior to the request being processed. I then realized that this probably resulted in the subsequent read to yield no string (which is what is passed over when the business logic is run). I verified that to be the case.

在我的情况下,我正在读取我在服务器端创建的 jersey RequestEventListener 中的流,以在处理请求之前记录请求正文。然后我意识到这可能导致后续读取不产生字符串(这是运行业务逻辑时传递的内容)。我证实确实如此。

So if you are using streams to read the JSON string be careful of that.

因此,如果您使用流来读取 JSON 字符串,请注意这一点。

回答by Farrukh Najmi

In my case the problem was caused by my passing a null InputStream to the ObjectMapper.readValue call:

在我的情况下,问题是由于我将 null InputStream 传递给 ObjectMapper.readValue 调用引起的:

ObjectMapper objectMapper = ...
InputStream is = null; // The code here was returning null.
Foo foo = objectMapper.readValue(is, Foo.class)

I am guessing that this is the most common reason for this exception.

我猜这是此异常的最常见原因。

回答by Prakash Palnati

A simple fix could be Content-Type: application/json

一个简单的修复可能是 Content-Type: application/json

You are probably making a REST API call to get the response.

您可能正在调用 REST API 来获取响应。

Mostly you are not setting Content-Type: application/jsonwhen you the request. Content-Type: application/x-www-form-urlencodedwill be chosen which might be causing this exception.

大多数情况下,您没有Content-Type: application/json在请求时进行设置 。 Content-Type: application/x-www-form-urlencoded将被选择,这可能会导致此异常。

回答by ?smail Yavuz

I know this is weird but when I changed GetMappingto PostMappingfor both client and server side the error disappeared.

我知道这是奇怪的,但是当我改变GetMappingPostMapping为客户端和服务器端错误消失。

Both client and server are Spring boot projects.

客户端和服务器都是 Spring 启动项目。