2015-01-12 109 views
2

我正在嘗試使用Jackson CsvParser讀取一個簡單的CSV文件。 我跟着教程,但我不斷收到以下錯誤:CsvParser「找不到適合類型的構造函數」

com.fasterxml.jackson.databind.RuntimeJsonMappingException: No suitable constructor found for type [simple type, class data.MyPojo$MyPojo]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) 
at [Source: [email protected]; line: 2, column: 1] 
at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:121) 

CSV文件MYFILE.CSV是非常簡單的:

FirstAddress,SecondAddress 
Blah,Blah 
Etc,Etc 

,因此該代碼:

public class MyPojoLookup { 
    private final static String FILENAME = "/MYFILE.CSV"; 
    private final static CsvMapper mapper = new CsvMapper(); 

    static { 
     CsvSchema schema = CsvSchema.emptySchema().withHeader(); 

     InputStream input = (MyPojoLookup.class.getResourceAsStream(FILENAME)); 

     MappingIterator<MyPojo> it; 
     try { 
      it = mapper.reader(MyPojo.class).with(schema).readValues(input); 

      while (it.hasNext()){ 
       MyPojo row = it.next(); 
       log.info(row.toString()); 
      } 
     } catch (Exception e) { 
      log.error("Cannot load the addresses", e); 
      System.exit(-1); 
     } 
    } 

    private class MyPojo { 
     public String address1; 
     public String address2; 

     public MyPojo(String address1, String address2) { 
      super(); 
      this.address1 = address1; 
      this.address2 = address2; 
     } 

     @Override 
     public String toString() { 
      return "MyPojo ["address1=" + address1 + ", address2=" + address2 + "]"; 
     } 
    } 
} 

回答

3

您的代碼有三個問題:

1)您需要傳遞屬性那將要讀取到CSV模式,你的情況這些都是address1address2

CsvSchema schema = CsvSchema.builder() 
          .addColumn("address1") 
          .addColumn("address2") 
          .build(); 

2)你的內部類沒有標記爲靜態的,檢查this link這可以解釋爲什麼這是必要的。

3)你缺少一個默認的構造函數。

這裏的所有修補程序的完整列表應用:

import com.fasterxml.jackson.databind.MappingIterator; 
import com.fasterxml.jackson.dataformat.csv.CsvMapper; 
import com.fasterxml.jackson.dataformat.csv.CsvSchema; 
import java.io.InputStream; 

public class MyPojoLookup { 

    private final static String FILENAME = "/MYFILE.CSV"; 
    private final static CsvMapper mapper = new CsvMapper(); 

    public static void main(String[] args) { 
     CsvSchema schema = CsvSchema.builder().addColumn("address1").addColumn("address2").build(); 

     InputStream input = (MyPojoLookup.class.getResourceAsStream(FILENAME)); 

     MappingIterator<MyPojo> it; 
     try { 
      it = mapper.reader(MyPojo.class).with(schema).readValues(input); 

      while (it.hasNext()){ 
       MyPojo row = it.next(); 
       System.out.println(row.toString()); 
      } 
     } catch (Exception e) { 
      System.out.println("Cannot load the addresses"); 
      e.printStackTrace(); 
      System.exit(-1); 
     } 
    } 

    private static class MyPojo { 
     public String address1; 
     public String address2; 

     public MyPojo() {} 

     public MyPojo(String address1, String address2) { 
      super(); 
      this.address1 = address1; 
      this.address2 = address2; 
     } 

     @Override 
     public String toString() { 
      return "MyPojo [address1=" + address1 + ", address2=" + address2 + "]"; 
     } 
    } 
} 
+0

謝謝!那就是訣竅。編號1)雖然不是必需的,只要文件頭匹配POJO屬性,它與我的初始CsvSchema.emptySchema()。withHeader()一起工作。 – Gep

相關問題