我们正在开发一个基于 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] ^