我试图使用MongoDb 3.7中可用的Codec Registry将Bson Document映射到POJO,但这给了我空值。如果我只打印出Bson文档,效果很好。
div[class*="dashboard-wrapper transition"]
public void run(MlsMongoConfiguration configuration, Environment environment) throws Exception {
// Create a CodecRegistry containing the PojoCodecProvider instance.
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientOptions.Builder clientOptions = new MongoClientOptions.Builder();
MongoClient mongoClient = new MongoClient(new ServerAddress(configuration.mongohost, configuration.mongoport), clientOptions.build());
MongoManaged mongoManaged = new MongoManaged(mongoClient);
environment.lifecycle().manage(mongoManaged);
MongoDatabase db = mongoClient.getDatabase(configuration.mongodb);
MongoCollection<MlsListing> collection = db.getCollection("mlsdata", MlsListing.class).withCodecRegistry(pojoCodecRegistry);
MongoCollection<Person> people_collection = db.getCollection("people", Person.class).withCodecRegistry(pojoCodecRegistry);
// make a document and insert it
//Person ada = new Person("Ada Byron", 20, new Address("St James Square", "London", "W1"));
//people_collection.insertOne(ada);
MlsListing somelisting = collection.find().first();
Person somebody = people_collection.find().first();
System.out.println("=====================================================================");
System.out.println(somelisting);
System.out.println(collection.getNamespace());
System.out.println(somebody);
System.out.println("=====================================================================");
environment.jersey().register(new MlsMongoResource(collection));
}
import org.bson.types.ObjectId;
import java.util.Date;
public final class MlsListing {
public MlsListing(ObjectId _id, String SOURCE) {
this._id = _id;
this.SOURCE = SOURCE;
}
public ObjectId get_id() {
return _id;
}
public void set_id(ObjectId _id) {
this._id = _id;
}
public String getSOURCE() {
return SOURCE;
}
public void setSOURCE(String SOURCE) {
this.SOURCE = SOURCE;
}
private ObjectId _id;
private String SOURCE;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("MlsData{");
sb.append("id='").append(_id).append('\'');
sb.append(", source='").append(SOURCE).append('\'');
//sb.append(", listing_num='").append(LISTING_NUM).append('\'');
sb.append('}');
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MlsListing that = (MlsListing) o;
if (_id != null ? !_id.equals(that._id) : that._id != null) return false;
return true;
}
@Override
public int hashCode() {
int result = _id != null ? _id.hashCode() : 0;
result = 31 * result + (SOURCE != null ? SOURCE.hashCode() : 0);
//result = 31 * result + (LISTING_NUM != null ? LISTING_NUM.hashCode() : 0);
return result;
}
}
people集合和mlsdata集合在同一个数据库中,我能够成功打印People集合中的元素。
有什么我想念的吗?感谢您的任何帮助,谢谢。