Amar Mond
Amar Mond

Reputation: 195

ObjectMapper error in Jackson

I have the following class:

public class ServletStatus {
    private int ElapsedTime;
    private int TotalBusinessImpressionKeywordFailed;
    private int TotalKeywordStatsFailed;
    private int TotalRecentSearchesFailed;
    private int TotalBusinessImpressionKeywordSaved;
    private int TotalKeywordStatsSaved;
    private int TotalRecentSearchesSaved;

    private String error;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    // rest of the getters and setters
}

and the following JSON:

{"error": "Cannot Accept Request. Servlet has been Shut Down.. "}

and I am trying to read it as follows:

        ObjectMapper mapper = new ObjectMapper();
        ServletStatus status = mapper.readValue(in, ServletStatus.class);

        String strResult = String.format("ElapsedTime(ms): %d, TotalBusinessImpressionKeywordFailed %d, TotalRecentSearchesFailed %d, TotalKeywordStatsFailed %d, TotalBusinessImpressionKeywordSaved %d, TotalKeywordStatsSaved %d, TotalRecentSearchesSaved %d, Success %s ", 
        status.getElapsedTime(), status.getTotalBusinessImpressionKeywordFailed(), status.getTotalRecentSearchesFailed(), status.getTotalKeywordStatsFailed(), status.getTotalBusinessImpressionKeywordSaved(), status.getTotalKeywordStatsSaved(), status.getTotalRecentSearchesSaved(), status.getError());

I get the following exception:

java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1886)
at nightlybusinessstatsrestart.ShutDownBusinessStatsServlet.showdownServlet(ShutDownBusinessStatsServlet.java:38)
at nightlybusinessstatsrestart.NightlyBusinessStatsRestart.main(NightlyBusinessStatsRestart.java:56)

what am I doing wrong? My JSON contains only the error field where there is an error. On success, however, I get the following additional fields:

ElapsedTime, TotalBusinessImpressionKeywordFailed, TotalKeywordStatsFailed, TotalRecentSearchesFailed, TotalBusinessImpressionKeywordSaved, TotalKeywordStatsSaved, TotalRecentSearchesSaved;

Upvotes: 1

Views: 4365

Answers (1)

Koitoer
Koitoer

Reputation: 19543

Jackson use all the properties (fields) to generate the json for the output, so when you try to create a json representation for the Object ServletStatus , you will find something like :

{
  ElapsedTime:"",
  TotalBusinessImpressionKeywordFailed:"",
  TotalKeywordStatsFailed:"",
  TotalRecentSearchesFailed="",
  .
  .
  .
  error:""
}

If you only want the error in the json consider using

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)  

Or

public void setSerializationInclusion(JsonSerialize.Inclusion props)
Method that will define global setting of which bean/map properties are to
be included in serialization. 

And avoid failing if properties are not included during json read

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Upvotes: 2

Related Questions