繁体   English   中英

如何设置Play框架ApplicationLoader和Macwire以使用自定义路由?

[英]How to set up play framework ApplicationLoader and Macwire to work with custom routes?

我这样设置应用程序加载器:

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] wire [Routes]

所以我不知道如何解决这个问题,有人知道如何使加载程序与这种路由结构一起工作?

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

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

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

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

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

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]
}

您还需要连接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。:没有在真实项目中测试它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM