我们正在开发基于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-compat'是一个库。您需要将其添加到build.sbt中。对于播放2.5,它将是
"org.reactivemongo" %% "reactivemongo-play-json-compat" % "0.20.3-play25"
然后从文件中删除json导入
import reactivemongo.play.json._
并替换为
import reactivemongo.play.json.compat._
对我有用