在浏览了两天的教程和帮助网站之后,我似乎找不到我的问题的答案。
我是 Scala 和 Mongo 的新手,我正在使用 Scalatra (v2.4.1)、Scala (v2.11.8) 和 mongo-scala-driver (v1.1.1) 构建 HTTP 服务器。
MongoinsertOne
函数的基本 Scalatra 教程返回错误:
ERROR SalesAppScalatraServlet - Employee insertion failed! java.lang.NoSuchMethodError: com.mongodb.operation.MixedBulkWriteOperation.bypassDocumentValidation(Ljava/lang/Boolean;)Lcom/mongodb/operation/MixedBulkWriteOperation;
at com.mongodb.async.client.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:588)
at com.mongodb.async.client.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:314)
at com.mongodb.async.client.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:305)
at org.mongodb.scala.MongoCollection$$anonfun$insertOne$1.apply(MongoCollection.scala:251)
at org.mongodb.scala.MongoCollection$$anonfun$insertOne$1.apply(MongoCollection.scala:251)
at org.mongodb.scala.internal.ObservableHelper$$anon$2.apply(ObservableHelper.scala:42)
at org.mongodb.scala.internal.ObservableHelper$$anon$2.apply(ObservableHelper.scala:40)
at com.mongodb.async.client.SingleResultCallbackSubscription.requestInitialData(SingleResultCallbackSubscription.java:39)
at com.mongodb.async.client.AbstractSubscription.tryRequestInitialData(AbstractSubscription.java:151)
at com.mongodb.async.client.AbstractSubscription.request(AbstractSubscription.java:82)
at org.mongodb.scala.ObservableImplicits$BoxedSubscription.request(ObservableImplicits.scala:382)
at org.mongodb.scala.Observer$class.onSubscribe(Observer.scala:49)
at com.syncroness.salesapp.SalesAppScalatraServlet$$anonfun$1$$anon$1.onSubscribe(SalesAppScalatraServlet.scala:35)
at org.mongodb.scala.Observer$class.onSubscribe(Observer.scala:85)
at com.syncroness.salesapp.SalesAppScalatraServlet$$anonfun$1$$anon$1.onSubscribe(SalesAppScalatraServlet.scala:35)
at com.mongodb.async.client.SingleResultCallbackSubscription.<init>(SingleResultCallbackSubscription.java:34)
at com.mongodb.async.client.Observables$2.subscribe(Observables.java:76)
at org.mongodb.scala.ObservableImplicits$BoxedObservable.subscribe(ObservableImplicits.scala:366)
at com.syncroness.salesapp.SalesAppScalatraServlet$$anonfun$1.apply$mcV$sp(SalesAppScalatraServlet.scala:35)
at com.syncroness.salesapp.SalesAppScalatraServlet$$anonfun$1.apply(SalesAppScalatraServlet.scala:31)
at com.syncroness.salesapp.SalesAppScalatraServlet$$anonfun$1.apply(SalesAppScalatraServlet.scala:31)
at org.scalatra.ScalatraBase$class.org$scalatra$ScalatraBase$$liftAction(ScalatraBase.scala:285)
at org.scalatra.ScalatraBase$$anonfun$invoke$1.apply(ScalatraBase.scala:279)
at org.scalatra.ScalatraBase$$anonfun$invoke$1.apply(ScalatraBase.scala:279)
at org.scalatra.ApiFormats$class.withRouteMultiParams(ApiFormats.scala:189)
at com.syncroness.salesapp.SalesAppScalatraServlet.withRouteMultiParams(SalesAppScalatraServlet.scala:12)
at org.scalatra.ScalatraBase$class.invoke(ScalatraBase.scala:278)
at com.syncroness.salesapp.SalesAppScalatraServlet.org$scalatra$json$JsonSupport$$super$invoke(SalesAppScalatraServlet.scala:12)
at org.scalatra.json.JsonSupport$$anonfun$invoke$1.apply(JsonSupport.scala:88)
at org.scalatra.json.JsonSupport$$anonfun$invoke$1.apply(JsonSupport.scala:82)
at org.scalatra.ApiFormats$class.withRouteMultiParams(ApiFormats.scala:189)
at com.syncroness.salesapp.SalesAppScalatraServlet.withRouteMultiParams(SalesAppScalatraServlet.scala:12)
at org.scalatra.json.JsonSupport$class.invoke(JsonSupport.scala:82)
at com.syncroness.salesapp.SalesAppScalatraServlet.invoke(SalesAppScalatraServlet.scala:12)
at org.scalatra.ScalatraBase$$anonfun$runRoutes$1$$anonfun$apply$8.apply(ScalatraBase.scala:253)
at org.scalatra.ScalatraBase$$anonfun$runRoutes$1$$anonfun$apply$8.apply(ScalatraBase.scala:251)
at scala.Option.flatMap(Option.scala:171)
at org.scalatra.ScalatraBase$$anonfun$runRoutes$1.apply(ScalatraBase.scala:251)
at org.scalatra.ScalatraBase$$anonfun$runRoutes$1.apply(ScalatraBase.scala:250)
at scala.collection.immutable.Stream.flatMap(Stream.scala:489)
at org.scalatra.ScalatraBase$class.runRoutes(ScalatraBase.scala:250)
at com.syncroness.salesapp.SalesAppScalatraServlet.runRoutes(SalesAppScalatraServlet.scala:12)
at org.scalatra.ScalatraBase$class.runActions$1(ScalatraBase.scala:175)
at org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply$mcV$sp(ScalatraBase.scala:187)
at org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply(ScalatraBase.scala:187)
at org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply(ScalatraBase.scala:187)
at org.scalatra.ScalatraBase$class.org$scalatra$ScalatraBase$$cradleHalt(ScalatraBase.scala:205)
at org.scalatra.ScalatraBase$class.executeRoutes(ScalatraBase.scala:187)
at com.syncroness.salesapp.SalesAppScalatraServlet.executeRoutes(SalesAppScalatraServlet.scala:12)
at org.scalatra.ScalatraBase$$anonfun$handle$1.apply$mcV$sp(ScalatraBase.scala:126)
at org.scalatra.ScalatraBase$$anonfun$handle$1.apply(ScalatraBase.scala:126)
at org.scalatra.ScalatraBase$$anonfun$handle$1.apply(ScalatraBase.scala:126)
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
at org.scalatra.DynamicScope$class.withResponse(DynamicScope.scala:78)
at com.syncroness.salesapp.SalesAppScalatraServlet.withResponse(SalesAppScalatraServlet.scala:12)
at org.scalatra.DynamicScope$$anonfun$withRequestResponse$1.apply(DynamicScope.scala:58)
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
at org.scalatra.DynamicScope$class.withRequest(DynamicScope.scala:69)
at com.syncroness.salesapp.SalesAppScalatraServlet.withRequest(SalesAppScalatraServlet.scala:12)
at org.scalatra.DynamicScope$class.withRequestResponse(DynamicScope.scala:57)
at com.syncroness.salesapp.SalesAppScalatraServlet.withRequestResponse(SalesAppScalatraServlet.scala:12)
at org.scalatra.ScalatraBase$class.handle(ScalatraBase.scala:125)
at com.syncroness.salesapp.SalesAppScalatraServlet.org$scalatra$servlet$ServletBase$$super$handle(SalesAppScalatraServlet.scala:12)
at org.scalatra.servlet.ServletBase$class.handle(ServletBase.scala:53)
at com.syncroness.salesapp.SalesAppScalatraServlet.org$scalatra$scalate$ScalateSupport$$super$handle(SalesAppScalatraServlet.scala:12)
at org.scalatra.scalate.ScalateSupport$class.handle(ScalateSupport.scala:152)
at com.syncroness.salesapp.SalesAppScalatraServlet.handle(SalesAppScalatraServlet.scala:12)
at org.scalatra.ScalatraServlet$class.service(ScalatraServlet.scala:60)
at com.syncroness.salesapp.SalesAppScalatraServlet.service(SalesAppScalatraServlet.scala:12)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:751)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:566)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:578)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:498)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:98)
at org.eclipse.jetty.server.Server.handle(Server.java:461)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:284)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:534)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:745)
我的 Scalatra Servlet 如下:
package com.foo.bar
import com.typesafe.scalalogging.Logger
import java.io.{StringWriter, PrintWriter}
import org.slf4j.LoggerFactory
import org.json4s.{DefaultFormats, Formats}
import org.scalatra._
import org.scalatra.json._
import org.mongodb._
import org.mongodb.scala.{Completed, MongoCollection, MongoDatabase, Document, Observable, Observer}
class SalesAppScalatraServlet(database: MongoDatabase) extends SalesappserverStack with JacksonJsonSupport {
protected implicit lazy val jsonFormats: Formats = DefaultFormats
val logger = Logger(LoggerFactory.getLogger("SalesAppScalatraServlet"))
logger.trace("SalesAppScalatraServlet started")
val employeeCollection: MongoCollection[Document] = database.getCollection("employee");
def getStackTraceAsString(t: Throwable) = {
val sw = new StringWriter
t.printStackTrace(new PrintWriter(sw))
sw.toString
}
get("/") {
halt(status = 403, reason = "Forbidden")
}
post("/employee") {
val doc: Document = Document("_id" -> 1, "name" -> "John Smith")
val observable: Observable[Completed] = employeeCollection.insertOne(doc)
observable.subscribe(new Observer[Completed] {
override def onNext(result: Completed): Unit = logger.info("Employee inserted successfully!")
override def onError(e: Throwable): Unit = {
logger.error("Employee insertion failed! " + getStackTraceAsString(e))
halt(status = 520, reason = "Insert Employee Error")
}
override def onComplete(): Unit = Ok()
})
}
notFound {
<h2>Page not found</h2>
}
}
我的 Scalatra 引导文件如下:
import com.foo.bar._
import org.scalatra._
import javax.servlet.ServletContext
import org.mongodb.scala.{MongoClient, MongoDatabase}
class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("salesAppDB")
context.mount(new SalesAppScalatraServlet(database), "/*")
}
}
而我的 build.scala 文件如下:
import sbt._
import Keys._
import org.scalatra.sbt._
import org.scalatra.sbt.PluginKeys._
import com.earldouglas.xwp.JettyPlugin
import com.mojolly.scalate.ScalatePlugin._
import ScalateKeys._
object SalesappserverBuild extends Build {
val Organization = "com.foo"
val Name = "SalesAppServer"
val Version = "1.0.0"
val ScalaVersion = "2.11.8"
val ScalatraVersion = "2.4.1"
lazy val project = Project (
"salesappserver",
file("."),
settings = ScalatraPlugin.scalatraSettings ++ scalateSettings ++ Seq(
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
resolvers += Classpaths.typesafeReleases,
resolvers += "Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases",
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % ScalaVersion,
"org.mongodb.scala" %% "mongo-scala-driver" % "1.1.1",
"org.fusesource.jansi" % "jansi" % "1.8",
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.1.7" % "runtime",
"com.typesafe.scala-logging" %% "scala-logging" % "3.4.0",
"org.eclipse.jetty" % "jetty-webapp" % "9.2.15.v20160210" % "container",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
"org.scalatra" %% "scalatra-json" % ScalatraVersion,
"org.json4s" %% "json4s-jackson" % "3.3.0",
"org.json4s" %% "json4s-mongo" % "3.3.0"
),
scalateTemplateConfig in Compile <<= (sourceDirectory in Compile){ base =>
Seq(
TemplateConfig(
base / "webapp" / "WEB-INF" / "templates",
Seq.empty, /* default imports should be added here */
Seq(
Binding("context", "_root_.org.scalatra.scalate.ScalatraRenderContext", importMembers = true, isImplicit = true)
), /* add extra bindings here */
Some("templates")
)
)
}
)
).enablePlugins(JettyPlugin)
}
有任何想法吗?