问题
I'm trying to do a basic CRUD on a scala play mongo prototype. The code works as a standalone main method - but when executed as a play application invoked through a controller, getting runtime exceptions
[debug] Running task... Cancel: Null, check cycles: false, forcegc: true
[info] play.api.Play - Application started (Dev)
[error] application -
! @7b9n058gm - Internal server error, for (GET) [/mongoTestUserCollection] ->
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeException: java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getApplicationName()Ljava/lang/String;]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:36)
Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getApplicationName()Ljava/lang/String;
at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:463)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:112)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:112)
at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:111)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:110)
at scala.Option.map(Option.scala:146)
at play.api.mvc.Action$$anonfun$apply$2.apply(Action.scala:110)
at play.api.mvc.Action$$anonfun$apply$2.apply(Action.scala:103)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253)
Caused by: java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getApplicationName()Ljava/lang/String;
at com.mongodb.MongoClientSettings$Builder.applyConnectionString(MongoClientSettings.java:181)
at org.mongodb.scala.MongoClient$.apply(MongoClient.scala:62)
at org.mongodb.scala.MongoClient$.apply(MongoClient.scala:50)
at com.test.db.mongodb.MongoDBConnector$.<init>(MongoDBConnector.scala:25)
at com.test.db.mongodb.MongoDBConnector$.<clinit>(MongoDBConnector.scala)
at com.test.db.mongodb.Repo$class.$init$(Repo.scala:15)
at com.test.db.mongodb.UserSessionHistory2MongoDBCollection$.<init>(Repo.scala:42)
at com.test.db.mongodb.UserSessionHistory2MongoDBCollection$.<clinit>(Repo.scala)
at com.test.controllers.AsyncController$$anonfun$testUserSessionHistory2MongoDBCollection$1.apply(AsyncController.scala:83)
at com.test.controllers.AsyncController$$anonfun$testUserSessionHistory2MongoDBCollection$1.apply(AsyncController.scala:83)
I tried changing the driver version - yet same errors
Build.sbt:
scalaVersion := "2.11.8"
"org.mongodb.scala" %% "mongo-scala-driver" % "2.3.0",
"org.mongodb.scala" %% "mongo-scala-bson" % "2.3.0",
Connector:
val mongoClient: MongoClient = MongoClient("mongodb://server-internal.com:27017/")
val codecRegistry = fromRegistries(fromProviders(classOf[HistoryRecord]), DEFAULT_CODEC_REGISTRY )
val database = mongoClient.getDatabase("db-test").withCodecRegistry(codecRegistry)
This code works if I invoke it as below:
object MongoService {
def makeMongoCalls(repo: Repo) = {
repo.insertHistoryRecords(5)
}
def main(args: Array[String]): Unit = {
makeMongoCalls(UserSessionHistory2MongoDBCollection)
}
}
回答1:
Caused by: java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getApplicationName()Ljava/lang/String;
Is very often happens in the case of libraries conflict.
- Run
sbt clean
- Check if you do not have other MongoDB driver in dependencies. Some library that can use it.
- Try to remove
"org.mongodb.scala" %% "mongo-scala-bson" % "2.3.0"
from dependencies. The"org.mongodb.scala" %% "mongo-scala-driver" % "2.3.0"
already have it.
来源:https://stackoverflow.com/questions/55287521/mongo-scala-play-java-lang-nosuchmethoderror-com-mongodb-connectionstring-get