MongoDB Java自己的编解码器无法正常工作

时间:2017-09-21 19:19:54

标签: java mongodb class codec provider

我尝试使用自己的.class从MongoDB保存/获取文档 我创建了自己的编解码器,一切正常,除了解码

我花了很多时间重新创建,搜索解决方案但仍然没有理由为什么它不起作用:/

测试方法:

CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(new UserCodecProvider()),
            MongoClients.getDefaultCodecRegistry());

    MongoCredential credential = MongoCredential.createCredential(user, base, pass);

    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Arrays.asList(new ServerAddress(host, port)))
            .build();

    MongoClientSettings settings = MongoClientSettings.builder()
            .clusterSettings(clusterSettings)
            .codecRegistry(codecRegistry)
            .credentialList(Arrays.asList(credential))
            .build();

    MongoClient mongoClient = MongoClients.create(settings);
    MongoDatabase database = mongoClient.getDatabase(base);
    MongoCollection<User> collection = database.getCollection("test", User.class);

    CountDownLatch countDownLatch = new CountDownLatch(1);

    User user = new User();
    user.setPoints(1);
    user.setName("Test");

    collection.insertOne(user, (aVoid, throwable) -> {
        System.out.println(user);
        countDownLatch.countDown();
    });

    countDownLatch.await();

    CountDownLatch countDownLatch2 = new CountDownLatch(1);

    collection.find(eq("name", "Test")).first((user1, throwable) -> {
        System.out.println(user1);
        countDownLatch2.countDown();
    });

    countDownLatch2.await();

UserCodecProvider.class

public class UserCodecProvider implements CodecProvider {

@Override
public <T> Codec<T> get(Class<T> aClass, CodecRegistry codecRegistry) {
    if(aClass == User.class) {
        return (Codec<T>) new UserCodec(codecRegistry);
    }
    return null;
}

}

UserCodec.class

public class UserCodec implements Codec<User> {

private CodecRegistry codecRegistry;

public UserCodec(CodecRegistry codecRegistry) {
    this.codecRegistry = codecRegistry;
}

@Override
public void encode(BsonWriter writer, User user, EncoderContext encoderContext) {
    writer.writeStartDocument();

    writer.writeName("name");
    writer.writeString(user.getName());
    writer.writeName("points");
    writer.writeInt32(user.getPoints());

    writer.writeEndDocument();
}

@Override
public User decode(BsonReader reader, DecoderContext decoderContext) {
    reader.readStartDocument();

    String name = reader.readString("name");
    int points = reader.readInt32("points");

    reader.readEndDocument();

    User user = new User();
    user.setName(name);
    user.setPoints(points);
    return user;
}

@Override
public Class<User> getEncoderClass() {
    return User.class;
}

}

User.class

public class User {

private String name;

private int points;

public User() {

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPoints() {
    return points;
}

public void setPoints(int points) {
    this.points = points;
}

@Override
public String toString() {
    return "User [name=" + name + ", points=" + points + "]";
}
}

测试方法的输出:

User [name=Test, points=1]
null

Process finished with exit code 0

//编辑 我找到了https://github.com/dozd/mongo-mapper的解决方案 但如果你知道如何在没有这个lib的情况下做到这一点,那就回答吧。 谢谢! :)

0 个答案:

没有答案