作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 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/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!