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

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

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

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

路线文件:

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

和我的v1.Routes文件:

GET    /getData    controllers.MyController.getData

所以现在当我编译项目时,我收到此错误:

Error: Cannot find a value of type: [v1.Routes]
wire[Routes]

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

解决方法

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

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

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

因此,要连接路由器,您需要一个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]
}

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...