MongoDB Java驱动程序〜解码抽象类

问题描述 投票:1回答:1

我有一个复杂的数据模型,该模型除其他外还使用抽象类。使用AutomaticPojoCodec将数据模型对象写入MongoDB可以正常工作,但是当尝试再次从MongoDB读取数据模型对象时,显示以下错误:

org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'ReqIF' failed with the following exception:

Failed to decode 'coreContent'. Failed to decode 'reqIF_content'. Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:

null

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.

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.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
    at com.mongodb.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.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
    at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
    at com.mongodb.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.connection.ReplyMessage.<init>(ReplyMessage.java:50)
    at com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:132)
    at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:111)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
    at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:176)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:113)
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:715)
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:709)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:433)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:709)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:81)
    at com.mongodb.Mongo.execute(Mongo.java:810)
    at com.mongodb.Mongo$2.execute(Mongo.java:797)
    at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
    at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:200)
    at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:37)
    at io.cesonia.camelot.knight.reqIF.mongoDB.ReqIF_MongoDB_Reader.findByAddress(ReqIF_MongoDB_Reader.java:43)
    at io.cesonia.camelot.knight.reqIF.mongoDB.ReqIF_MongoDB_Reader.readReqIF_DataFromMongoDB(ReqIF_MongoDB_Reader.java:29)
    at io.cesonia.camelot.knight.reqIF.mongoDB.ReqIF_MongoDB_ReaderTest.readReqIF_DataFromMongoDB_shouldReturnReqIF_dataObject(ReqIF_MongoDB_ReaderTest.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:205)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'coreContent'. Failed to decode 'reqIF_content'. Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:

null

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
    at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
    at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37)
    ... 97 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'reqIF_content'. Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:

null

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
    at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
    at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:55)
    at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
    ... 101 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:

null

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
    at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
    at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:55)
    at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
    ... 107 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:

null

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
    at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
    at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:55)
    at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
    ... 113 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:

null

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 org.bson.codecs.pojo.CollectionCodec.decode(CollectionCodec.java:53)
    at org.bson.codecs.pojo.CollectionCodec.decode(CollectionCodec.java:30)
    at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
    ... 119 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException
    at org.bson.codecs.pojo.CreatorExecutable.getInstance(CreatorExecutable.java:89)
    at org.bson.codecs.pojo.InstanceCreatorImpl.<init>(InstanceCreatorImpl.java:40)
    at org.bson.codecs.pojo.InstanceCreatorFactoryImpl.create(InstanceCreatorFactoryImpl.java:28)
    at org.bson.codecs.pojo.ClassModel.getInstanceCreator(ClassModel.java:70)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:102)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
    at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37)
    ... 123 more
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.bson.codecs.pojo.CreatorExecutable.getInstance(CreatorExecutable.java:84)
    ... 129 more

我相信此错误的相关内容如下:

Failed to decode 'coreContent'.
Failed to decode 'reqIF_content'.
Failed to decode 'datatypes'.
Failed to decode 'datatypeDefinitions'. 
An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
[…]
Caused by: java.lang.InstantiationException

我认为发生这种情况是因为“ DatatypeDefinitionTag”是一个抽象类,因此不能也不应将其初始化。它用于存储有关数据类型定义的数据,因此可以包含可变数量的DatatypeDefinitionStringDatatypeDefinitionDateDatatypeDefinitionInt等...对象。

在我的数据类型类中,错误变量datatypeDefinitions的定义如下:

    public List<DatatypeDefinitionTag> datatypeDefinitions;

...并且该变量充满了看起来像这样的对象:

public class DatatypeDefinitionString extends DatatypeDefinitionTag{[...]}
public class DatatypeDefinitionInt extends DatatypeDefinitionTag{[...]}
public class DatatypeDefinitionDate extends DatatypeDefinitionTag{[...]}

我有理由确定这是导致错误的原因,因为如果我从数据对象中删除数据类型,该错误将不再出现。但是,这自然不是可行的解决方案。

我的问题归结为两件事:

  1. 我是否正确假设这里的问题是AutomaticPojoCodec无法解码抽象对象?
  2. 我该怎么办?
java mongodb codec
1个回答
0
投票

我终于让它工作了。

秘密在于升级到org.mongodb:mongodb-driver:3.6.0或更高版本,从而解决了此问题。之后,只需添加以下注释即可:

@BsonDiscriminator
public abstract class DatatypeDefinitionTag extends IdentifiableTag {
© www.soinside.com 2019 - 2024. All rights reserved.