我正在尝试使用以下方法创建一个向 mongoddb 插入数据的发布请求: 1. sbt 0.13.6 2. play 2.10 3. scala 2.11.2 4. play2-reactivamongo 0.10.2 5. mongodb 2.6.4
通过json发布数据,并为模型创建案例类,并使用JSPath将json转换为实体类。
这是我的示例代码:
def inserTransaction = Action(parser.json) { implicit request =>
val json = request.body
val data = json.as[Transaction]
Logger.info(data.toString)
val future = collection.insert(data.copy(id = Option[BSONObjectID](BSONObjectID.generate)))
var result = ""
future.onComplete {
case Failure(t) => result = "An error has occured: " + t.getMessage
case Success(post) => result = "success"
}
Ok(result)
}
我已经看到一些使用 Action.sync 在控制器中处理异步的示例代码,但是当我尝试使用 Action.sync 时,我的 Intellij IDE 检测到错误“无法将 Action.sync 解析为签名”,我试图改变像这样的函数的结果
future.onComplete {
case Failure(t) => Ok("An error has occured: " + t.getMessage)
case Success(post) => Ok("success")
}
所以我决定使用 Action(parser.json) ,但是来自 activator play 的问题是告诉我应该在我的代码中使用“import play.api.libs.concurrent.Execution.Implicits._”。但是当我导入库时,出现了一个新错误:
! Internal server error, for (POST) [/insertdata] ->
java.lang.ExceptionInInitializerError: null ....
Caused by: play.api.PlayException: ReactiveMongoPlugin Error[The ReactiveMongoPlugin has not been
initialized! Please edit your conf/play.plugins file and add the following line....
当我尝试重新加载请求时,它显示了另一个错误:
! Internal server error, for (POST) [/api/insertdata] ->
java.lang.NoClassDefFoundError: Could not initialize class controllers.TransactionController$
[error] application - Error while rendering default error page
scala.MatchError: java.lang.NoClassDefFoundError: Could not initialize class
controllers.TransactionController$ (of class java.lang.NoClassDefFoundError)
有人对我的问题有任何解决方案吗?