【发布时间】:2016-03-22 19:50:24
【问题描述】:
我正在查看 akka-http 文档,并在那里找到如何使用 HttpResponses 通过低级服务器 api 提供 html 内容。但是,我找不到任何关于如何提供 html 内容的好例子,应该在浏览器中正确表示。我发现并且可以工作的唯一事情是当它提供如下字符串内容时。我发现了一个例子:
imports akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
但我看不到 scaladsl 包含编组器(它包含编组)
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.http.scaladsl.server.Directives._
import akka.actor.ActorSystem
object HttpAkka extends App{
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val route = get {
pathEndOrSingleSlash {
complete("<h1>Hello</h1>")
}
}
Http().bindAndHandle(route, "localhost", 8080)
}
在这里Akka-http: Accept and Content-type handling找到一个相关问题
我没有完全理解该链接中的答案,但尝试了以下操作(我最终得到了这个工作..):
complete {
HttpResponse(entity=HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say Hello</h1>"))
}
还有:
complete {
respondWithHeader (RawHeader("Content-Type", "text/html(UFT-8"))
"<h1> Say hello</h1>"
}
【问题讨论】:
-
两者都不能像现在这样工作。对于 mustafas 建议,需要添加一个 xml 库,对于 kostya
s suggestion I get a not found value error (if I use ContentTypes.text/html(UTF8)`,我得到一个类型不匹配。