【问题标题】:Scala/Play ClassCastException: Cannot cast java.lang.Integer to java.lang.Long]Scala/Play ClassCastException:无法将 java.lang.Integer 转换为 java.lang.Long]
【发布时间】:2017-05-10 13:14:06
【问题描述】:

这个问题我已经有一段时间了,游戏将面临“无法将整数转换为长”异常。

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: Cannot cast java.lang.Integer to java.lang.Long]]

这发生在下面代码片段中“msgService.processMsg”调用的那一行:

def msgHandler(params: String) = auth.SecuredAction.async { implicit request =>
  usersService.findUserByEmail(request.identity.email) flatMap {
    case Some(ue) =>
      val p = Json.parse(params.toLowerCase())
      val op = (p \ "op").get(0).as[JsString].value
      val prodId = (p \ "prodid").get(0).as[JsString].value
      op match {
        case "get-inventory" =>
          msgService.processMsg(prodId.toLong, ue) flatMap { case res =>
            Future.successful(Redirect(controllers.www.routes.Dashboard.dashboard))
          }
        case _ =>
          Future.successful(Redirect(controllers.www.routes.Dashboard.dashboard))
      }
    case None =>
      Future.successful(Redirect(controllers.www.routes.Dashboard.dashboard))
  }
}

...
def amend(inventory: Inventory): Future[Long] = {
  db.run(inventorys.withFilter(_.id === inventory.id).update(inventory)).mapTo[Long]
} 

def processMsg(prodId: Long, user: UserEntry): Future[Long] = {
  findInventoryByProdIdAndUserId(prodId, user.id) flatMap {
    case Some(inventory) =>
      var updInventory = inventory.copy(status = InventoryStatus.UPDATED)
      Logger.debug(s"Updating inventory: ${updInventory}")
      amend(updInventory)
    case None =>
      throw new Exception("No inventory entry found!!")
  }
}

如果我删除 flatMap 并保留它:

msgService.processMsg(prodId.toLong, ue)

然后我看不到错误。

另外,如果我返回 Future[Int] 而不是 Future[Inventory],我不会看到错误。

我在代码的其他几个地方使用过这种模式,但到目前为止还无法找出导致此问题的原因。如何摆脱这个错误?

完整的错误跟踪:

! @7416eon6i - Internal server error, for (GET) [/processInviteResponse/%7B%22domain%22:%5B%22invites%22%5D,%22jobId%22:%5B%221%22%5D,%22op%22:%5B%22cand-accept%22%5D%7D] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: Cannot cast java.lang.Integer to java.lang.Long]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40)
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248)
Caused by: java.lang.ClassCastException: Cannot cast java.lang.Integer to java.lang.Long
    at java.lang.Class.cast(Class.java:3369)
    at scala.concurrent.Future$$anonfun$mapTo$1.apply(Future.scala:405)
    at scala.util.Success$$anonfun$map$1.apply(Try.scala:237)
    at scala.util.Try$.apply(Try.scala:192)
    at scala.util.Success.map(Try.scala:237)
    at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
    at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
    at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:78)
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@476e0189
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@1141e6d7
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@69368153
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@27b15ff
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@6a1fb0f1
2017-05-08 21:27:37,453 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@3884d6d5

【问题讨论】:

  • processMsg的结果是什么类型,能提供processMsg的代码吗?
  • @RaKa 已更新 - 谢谢

标签: scala playframework


【解决方案1】:

将修改功能更改为后,问题就解决了

def amend(inventory: Inventory): Future[Int] = {
  db.run(inventorys.withFilter(_.id === 
      inventory.id).update(inventory)).mapTo[Int]
} 

甚至更好

def amend(inventory: Inventory) = {
  db.run(inventorys.withFilter(_.id === 
      inventory.id).update(inventory))
} 

相信返回的 Int 是更新的行数,但没有引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2019-12-19
    • 1970-01-01
    • 2011-10-22
    相关资源
    最近更新 更多