【发布时间】:2019-12-22 15:23:23
【问题描述】:
我们正在开发一个基于 Play Framework (Scala) 的应用程序,并使用 ReactiveMongo 将数据存储到 Mongo 数据库中。
将 play2-reactivemongo 从 0.18.4 版本升级到 0.19.5 后,我们收到了很多弃用警告,我可以理解如何修复它们。
这是我收到的弃用警告:
package object json in package json is deprecated (since 0.19.2): Will be replaced `reactivemongo.play.json.compat._` from `reactivemongo-play-json-compat`
例如,我有一个 Nonce 对象:
package models
import java.security.SecureRandom
import java.util.Base64
import org.joda.time.DateTime
import play.api.libs.json._
import reactivemongo.bson.{ BSONDateTime, BSONObjectID }
case class Nonce(_id: Option[BSONObjectID], value: String, expiresAt: BSONDateTime, ttl: Option[Int])
object Nonce {
import reactivemongo.play.json._
// Generate a Nonce object with a value matching the base64url encoding without the trailing '=' as defined section two of [RFC7515]
def apply(entropy: Int, ttl: Int): Nonce = {
val pattern = "([^=]*).*".r
val random = new SecureRandom()
val input = new Array[Byte](entropy)
random.nextBytes(input)
val pattern(nonce) = Base64.getUrlEncoder.encodeToString(input)
Nonce(None, nonce, BSONDateTime(DateTime.now.plusSeconds(ttl).getMillis), Some(ttl))
}
implicit val nonceFormat: OFormat[Nonce] = Json.format[Nonce]
}
还有一个存储 Nonce 对象的服务类:
package models
import javax.inject.{ Inject, Singleton }
import play.api.Configuration
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.WriteConcern
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.indexes.{ Index, IndexType }
import reactivemongo.bson.BSONDocument
import reactivemongo.play.json._
import reactivemongo.play.json.collection.JSONCollection
import scala.concurrent.{ ExecutionContext, Future }
@Singleton
class NonceService @Inject() (implicit ec: ExecutionContext, configuration: Configuration, reactiveMongoApi: ReactiveMongoApi) {
// Specifying a TTL on the nonces collection for the element expiresAt
private val ttlIndex = Index(Seq("expiresAt" -> IndexType.Ascending), name = Some("expiresAt_"), options = BSONDocument("expireAfterSeconds" -> 0))
val noncesCollection = reactiveMongoApi.database.map(_.collection[JSONCollection]("nonces"))
def add(nonce: Nonce): Future[WriteResult] = {
noncesCollection.flatMap { col =>
col.insert(ordered = false).one(nonce) andThen { case _ => col.indexesManager.ensure(ttlIndex) }
}
}
def get(value: String): Future[Option[Nonce]] = {
val query = BSONDocument("value" -> value)
noncesCollection.flatMap(_.findAndRemove(query, None, None, WriteConcern.Journaled, None, None, Seq.empty).map(_.result[Nonce]))
}
}
编译器提示以下警告:
[warn] ... Nonce.scala:26:57: package object json in package json is deprecated (since 0.19.2): Will be replaced `reactivemongo.play.json.compat._` from `reactivemongo-play-json-compat`
[warn] implicit val nonceFormat: OFormat[Nonce] = Json.format[Nonce]
[warn] ...NonceService.scala:31:45: package object json in package json is deprecated (since 0.19.2): Will be replaced `reactivemongo.play.json.compat._` from `reactivemongo-play-json-compat`
[warn] noncesCollection.flatMap(_.findAndRemove(query, None, None, WriteConcern.Journaled, None, None, Seq.empty).map(_.result[Nonce]))
[warn] ^
【问题讨论】:
-
你有什么尝试删除过时的代码(仍然有代码示例)?
-
我们尝试了几件事,例如将导入的 'reactivemongo.play.json._' 替换为 'reactivemongo.play.json.compat._',但我们仍然收到很多警告。当我阅读文档时,我很困惑。文档基本上说我们应该使用 reactivemongo.api.bson 类而不是 reactivemongo.bson 类,但是文档中的代码没有反映这一点....说实话,我完全迷失了,不知道是什么我应该这样做。
-
这有什么更新吗?
标签: json scala playframework play-reactivemongo reactivemongo-play-json