2016-06-13 13 views
0

エラーコードとしてマークされていない:ジャクソンのJava認識できないフィールドは無視

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

と私のコードは

JacksonTester.javaです:

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

は、私は以下のJSONファイルを持っています:

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

} 
} 

答えて

0

JacksonはStudentのフィールドにアクセスできません。

Studentのパブリックゲッターとセッターを実装すると動作します。

1

あなたがそれらのフィールドまたはこれらのフィールドを受け入れるコンストラクタのセッターを持っているいずれかの必要があるなどのパラメータ(+ approriate注釈やJava 8から-parameters、ジャクソン・モジュール・パラメータ名 モジュール):

public static class Student { 
    ... 
    String postStatus; 

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

    ... 
} 
関連する問題