【发布时间】:2017-02-23 12:48:47
【问题描述】:
这是完整的代码:
package com.example.controller
import spray.routing.{HttpService, Route}
import spray.http.StatusCodes
import scala.util.{Failure, Success}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
trait RentController extends HttpService{
var books: List[String] = List("1","2","3","4")
def rentBook(intNum: Int): Future[Unit] = Future[Unit] {
val book = books.find(p => p.equals(intNum.toString))
if(book.head.equals("4")) throw new UnsupportedOperationException()
}
def giveBackBook(intNum: Int): String = {
val book = books.find(p => p.equals(intNum.toString))
book.head
}
val rent: Route =
pathPrefix("rent") {
path("book" / IntNumber) { intNum =>
get {
onComplete(rentBook(intNum)) {
case Success(_) => complete(StatusCodes.OK)
case Failure(_) => complete(StatusCodes.Accepted)
}
}
}
}
val giveBack: Route =
pathPrefix("giveBack") {
path("book" / IntNumber) { intNum =>
get {
onSuccess(Future.successful(giveBackBook(intNum))) {
extraction: String => complete(extraction)
}
}
}
}
}
奇怪的是,他只强调了 OnSuccess,但 OnComplete 显然对他来说是正确的。代码有效,但我想知道我是否可能在某个地方犯了一些错误。所以请告诉我是否缺少一些导入,或者这只是 IntelliJ 错误。
编辑
我更改了代码,现在函数rendBook和giveBackBook逻辑上不正确,并且没有做太多,但你应该很容易重现它
【问题讨论】:
-
你能发一个minimal reproducible example吗?复制粘贴对任何人都不起作用。
-
Bug report is welcome 附有一个要复制的示例项目。
-
@YuvalItzchakov 如果您仔细观察,这是最小、完整和可验证的示例:) 两条路线,一条有效,一条无效,所有进口 :) 我认为需要这段代码的每一段来验证我的问题:)
-
@CrazyCoder 我知道,但我想从一些 Scala 专家那里获得专业知识,如果我没有犯任何错误的话 :)
-
@KamilBanaszczyk
import com.example.repository.BookRepository将如何为我编译?
标签: scala intellij-idea akka spray