首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置play框架ApplicationLoader和Macwire来处理自定义路由?

如何设置play框架ApplicationLoader和Macwire来处理自定义路由?
EN

Stack Overflow用户
提问于 2017-07-04 18:27:18
回答 2查看 1.2K关注 0票数 7

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

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
-> /myApi/v1 v1.Routes
-> /MyHealthcheck HealthCheck.Routes

我的v1.Routes文件:

代码语言:javascript
复制
GET    /getData    controllers.MyController.getData

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

错误:找不到类型: v1.Routes wireRoutes的值

所以我不知道如何解决这个问题,有人知道我怎样才能让装载机和这个路线的结构一起工作吗?

EN

回答 2

Stack Overflow用户

发布于 2017-07-14 17:44:10

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

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

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

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

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

代码语言:javascript
复制
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]
}
票数 4
EN

Stack Overflow用户

发布于 2017-07-05 19:35:49

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

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

这应该如您所期望的那样起作用:

代码语言:javascript
复制
lazy val v1Routes = wire[v1.Routes]
lazy val wiredRouter = wire[Routes]

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

ps.:没有在真正的项目中测试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44912215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档