本文介绍了尝试在Play Framework中将ReactiveMongo + BSON重写为JSON时收到错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Json库替换Bson库.这是有效的原始代码.

I tried to use Json library to replace Bson library.This is the original code which works.

case class City(name: String, population: Int)

object City {
  implicit val reader = Macros.reader[City]
}

@Singleton
class CityController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext) extends Controller with MongoController with ReactiveMongoComponents {
  def findByMinPopulation(minPop: Int) = Action.async {
    import citiesBSON.BatchCommands.AggregationFramework.Match
    val futureCitiesList: Future[List[City]] = citiesBSON.aggregate(
      Match(BSONDocument("population" -> BSONDocument("$gte" -> minPop)))
    ).map(_.head[City])
    futureCitiesList.map { cities =>
      Ok(Json.toJson(cities))
    }
  }
}

这是使用Json进行编译的代码,但在运行时会出错.

And this is the code using Json which compiles but get an error while running.

case class City(name: String, population: Int)

object City {
  implicit val formatter = Json.format[City]
}

@Singleton
class CityController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext) extends Controller with MongoController with ReactiveMongoComponents {
  def findByMinPopulation(minPop: Int) = Action.async {
    import cities.BatchCommands.AggregationFramework.Match
    val futureCitiesList: Future[List[City]] = cities.aggregate(
        Match(Json.obj("population" -> Json.obj("$gte" -> minPop)))
      ).map(_.head[City])
    futureCitiesList.map { cities =>
      Ok(Json.toJson(cities))
    }
  }
}

这是我收到的错误消息:

And this is the error message I've got:

推荐答案

正如@ andrey.ladniy所说,此问题已在版本0.12.0-SNAPSHOT中修复.要使用此版本,请更新build.sbt文件并添加以下内容:

As @andrey.ladniy said, this issue got fixed in version 0.12.0-SNAPSHOT. To use this version, update build.sbt file and add this:

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.12.0-SNAPSHOT"
)

并清除常春藤缓存.要在IntelliJ IDEA中执行此操作,只需选择文件"->使缓存无效/重新启动",然后选择使无效并重新启动".

And clear ivy cache. To do this in IntelliJ IDEA, just select "File" -> "Invalidate Caches / Restart", and select "Invalidate and Restart".

起初我没有清除缓存,即使更新到新版本后也遇到了同样的错误.

I didn't clear cache at first and got the same error even after updated to the new version.

这篇关于尝试在Play Framework中将ReactiveMongo + BSON重写为JSON时收到错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 06:49