gpt4 book ai didi

java - 带有枚举的 CodecConfigurationException

转载 作者:行者123 更新时间:2023-12-02 01:08:05 25 4
gpt4 key购买 nike

我正在尝试创建一个示例来演示 mongodb java 驱动程序 POJO 中的枚举支持。该示例失败并出现异常 CodecConfigurationException: Cannot find a public constructor for 'PType6_'

使用的mongodb java驱动版本是3.11

build.gradle

dependencies {
/*other dependencies*/
compile 'org.mongodb:mongodb-driver-sync:3.11.0'
/*other dependencies*/
}

枚举类型 PType6_

public enum PType6_ {
ONE,
TWO,
THREE;

public static PType6_ random() {
return PType6_.values()[getRandomNumber(PType6_.values().length)];
}

持有枚举类型的 POJO PType7_

public class PType7_ {
private int number;
private PType6_ pType6_1;

public PType7_ setNumber(int number) {
this.number = number;
return this;
}

public PType7_ setPType6_1(PType6_ pType6_1) {
this.pType6_1 = pType6_1;
return this;
}

public int getNumber() {
return number;
}

public PType6_ getPType6_1() {
return pType6_1;
}

@Override
public String toString() {
return "PType7_{number=" + number + ", pType6_1=" + pType6_1 + '}';
}

public static PType7_ random() {
return new PType7_()
.setNumber(getRandomNumber(1000))
.setPType6_1(PType6_.random());
}
}

CodecRegistryFactory CustomCodecRegistriesFactoryWithPOJO

public class CustomCodecRegistriesFactoryWithPOJOs {
public static CodecRegistry getDefault() {
return MongoClientSettings.getDefaultCodecRegistry();
}

public static CodecRegistry getDefaultWithPOJOs(final Class<?>... pojoClasses) {
return CodecRegistries.fromRegistries(
CodecRegistries.fromProviders(PojoCodecProvider.builder().register(pojoClasses).build()),
getDefault()
);
}

public static CodecRegistry getDefaultWithPOJOsAndConventions(final List<Convention> conventions, final Class<?>... pojoClasses) {
return CodecRegistries.fromRegistries(
CodecRegistries.fromProviders(PojoCodecProvider.builder().conventions(conventions).register(pojoClasses).build()),
getDefault()
);
}
}

测试方法

    @Test
public void demonstrate_POJO_default_enum_registeringEnumClass_throwsException() {
final MongoClient mc = buildMongoClientWithCustomCodec(CustomCodecRegistriesFactoryWithPOJOs.getDefaultWithPOJOs(PType7_.class, PType6_.class));
final MongoDatabase mdb = initRandomDatabase(mc);
final String colName = "col";
mdb.createCollection(colName);
final MongoCollection<Document> colDocs = mdb.getCollection(colName);
final MongoCollection<PType7_> colTyped = mdb.getCollection(colName, PType7_.class);

printTitle("[Encoding] Inserting PType7_ POJO Object");
for (int i = 0; i < 5; i++) {
colTyped.insertOne(PType7_.random());
}

printTitle("Reading Collection Raw");
printDocumentMongoCollection(colDocs);

printTitle("[Decoding] Reading PType7_ POJO Objects As Strings");
try {
for (final PType7_ item : colTyped.find()) {
out.println(item);
}
} catch (final CodecConfigurationException e) {
e.printStackTrace(out);
}
}

尝试解码枚举PType6_时抛出错误

摘要我想要一个完整且有效的示例来演示如何利用 mongodb java 驱动程序 POJO 对枚举的支持。

引用:POJOs - Plain Old Java Objects | Enum support

最佳答案

似乎是当枚举类型类PType6_本身注册为POJO时抛出此异常。这会以某种方式禁用默认行为,从而阻止枚举被序列化。

以下测试方法有效

    @Test
public void demonstrate_POJO_default_enum() {
final MongoClient mc = buildMongoClientWithCustomCodec(CustomCodecRegistriesFactoryWithPOJOs.getDefaultWithPOJOs(PType7_.class));
final MongoDatabase mdb = initRandomDatabase(mc);
final String colName = "col";
mdb.createCollection(colName);
final MongoCollection<Document> colDocs = mdb.getCollection(colName);
final MongoCollection<PType7_> colTyped = mdb.getCollection(colName, PType7_.class);

printTitle("[Encoding] Inserting PType7_ POJO Object");
for (int i = 0; i < 5; i++) {
colTyped.insertOne(PType7_.random());
}

printTitle("Reading Collection Raw");
printDocumentMongoCollection(colDocs);

printTitle("[Decoding] Reading PType7_ POJO Objects As Strings");
for (final PType7_ item : colTyped.find()) {
out.println(item);
}
}

唯一的区别是工作测试方法没有提供枚举类作为 POJO。

这不起作用

    final MongoClient mc = buildMongoClientWithCustomCodec(CustomCodecRegistriesFactoryWithPOJOs.getDefaultWithPOJOs(PType7_.class, PType6_.class));

这有效

    final MongoClient mc = buildMongoClientWithCustomCodec(CustomCodecRegistriesFactoryWithPOJOs.getDefaultWithPOJOs(PType7_.class));

结论 枚举默认作为属性处理,不需要将它们添加为 POJO 类。将它们添加为 POJO 类会禁用所添加枚举类的默认行为。

关于java - 带有枚举的 CodecConfigurationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com