簡體   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