我有它返回一個字符串,如下所示的阿卡HTTP服務:爲什麼我的地圖()不工作?
val route1: Route = {
path("hello") {
get{
complete{
println("Inside r1")
"You just accessed hello"
}
}
}
}
我想了解地圖和flatMap
例如之間的差別,下面的代碼給我的結果作爲預計:
val future1: Future[String] =
Http()
.singleRequest(
HttpRequest(method = HttpMethods.GET,
uri = "http://localhost:8187/hello"))
.flatMap(testFlatFunc)
def testFlatFunc(x: HttpResponse): Future[String] = {
Unmarshal(x).to[String]
}
但是,如果我試圖用地圖來取代它,下面我得到的輸出爲FulfilledFuture(You just accessed hello)
val future1: Future[String] = Http()
.singleRequest(
HttpRequest(method = HttpMethods.GET,
uri = "http://localhost:8187/hello"))
.map(testFunc)
def testFunc(x: HttpResponse): String={
Unmarshal(x).to[String].toString
}
爲什麼我的map()不能按預期工作?