简体   繁体   中英

jackson java Unrecognized field not marked as ignorable

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() {
    }

}
}

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;
    }

    ...
}

Jackson has no access to the fields of Student.

Implement the public getters and setters for Student and it works.

I sorted this problem and it's working fine. Here is my code for the same.

 **MainClass.java:**

 public class MainClass {

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

    String jsonStr = "{\r\n" + "    \"id\": \"168\",\r\n" + "   \"title\": \"Mr\",\r\n"
            + " \"externalId\": \"247518\",\r\n" + "    \"externalLink\": \"www.gmail.com\",\r\n"
            + " \"sourceApplication\": \"adsense\",\r\n" + "    \"content\": \"hmtl\",\r\n"
            + " \"summaryContent\": \"null\",\r\n" + "  \"publishedDate\": \"12122018\",\r\n"
            + " \"harvestDate\": \"12122018\",\r\n" + " \"languageId\": \"3\",\r\n" + " \"regionId\": \"45\",\r\n"
            + " \"postStatus\": \"1\"\r\n" + "}";

    ObjectMapper mapper = new ObjectMapper();

    MyPojo details = mapper.readValue(jsonStr, MyPojo.class);

    System.out.println("Value for getId  is: " + details.getId());
    System.out.println("Value for getSourceApplication  is: " + details.getSourceApplication());
    System.out.println("Value for getExternalId  is: " + details.getPublishedDate());
    System.out.println("Value for getExternalLink  is: " + details.getExternalLink());

} }

**MyPojo.class**

public class MyPojo {
private String content;

private String id;

private String sourceApplication;

private String title;

private String postStatus;

private String publishedDate;

private String summaryContent;

private String harvestDate;

private String languageId;

private String externalId;

private String regionId;

private String externalLink;

public String getContent() {
    return content;
}

public String getId() {
    return id;
}

public String getSourceApplication() {
    return sourceApplication;
}

public String getTitle() {
    return title;
}

public String getPostStatus() {
    return postStatus;
}

public String getPublishedDate() {
    return publishedDate;
}

public String getSummaryContent() {
    return summaryContent;
}

public String getHarvestDate() {
    return harvestDate;
}

public String getLanguageId() {
    return languageId;
}

public String getExternalId() {
    return externalId;
}

public String getRegionId() {
    return regionId;
}

public String getExternalLink() {
    return externalLink;
} }


**RESULT:**
 Value for getId  is: 168
 Value for getSourceApplication  is: adsense
 Value for getExternalId  is: 12122018
 Value for getExternalLink  is: www.gmail.com

NOTE One has to change the fields in the json to begin with a lower case letter. The reason for the JSON change is that the Jackson bean serialisation will reflect over the class, and when it sees getXyz() and setXyz() methods will map these to a Json filed names "xyz" (and not "Xyz").I think there are several ways to override this behaviour, one is to use the one of the Jackson annotations.

您可以简单地将private变量修改为public ,而不是创建这么多公共 getter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM