2

我正在尝试在运行时使用特征聚合路由,到目前为止我有

object SMController {
  def aggregateRoutes(actorSystem: ActorSystem): List[Route] = {
    val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
    val reflections = new Reflections("com.example.api")
    val subclasses = reflections.getSubTypesOf(classOf[Routable])
    val routesList = new ListBuffer[Route]()

    for (i <- subclasses) {
      val module = runtimeMirror.staticModule(i.getName)
      val obj = runtimeMirror.reflectModule(module)
      val someTrait: Routable = obj.instance.asInstanceOf[Routable]
      routesList += someTrait.getAllRoutes(actorSystem)
    }

    routesList.toList
  }
}

显然上面的代码不起作用,因为项目列表不能传递给Http().bindAndHandle.

所以我的问题是,如何将 解析List[Routes]Http().bindAndHandle接受,或者如何从 的子类动态加载路由Routable

4

1 回答 1

5

foldLeft: 我已经设法 foldLeft 路线并合并所有路线,如下所示

val routes = SMController.aggregateRoutes(system)
val bindingFuture = Http().bindAndHandle(
        routes.tail.foldLeft(routes.head)((a, b) => a ~ b), "localhost", 8080)

减少左:

routes.reduceLeft(_ ~ _)
于 2016-03-27T12:56:53.580 回答