带Play的MongoController

时间:2018-01-16 08:05:49

标签: scala playframework reactivemongo play-reactivemongo playframework-2.6

我正在使用Play!框架与当前版本,我试图通过ReactiveMongo与MongoDB集成,但我得到以下错误“非法继承;超类对象不是mixin trait MongoController的超类AbstractController的子类”

这是我的控制器代码;

package controllers

import javax.inject._

import org.slf4j.LoggerFactory
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.mvc._
import play.modules.reactivemongo._
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.{JSONCollection, _}
import reactivemongo.api.Cursor

import scala.concurrent.Future

@Singleton
class HomeController  @Inject() (val reactiveMongoApi: ReactiveMongoApi)
extends Controller with MongoController with ReactiveMongoComponents {

val logger = LoggerFactory.getLogger(this.getClass)

def collection: JSONCollection = db.collection[JSONCollection]("scrawler1")

def index = Action {
Ok(views.html.index(""))
}

def query = Action.async { request =>
val body = request.body
val query = body.asFormUrlEncoded.get("query")

val querySet = query.toSet[String]

val keywords = querySet.flatMap({ string: String =>
  string.split(" ")
})

val searchQuery = Json.obj("keywords" -> Json.obj("$in" -> Json.toJson(keywords)))
logger.info(s"Internal query from client: $searchQuery")
val cursor: Cursor[JsObject] = collection.find(searchQuery).cursor[JsObject]

val result: Future[List[JsObject]] = cursor.collect[List]()

val resultJson: Future[JsValue] =
  result.map { persons => Json.toJson(persons) }

resultJson.map { results =>
  val title = results \\ "title"
  val url = results \\ "url"
  val description = results \\ "body"
  val queryData: Seq[((JsValue, JsValue), JsValue)] = title.zip(url).zip(description)
  Ok(views.html.test(queryData))
}
}
}

0 个答案:

没有答案