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

280,243

Solution 1

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

Solution 2

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.

Solution 3

The problem for me was that I read the response twice as follows:

System.out.println(response.body().string());
getSucherResponse = objectMapper.readValue(response.body().string(), GetSucherResponse.class);

However, the response can only be read once as it is a stream.

Solution 4

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

Solution 5

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.

Share:
280,243
Swapnil Kadam
Author by

Swapnil Kadam

Just want to get better everyday than yesterday.

Updated on July 08, 2022

Comments

  • Swapnil Kadam
    Swapnil Kadam almost 2 years

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

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

    com.fasterxml.jackson.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;
        }
    }