首页 > 解决方案 > 为什么akka http可以使用方法路径而不扩展特征?

问题描述

通过 akka http 示例:https ://doc.akka.io/docs/akka-http/current/introduction.html#using-akka-http

object WebServer {
val route =
      path("hello") {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
        }
      }

路径是 trait PathDirectives 中定义的方法:

trait PathDirectives extends PathMatchers with ImplicitPathMatcherConstruction with ToNameReceptacleEnhancements {

def path[L](pm: PathMatcher[L]): Directive[L] = pathPrefix(pm ~ PathEnd)

那么WebServer不会使用方法路径,它必须扩展特征PathDirectives?为什么不需要?

谢谢!

标签: scalaakka

解决方案


如果您使用正确的导入语句,则无需扩展任何内容。

import akka.http.scaladsl.server.Directives._

这带来了对象中定义的所有方法Directives

object Directives extends Directives

trait Directives extends RouteConcatenation
  with BasicDirectives
  with CacheConditionDirectives
  with CookieDirectives
  with DebuggingDirectives
  with CodingDirectives
  with ExecutionDirectives
  with FileAndResourceDirectives
  with FileUploadDirectives
  with FormFieldDirectives
  with FutureDirectives
  with HeaderDirectives
  with HostDirectives
  with MarshallingDirectives
  with MethodDirectives
  with MiscDirectives
  with ParameterDirectives
  with TimeoutDirectives
  with PathDirectives
  with RangeDirectives
  with RespondWithDirectives
  with RouteDirectives
  with SchemeDirectives
  with SecurityDirectives
  with WebSocketDirectives
  with FramedEntityStreamingDirectives

其中也PathDirectives包括


推荐阅读