gpt4 book ai didi

java - Mongodb 多态性 Java 对象无法确定具体类

转载 作者:行者123 更新时间:2023-12-05 04:57:13 33 4
gpt4 key购买 nike

我将 Mongodb 与 Micronaut 一起使用,读取多态 Java 对象时出错,因为 Mongodb 无法通过其类型确定具体的实现类。

myPet: { "_id" : ObjectId("5f96f4633dbd690c548c2a38"), pets: [{"type" : "Cat" }, {"type" : "Dog" }]}

public class MyPet {
private List<Animal> pets = new ArrayList<Animal>(0);

public List<Animal> getPets() {
return pets;
}

public void setPets(List<Animal> pets) {
this.pets = pets;
}
}

public class Cat implements Animal {
private String type = "Cat";

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}

public class Dog implements Animal {
private String type = "Dog";

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}

public interface Animal {
String getType();
}

我试过了

MongoCollection<MyPet> myPetColl = db.getCollection("myPet", MyPet.class);
myPetColl.find()

但低于错误

Unexpected error occurred: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'MyPet' failed with the following exception:

Failed to decode 'MyPet'. Decoding 'content' errored with: Cannot find a public constructor for 'Animal'.

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'MyPet' failed with the following exception:

Failed to decode 'MyPet'. Decoding 'content' errored with: Cannot find a public constructor for 'Animal'.

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:40)
at com.mongodb.internal.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
at com.mongodb.internal.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at org.bson.internal.LazyCodec.decode(LazyCodec.java:48)
at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
at com.mongodb.internal.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at com.mongodb.internal.connection.ReplyMessage.<init>(ReplyMessage.java:51)
at com.mongodb.internal.connection.InternalStreamConnection.getCommandResult(InternalStreamConnection.java:412)
at com.mongodb.internal.connection.InternalStreamConnection.access$900(InternalStreamConnection.java:75)
at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:397)
at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:375)
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:676)
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:643)
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:513)
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:510)
at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:230)
at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:213)
at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:127)
at java.base/sun.nio.ch.Invoker.invokeDirect(Invoker.java:158)
at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.implRead(UnixAsynchronousSocketChannelImpl.java:560)
at java.base/sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:277)
at java.base/sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:298)
at com.mongodb.internal.connection.AsynchronousSocketChannelStream$AsynchronousSocketChannelAdapter.read(AsynchronousSocketChannelStream.java:136)
at com.mongodb.internal.connection.AsynchronousChannelStream.readAsync(AsynchronousChannelStream.java:109)
at com.mongodb.internal.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:510)
at com.mongodb.internal.connection.InternalStreamConnection.access$1000(InternalStreamConnection.java:75)
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:633)
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:618)
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:513)
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:510)
at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:230)
at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:213)
at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:127)
at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishRead(UnixAsynchronousSocketChannelImpl.java:437)
at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:191)
at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
at java.base/sun.nio.ch.KQueuePort$EventHandlerTask.run(KQueuePort.java:312)
at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

有没有办法用 if-condition 返回正确的类类型:

if (obj.type == "Dog") 返回 Dog.class else Cat.class

就像 jackson 一样

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "Dog"),
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
})

最佳答案

您可以使用 @BsonDiscriminator 注解来告诉 MongoDB 驱动程序哪个字段用作鉴别器键。这是一个工作示例:

import org.bson.codecs.pojo.annotations.BsonDiscriminator;

@BsonDiscriminator(key = Animal.DISCRIMINATOR_KEY)
public interface Animal {
String DISCRIMINATOR_KEY = "type";

String getType();
}

public class Cat implements Animal {
@Override
public String getType() {
return Cat.class.getName();
}
}

public class Dog implements Animal {
@Override
public String getType() {
return Dog.class.getName();
}
}

如您所见,我使用完整的类名作为 type 字段的值。不幸的是,自定义鉴别器值(不同于完整的类名)现在在 Micronaut MongoDB 客户端中还不起作用。就是因为这个问题,还没有解决:https://github.com/micronaut-projects/micronaut-mongodb/issues/10

关于java - Mongodb 多态性 Java 对象无法确定具体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64546106/

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