Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson java Unrecognized field not marked as ignorable

Tags:

java

json

jackson

The error code :

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 
Unrecognized field "id" (Class JacksonTester$Student), not
marked as ignorable
 at [Source: [B@40334c25; line: 2, column: 8] 
(through reference chain: Student["id"])

I have the below JSON file:

{
  "id": "0",
  "title": "0",
  "externalId": "0",
  "externalLink": "0",
  "sourceApplication": "0",
  "content": "0",
  "summaryContent": "0",
  "publishedDate": "0",
  "harvestDate": "0",
  "languageId": "0",
  "regionId": "0",
  "postStatus": "0"
}

and my code is

JacksonTester.java:

public class JacksonTester {
public static void main(String args[]) {

    ObjectMapper mapper = new ObjectMapper();

    // map json to student

    try {

        byte[] jsonData = Files.readAllBytes(Paths.get("output_json.txt"));
        Student student = mapper.readValue(jsonData, Student.class);
        System.out.println(student);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

static class Student {
    String id;
    String title;
    String externalId;
    String externalLink;
    String sourceApplication;
    String content;
    String summaryContent;
    String publishedDate;
    String harvestDate;
    String languageId;
    String regionId;
    String postStatus;

    public Student() {
    }

}
}
like image 928
Dhiraj Tayade Avatar asked Feb 18 '26 05:02

Dhiraj Tayade


1 Answers

You need to either have setters for those fields or a constructor that accepts those fields as parameters (+ approriate annotations or -parameters from Java 8 and jackson-module-parameter-names module):

public static class Student {
    ...
    String postStatus;   

    public setPostStatus(postStatus) {
        this.postStatus = postStatus;
    }

    ...
}
like image 128
Krzysztof Krasoń Avatar answered Feb 19 '26 19:02

Krzysztof Krasoń



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!