MongoDB Java Insert Throws org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class io.github.ilkgunel.mongodb.Pojo
我正在用Java学习MongoDB。我正在尝试使用Java驱动程序将数据插入MongoDB。我正在像MongoDB教程中一样进行插入,每件事都是okey。但是,如果我想插入一个变量,并且当我运行代码时,驱动程序会抛出如下错误:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class io.github.ilkgunel.mongodb.Pojo.
我像这样在Stack Overflow中提出了问题,但我什么也听不懂,也找不到任何解决这个错误的东西。我的代码在下面。如何解决这个问题?
我正在使用这个代码:
package io.github.ilkgunel.mongodb;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
public class MongoDBBasicUsage {
public static void main(String[] args) {
MongoClient mongoClient;
try {
Pojo pojo = new Pojo();
mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("MongoDB");
pojo.setId("1");
pojo.setName("ilkay");
pojo.setSurname("günel");
Document document = new Document();
document.put("person", pojo);
database.getCollection("Records").insertOne(document);
} catch (Exception e) {
System.err.println("Bir Hata Meydana Geldi!");
System.out.println("Hata" + e);
}
}
}
我的Pojo是这样的:
package io.github.ilkgunel.mongodb;
public class Pojo {
String name;
String surname;
String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}