3

I am trying to map Bson Document to POJO using available Codec Registry in MongoDb 3.7, But It was giving me null values. Works well if I just print out the Bson Document.

Run menthod

 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));

    }

MlsListing POJO

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;
    }
}

OUTPUT

=====================================================================
MlsData{id='null', source='null'}
mlsdb.mlsdata
Person{id='5b2942b7dccb096b526504d6', name='Ada Byron', age=20, address=Address{street='St James Square', city='London', zip='W1'}}
=====================================================================

people collection and mlsdata Collection are in the same Database , I was able to print elements in People Collection Successfully.

Is there something that I am missing ?? Any help is appreciated, Thanks.

Community
  • 1
  • 1
sudarshan kakumanu
  • 308
  • 1
  • 4
  • 15

0 Answers0