我有一个返回字符串的 Akka HTTP 服务,如下所示:
val route1: Route = {
path("hello") {
get{
complete{
println("Inside r1")
"You just accessed hello"
}
}
}
}
我试图了解 map 和 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() 没有按预期工作?