这是我第一次使用 Mongo Scala 驱动程序(版本 2.1),我注意到在处理我的一些案例类时出现了奇怪的行为——其中一些在classToCaseClassMap
中没有被识别出来,结果是其中一个的解码数据走错了路,并以错误java.util.NoSuchElementException: head of empty list
结束org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec
第 204 行,因为fieldClazzTypeArgs
列表是空。
下面是堆栈跟踪的前三分之一左右:
error: class java.util.NoSuchElementException: head of empty list
scala.collection.immutable.Nil$.head(List.scala:420)
scala.collection.immutable.Nil$.head(List.scala:417)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readDocument(MacroCodec.scala:204)
palladium.calendar.dal.mongodb.Implicits$$anon$2$GoogleCalendarEventMacroCodec$3.readDocument(GoogleCalendarDAO.scala:55)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readValue(MacroCodec.scala:173)
palladium.calendar.dal.mongodb.Implicits$$anon$2$GoogleCalendarEventMacroCodec$3.readValue(GoogleCalendarDAO.scala:55)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.decode(MacroCodec.scala:104)
palladium.calendar.dal.mongodb.Implicits$$anon$2$GoogleCalendarEventMacroCodec$3.decode(GoogleCalendarDAO.scala:55)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readDocument(MacroCodec.scala:197)
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readDocument(GoogleCalendarDAO.scala:61)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readValue(MacroCodec.scala:173)
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readValue(GoogleCalendarDAO.scala:61)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readArray(MacroCodec.scala:187)
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readArray(GoogleCalendarDAO.scala:61)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readValue(MacroCodec.scala:174)
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readValue(GoogleCalendarDAO.scala:61)
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.decode(MacroCodec.scala:104)
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.decode(GoogleCalendarDAO.scala:61)
com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:53)
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:56)
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:57)
com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:139)
com.mongodb.connection.CommandProtocol.access$000(CommandProtocol.java:51)
com.mongodb.connection.CommandProtocol$CommandResultCallback.callCallback(CommandProtocol.java:271)
com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:48)
com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:23)
com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:470)
com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:464)
com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:119)
com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:115)
com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
com.mongodb.connection.InternalStreamConnection.executeCallbackAndReceiveResponse(InternalStreamConnection.java:378)
com.mongodb.connection.InternalStreamConnection.access$1700(InternalStreamConnection.java:66)
com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:420)
com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:389)
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onSuccess(InternalStreamConnection.java:562)
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.access$2200(InternalStreamConnection.java:517)
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:584)
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:568)
com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:447)
com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444)
com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:218)
com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:201)
sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
sun.nio.ch.Invoker.invokeDirect(Invoker.java:157)
sun.nio.ch.UnixAsynchronousSocketChannelImpl.implRead(UnixAsynchronousSocketChannelImpl.java:553)
sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:276)
sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:297)
我的应用程序为某人的谷歌日历建模。以下是我的案例类:
case class GoogleDateTime(date: Option[DateTime] = None, dateTime: Option[DateTime])
case class LongId(id: Long)
case class GoogleCalendarEvent(id: String,
status: Option[String] = None,
htmlLink: Option[String] = None,
created: Option[DateTime] = None,
updated: Option[DateTime] = None,
creator: Option[GoogleWho] = None,
organizer: Option[GoogleWho] = None,
summary: Option[String],
description: Option[String] = None,
location: Option[String] = None,
start: GoogleDateTime,
end: GoogleDateTime,
extendedProperties: Option[GoogleEventExtendedProperties] = None)
case class GoogleCalendarEventList(nextPageToken: Option[String],
items: Option[List[GoogleCalendarEvent]])
case class GoogleWho(email: Option[String],
displayName: Option[String])
case class GoogleEventSharedProperties(createdBy: Option[String] = None,
sessionType: Option[String] = None,
sessionId: Option[LongId] = None,
locationId: Option[LongId] = None,
roomId: Option[LongId] = None)
case class GoogleEventExtendedProperties(shared: Option[GoogleEventSharedProperties])
下面是一个屏幕截图的链接,该屏幕截图显示了发生错误时classToCaseClassMap
的内容。
屏幕截图显示了包含六个项目的地图,其中包括我的四个案例类:GoogleCalendarEvent
、GoogleWho
、GoogleDateTime
和GoogleEventExtendedProperties
。只有其中第一个(GoogleCalendarEvent
)被正确地标识为案例类。
我尝试sealed
有问题的类,但这没有帮助。
以下是我创建编解码器注册表的方法:
val dtMap = Map(BsonType.DATE_TIME -> classOf[DateTime])
val typeClassReplacedMap = new BsonTypeClassMap(JavaConversions.mapAsJavaMap(dtMap))
val gcCodecProvider = new DocumentCodecProvider(typeClassReplacedMap)
implicit val gcRegistry: CodecRegistry = fromRegistries(fromCodecs(dtCodec),
fromProviders( gcCodecProvider,
classOf[GoogleCalendarEvent],
classOf[GoogleDateTime],
classOf[LongId],
classOf[GoogleEventExtendedProperties],
classOf[GoogleEventSharedProperties],
classOf[GoogleWho],
classOf[GoogleCalendar]),
DEFAULT_CODEC_REGISTRY )
这是一个要点,显示了数据访问代码以及我如何使用它(在 sbt 控制台或 Intellij IDEA Scala 控制台中)。
我正在重构的应用程序在反应式 Mongo 版本 0.11.7 上运行良好,但我们需要将其迁移到官方支持的 Mongo Scala 驱动程序才能使用新的 Atlas 数据库。
我修复了从提供者项目重新排序的类似问题。 那些家伙类 A(b: B) 和类 B(i: Int) 应该按相反的顺序排列: fromProviders(classOf[B], classOf[A])
所以,看起来你应该把classOf[GoogleCalendarEvent]放在classOf[GoogleEventExtendedProperties]之后
。FWIW 在那之后,看看 https://jira.mongodb.org/browse/SCALA-338
我有同样的问题,多亏了您的帖子,我发现这在某种程度上是编解码器提供商的排序。
最初,我认为可以通过确保在这些其他案例类之前列出包含其他案例类类型值的案例类来解决。但到目前为止,即使这样也不再解决我的问题,但也许它可以帮助解决您的具体情况。
简单地从 2.1.0 降级到 2.0.0 为我解决了这个问题。