5

I have this class:

public static class Person {

    public Person(){

    }

        public String get_id() {
            return _id;
        }

        public int getIndex() {
            return index;
        }

        public String getGuid() {
            return guid;
        }

        public boolean isActive() {
            return isActive;
        }

        public String getBalance() {
            return balance;
        }

        public String getPicture() {
            return picture;
        }

        public int getAge() {
            return age;
        }

        public String getEyeColor() {
            return eyeColor;
        }

        public String getName() {
            return name;
        }

        public String getGender() {
            return gender;
        }

        public String getCompany() {
            return company;
        }

        public String getEmail() {
            return email;
        }

        public String getPhone() {
            return phone;
        }

        public String getAddress() {
            return address;
        }

        public String getAbout() {
            return about;
        }

        public String getRegistered() {
            return registered;
        }

        public double getLatitude() {
            return latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public List<String> getTags() {
            return tags;
        }

        public List<App.Friend> getFriends() {
            return friends;
        }

        public String getGreeting() {
            return greeting;
        }

        public String getFavoriteFruit() {
            return favoriteFruit;
        }

        private boolean isActive;
        private String _id;
        private int index;
        private String guid;
        private String balance;
        private String picture;
        private int age;
        private String eyeColor;
        private String name;
        private String gender;
        private String company;
        private String email;
        private String phone;
        private String address;
        private String about;
        private String registered;
        private double latitude;
        private double longitude;
        private List<String> tags;
        private List<App.Friend> friends;
        private String greeting;
        private String favoriteFruit;

    @Override
    public String toString() {
        return "Person{" +
                "_id='" + _id + '\'' +
                ", index=" + index +
                ", guid='" + guid + '\'' +
                ", isActive=" + isActive +
                ", balance='" + balance + '\'' +
                ", picture='" + picture + '\'' +
                ", age=" + age +
                ", eyeColor='" + eyeColor + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", company='" + company + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", about='" + about + '\'' +
                ", registered='" + registered + '\'' +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                ", tags=" + tags +
                ", friends=" + friends +
                ", greeting='" + greeting + '\'' +
                ", favoriteFruit='" + favoriteFruit + '\'' +
                '}';
    }
}

I'm trying to read a JSON that contains an array of objects using Jackson ObjectMapper, everything works fine but when it has to read the field "isActive" it throws the exeption "com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isActive" (class com.jcg.maven.App$Person), not marked as ignorable (21 known properties: "gender", "registered", "guid", "name", "latitude", "about", "phone", "longitude", "index", "address", "friends", "favoriteFruit", "tags", "_id", "balance", "company", "picture", "age", "eyeColor", "greeting", "email"]) at [Source: generated.json; line: 6, column: 22] (through reference chain: java.util.ArrayList[0]->com.jcg.maven.Person["isActive"])

I don't want to make it ignorable as I would lost its value, also the field in my JSON file is written this way:

 some fields
"isActive": false,
 other fields

so I excluded the possibility of a malformed json file

Deyan koba
  • 238
  • 1
  • 4
  • 15

2 Answers2

4

As suggested in the comments, the solution was very simple, I just had to change the name of the getter and setter methods for the isActive field to getIsActive and setIsActive(IntelliJ IDEA created these methods automatically with a different name

Deyan koba
  • 238
  • 1
  • 4
  • 15
  • 1
    In case where you cannot modify the class, see my answer here: https://stackoverflow.com/a/58999529/2850848 – edmundpie Nov 22 '19 at 18:07
1

When you create getter & setter with IDE's generate option it will create getter and setter with different names.(i experienced in eclipse)

boolean isActive;

public boolean isActive() {
    return isActive;
}

public void setActive(boolean isActive) {
    this.isActive = isActive;
}

probably this issue created when we are using isActive, isDeleted type of name in variable.

Manually create getter setter for this type of field to solve issue.

    getIsActive(); 
    setIsActive();
Adarsh Patel
  • 103
  • 1
  • 6