MongoDriver和Play:java.lang.NoSuchMethodError:play.api.libs.json.JsLookup

时间:2016-04-21 11:50:08

标签: json mongodb playframework bson

我有以下代码:

import play.api.libs.json.Json
import reactivemongo.api.MongoDriver
import reactivemongo.play.json.BSONFormats

import scala.concurrent.ExecutionContext.Implicits.global

object DoesNotWork extends App {
  val driver = new MongoDriver
  val connection = driver.connection(List("localhost"))
  val db = connection("superman")
  var collection = db.collection("test")

  val album = Json.obj(
    "title" -> "Back in Black",
    "releaseYear" -> 1980,
    "allMusicRating" -> Some(5.0),
    "tracks" -> Json.arr(
      "Hells Bells",
      "Shoot to Thrill")
  )

  BSONFormats.toBSON(album) // crash
}

以及以下依赖项:

 <dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.12.0-M4</version>
    </dependency>

    <dependency>
        <groupId>com.typesafe.play</groupId>
        <artifactId>play_2.11</artifactId>
        <version>2.5.2</version>
    </dependency>

    <dependency>
        <groupId>org.reactivemongo</groupId>
        <artifactId>play2-reactivemongo_2.11</artifactId>
        <version>0.11.11-play24</version>
    </dependency>

    <dependency>
        <groupId>io.megl</groupId>
        <artifactId>play-json-extra_2.11</artifactId>
        <version>2.4.3</version>
    </dependency>

    <dependency>
        <groupId>io.really</groupId>
        <artifactId>jwt-scala_2.11</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>com.typesafe.play</groupId>
        <artifactId>play-json_2.11</artifactId>
        <version>2.5.2</version>
    </dependency>
</dependencies>

然而,在运行此示例时,我得到了

Exception in thread "main" java.lang.NoSuchMethodError: play.api.libs.json.JsLookup$.$bslash$extension(Lplay/api/libs/json/JsLookupResult;Ljava/lang/String;)Lplay/api/libs/json/JsLookupResult;
    at reactivemongo.play.json.BSONFormats$BSONDateTimeFormat$DateValue$.unapply(package.scala:224)
    at reactivemongo.play.json.BSONFormats$BSONDateTimeFormat$$anonfun$15.applyOrElse(package.scala:216)
    at reactivemongo.play.json.BSONFormats$BSONDateTimeFormat$$anonfun$15.applyOrElse(package.scala:215)
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:171)
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:172)
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:172)
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:172)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:224)
    at scala.PartialFunction$Lifted.apply(PartialFunction.scala:220)
    at reactivemongo.play.json.BSONFormats$class.toBSON(package.scala:393)
    at reactivemongo.play.json.BSONFormats$.toBSON(package.scala:82)
    at DoesNotWork$.delayedEndpoint$DoesNotWork$1(DoesNotWork.scala:22)
    at DoesNotWork$delayedInit$body.apply(DoesNotWork.scala:7)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:376)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at DoesNotWork$.main(DoesNotWork.scala:7)
    at DoesNotWork.main(DoesNotWork.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Uncaught error from thread [reactivemongo-akka.actor.default-dispatcher-2] shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled for ActorSystem[reactivemongo]
java.lang.NoSuchMethodError: scala.collection.immutable.Stream.filter(Lscala/Function1;)Lscala/collection/immutable/Stream;
    at reactivemongo.bson.BSONDocument.elements(types.scala:138)
    at reactivemongo.bson.buffer.DefaultBufferHandler$BSONDocumentBufferHandler$.write(bufferhandlers.scala:90)
    at reactivemongo.bson.buffer.DefaultBufferHandler$BSONDocumentBufferHandler$.write(bufferhandlers.scala:86)
    at reactivemongo.bson.buffer.DefaultBufferHandler$.serialize(bufferhandlers.scala:226)
    at reactivemongo.bson.buffer.DefaultBufferHandler$.writeDocument(bufferhandlers.scala:241)
    at reactivemongo.api.BSONSerializationPack$.writeToBuffer(serializationpack.scala:73)
    at reactivemongo.api.BSONSerializationPack$.writeToBuffer(serializationpack.scala:49)
    at reactivemongo.api.SerializationPack$class.serializeAndWrite(serializationpack.scala:25)
    at reactivemongo.api.BSONSerializationPack$.serializeAndWrite(serializationpack.scala:49)
    at reactivemongo.api.commands.Command$.buildRequestMaker(commands.scala:216)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$sendIsMaster$1.apply(actors.scala:818)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$sendIsMaster$1.apply(actors.scala:810)
    at scala.Option.map(Option.scala:146)
    at reactivemongo.core.actors.MongoDBSystem$class.sendIsMaster(actors.scala:810)
    at reactivemongo.core.actors.LegacyDBSystem.sendIsMaster(actors.scala:886)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$4$$anonfun$applyOrElse$23.apply(actors.scala:434)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$4$$anonfun$applyOrElse$23.apply(actors.scala:433)
    at reactivemongo.core.nodeset.NodeSet$$anonfun$10.apply(nodeset.scala:110)
    at reactivemongo.core.nodeset.NodeSet$$anonfun$10.apply(nodeset.scala:105)
    at scala.collection.TraversableLike.scala$collection$TraversableLike$$$anonfun$3(TraversableLike.scala:234)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1409)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
    at scala.collection.TraversableLike.map(TraversableLike.scala:234)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at reactivemongo.core.nodeset.NodeSet.updateByChannelId(nodeset.scala:105)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$4.applyOrElse(actors.scala:433)
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:171)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:480)
    at reactivemongo.core.actors.LegacyDBSystem.aroundReceive(actors.scala:886)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
    at akka.actor.ActorCell.invoke(ActorCell.scala:495)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
    at akka.dispatch.Mailbox.run(Mailbox.scala:224)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
[ERROR] [04/21/2016 13:45:51.467] [reactivemongo-akka.actor.default-dispatcher-2] [akka.actor.ActorSystemImpl(reactivemongo)] Uncaught error from thread [reactivemongo-akka.actor.default-dispatcher-2] shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled
java.lang.NoSuchMethodError: scala.collection.immutable.Stream.filter(Lscala/Function1;)Lscala/collection/immutable/Stream;
    at reactivemongo.bson.BSONDocument.elements(types.scala:138)
    at reactivemongo.bson.buffer.DefaultBufferHandler$BSONDocumentBufferHandler$.write(bufferhandlers.scala:90)
    at reactivemongo.bson.buffer.DefaultBufferHandler$BSONDocumentBufferHandler$.write(bufferhandlers.scala:86)
    at reactivemongo.bson.buffer.DefaultBufferHandler$.serialize(bufferhandlers.scala:226)
    at reactivemongo.bson.buffer.DefaultBufferHandler$.writeDocument(bufferhandlers.scala:241)
    at reactivemongo.api.BSONSerializationPack$.writeToBuffer(serializationpack.scala:73)
    at reactivemongo.api.BSONSerializationPack$.writeToBuffer(serializationpack.scala:49)
    at reactivemongo.api.SerializationPack$class.serializeAndWrite(serializationpack.scala:25)
    at reactivemongo.api.BSONSerializationPack$.serializeAndWrite(serializationpack.scala:49)
    at reactivemongo.api.commands.Command$.buildRequestMaker(commands.scala:216)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$sendIsMaster$1.apply(actors.scala:818)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$sendIsMaster$1.apply(actors.scala:810)
    at scala.Option.map(Option.scala:146)
    at reactivemongo.core.actors.MongoDBSystem$class.sendIsMaster(actors.scala:810)
    at reactivemongo.core.actors.LegacyDBSystem.sendIsMaster(actors.scala:886)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$4$$anonfun$applyOrElse$23.apply(actors.scala:434)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$4$$anonfun$applyOrElse$23.apply(actors.scala:433)
    at reactivemongo.core.nodeset.NodeSet$$anonfun$10.apply(nodeset.scala:110)
    at reactivemongo.core.nodeset.NodeSet$$anonfun$10.apply(nodeset.scala:105)
    at scala.collection.TraversableLike.scala$collection$TraversableLike$$$anonfun$3(TraversableLike.scala:234)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1409)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
    at scala.collection.TraversableLike.map(TraversableLike.scala:234)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at reactivemongo.core.nodeset.NodeSet.updateByChannelId(nodeset.scala:105)
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$4.applyOrElse(actors.scala:433)
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:171)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:480)
    at reactivemongo.core.actors.LegacyDBSystem.aroundReceive(actors.scala:886)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
    at akka.actor.ActorCell.invoke(ActorCell.scala:495)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
    at akka.dispatch.Mailbox.run(Mailbox.scala:224)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

Google上有各种各样的搜索结果,但都没有。我有点假设我只是错过了依赖。我该如何解决这个错误?

1 个答案:

答案 0 :(得分:1)

将playframework从2.4升级到2.5后,我遇到了同样的问题。 通过将reactivemongo升级到0.11.11来修复。 我可以看到你正在使用相同的reactivemongo版本。 我怀疑与jwt有关 请参阅此https://github.com/pauldijou/jwt-scala/issues/15