Csv 解析器 "No suitable constructor found for type"
CsvParser "No suitable constructor found for type"
我正在尝试使用 Jackson CsvParser 读取一个简单的 CSV 文件。
我已经按照教程进行操作,但我不断收到以下错误:
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: java.io.InputStreamReader@3d362683; 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 + "]";
}
}
}
您的代码存在三个问题:
1) 您需要将要读取的属性传递给 CSV 模式,在您的情况下这些是 address1
和 address2
:
CsvSchema schema = CsvSchema.builder()
.addColumn("address1")
.addColumn("address2")
.build();
2) 您的内部 class 未标记为静态,检查 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 + "]";
}
}
}
我正在尝试使用 Jackson CsvParser 读取一个简单的 CSV 文件。 我已经按照教程进行操作,但我不断收到以下错误:
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: java.io.InputStreamReader@3d362683; 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 + "]";
}
}
}
您的代码存在三个问题:
1) 您需要将要读取的属性传递给 CSV 模式,在您的情况下这些是 address1
和 address2
:
CsvSchema schema = CsvSchema.builder()
.addColumn("address1")
.addColumn("address2")
.build();
2) 您的内部 class 未标记为静态,检查 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 + "]";
}
}
}