2017-05-09 93 views
0

我一直有這個問題一段時間,戲劇將面臨一個「不能投整數到長期」異常。Scala/Play ClassCastException:無法投射java.lang.Integer到java.lang.Long]

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 = [email protected] 
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = [email protected] 
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = [email protected] 
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = [email protected] 
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = [email protected] 
2017-05-08 21:27:37,453 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = [email protected] 
+0

什麼是processMsg的結果類型,你能提供processMsg的代碼嗎? –

+0

@RaKa更新 - 謝謝 – srvy

回答

0

一次我改變了修正功能

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)) 
} 

相信返回詮釋是多少更新的行雖然沒有參考。

相關問題