Roni Koren Kurtberg
Roni Koren Kurtberg

Reputation: 515

Error com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input

I'm trying to convert the next string:

"{ \"contacts\": [{\"name\":\"1\",\"phone\":\"+123456\"}]}"

to some Custom object:

public class CustomObject{

    private List<Contact> contacts;

    public CustomObject(){

    }

    public CustomObject(List<Contact> contacts) {
        this.contacts = contacts;
    }

    public List<Contact> getContactList() {
        return contacts;
    }

    public void setContactList(List<Contact> contacts) {
        this.contacts = contacts;
    }
}

In addition, there is another object within this CustomObject:

public class Contact {

    private String name;
    private String phone;

    public Contact() {
    }

    public Contact(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

Right now, I'm trying to do the following:

private List<Contact> parseString(String jsonAsString) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    CustomObject customObject = mapper.readValue(jsonAsString, CustomObject .class);
    return customObject .getContactList();
}

But I'm getting the next error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: (String)""{ \"contacts\": [{\"name\":\"1\",\"phone\":\"+972545519713\"}]}""; line: 1, column: 1]

Upvotes: 1

Views: 18821

Answers (1)

Mena
Mena

Reputation: 48444

Jackson is clever, but not that clever.

Your public setter setContactList cannot be recognized during de-serialization without an annotation.

You have two choices:

  1. Annotate it with @JsonProperty("contacts")
  2. Change it to setContacts

Other choices would include changing your JSON or making the actual fields accessible - not elaborating as they would very likely be bad choices.

Also consider revising your code in other places, e.g. for the getContactList getter if you plan on serializing instances of your POJO.

Upvotes: 7

Related Questions