7

我以这种方式设置应用程序加载器:

class MyProjectApplicationLoader extends ApplicationLoader {
  def load(context: Context): Application = new ApplicationComponents(context).application
}

class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context)
  with QAControllerModule
  with play.filters.HttpFiltersComponents {

  // set up logger
  LoggerConfigurator(context.environment.classLoader).foreach {
    _.configure(context.environment, context.initialConfiguration, Map.empty)
  }

  lazy val router: Router = {
    // add the prefix string in local scope for the Routes constructor
    val prefix: String = "/"
    wire[Routes]
  }
}

但我的路线是自定义的,所以它看起来像:

routes文件:

-> /myApi/v1 v1.Routes
-> /MyHealthcheck HealthCheck.Routes

和我的v1.Routes文件:

GET    /getData    controllers.MyController.getData

所以现在当我编译项目时,我得到了这个错误:

错误:找不到类型的值:[v1.Routes] 线 [Routes]

所以我不知道如何解决这个问题,有人知道我怎样才能让加载器使用这种路由结构吗?

4

2 回答 2

4

@marcospereira 的答案是正确的,但可能代码有点错误。

该错误是由于routes文件引用导致的v1.routes- 这被编译为Routes具有类型参数的类v1.Routes

target/scala-2.12/routes/main/router您可以在和中看到生成的代码/v1

因此,要接线Router,您需要一个v1.Router实例。要创建v1.Router 实例,您首先需要连接它。

这是修复上述示例的一种方法:

lazy val router: Router = {
  // add the prefix string in local scope for the Routes constructor
  val prefix: String = "/"
  val v1Routes = wire[v1.Routes]
  wire[Routes]
}
于 2017-07-14T17:44:10.127 回答
1

您还需要连接v1.Routes. 在这里查看它是如何手动完成的:

https://www.playframework.com/documentation/2.6.x/ScalaCompileTimeDependencyInjection#Providing-a-router

这应该可以按您的预期工作:

lazy val v1Routes = wire[v1.Routes]
lazy val wiredRouter = wire[Routes]

lazy val router = wiredRouter.withPrefix("/my-prefix")

ps.:没有在实际项目中测试过。

于 2017-07-05T19:35:49.560 回答