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

前端 未结 11 1872
盖世英雄少女心
盖世英雄少女心 2020-12-23 19:00

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

I am trying to parse this json string using Jackson parser library

相关标签:
11条回答
  • 2020-12-23 19:12

    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
    
    0 讨论(0)
  • 2020-12-23 19:13

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

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

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

    0 讨论(0)
  • 2020-12-23 19:14

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

    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.

    0 讨论(0)
  • 2020-12-23 19:16
    import com.fasterxml.jackson.core.JsonParser.Feature;
    import com.fasterxml.jackson.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

    0 讨论(0)
  • 2020-12-23 19:16

    For one, @JsonProperty("status") and @JsonProperty("msg") should only be there only when declaring the fields, not on the setters and 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;
    
    }
    
    0 讨论(0)
  • 2020-12-23 19:20

    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.

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

    0 讨论(0)
提交回复
热议问题