加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – akka http编译错误

发布时间:2020-12-16 19:11:39 所属栏目:安全 来源:网络整理
导读:我是akka框架的新手,现在尝试用这个框架设置简单的webservice. 写一个简单的akka??-http应用程序: import akka.actor.ActorSystemimport akka.http.scaladsl.Httpimport akka.http.scaladsl.server.Directives._import akka.stream.ActorMaterializerimport
我是akka框架的新手,现在尝试用这个框架设置简单的webservice.
写一个简单的akka??-http应用程序:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer

import scala.io.StdIn

object MainRunner extends App {

  implicit val system = ActorSystem("mySystem")
  implicit val materializer = ActorMaterializer
  implicit val ec = system.dispatcher

  val route =
    path("hello") {
      get {
        complete("Congratulation,this is your response")
      }
    }

  val bindingFuture = Http().bindAndHandle(route,"localhost",8080)

  println(s"Server online at http://localhost:8080/nPress RETURN to stop...")
  StdIn.readLine() // let it run until user presses return
  bindingFuture
    .flatMap(_.unbind()) // trigger unbinding from the port
    .onComplete(_ => system.terminate()) // and shutdown when done
}

在编译时收到此错误:

Error:(34,44) type mismatch;
 found   : akka.http.scaladsl.server.Route
    (which expands to)  akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
 required: akka.stream.scaladsl.Flow[akka.http.scaladsl.model.HttpRequest,akka.http.scaladsl.model.HttpResponse,Any]
  val bindingFuture = Http().bindAndHandle(route,8080)

怎么解决这个问题?

解决方法

实例化ActorMaterializer时只是一个简单的错误:

implicit val materializer = ActorMaterializer

应该被替换

implicit val materializer = ActorMaterializer()

使用范围内的有效物理化器,Route和Flow之间的隐式转换[HttpRequest,HttpResponse,_]应该按预期发生,编译器应该很高兴.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读