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 (JsonParseException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException 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;
}
}
share|improve this question

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;
}
share|improve this answer
    
but how will this solve the mapping exception? And without getters and setters how can i access the arsed values for further use? – Swapnil Nov 14 '14 at 9:08
    
You can still add the getters and setters without the annotation. I think your annotation are confusing the mapper. – Katerina A. Nov 14 '14 at 9:21
up vote 1 down vote accepted
        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 (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Dont know how it worked and why it worked? :( but it worked

share|improve this answer
2  
Think that is because when you call getResponseAsString() it read all bytes from response and close connection. That's why it may throw exception – Koloritnij Jun 1 '16 at 11:35

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
share|improve this answer
    
This answer worked for me – bademba Oct 22 '16 at 21:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.