[英]How to set up play framework ApplicationLoader and Macwire to work with custom routes?
I set up the application loader this way: 我这样设置应用程序加载器:
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]
}
}
but my routes is custom, so it looks like: 但我的路线是自定义的,所以它看起来像:
the routes
file: routes
文件:
-> /myApi/v1 v1.Routes
-> /MyHealthcheck HealthCheck.Routes
and my v1.Routes
file: 和我的
v1.Routes
文件:
GET /getData controllers.MyController.getData
so now when I compile the project I get this error: 所以现在当我编译项目时,我收到此错误:
Error: Cannot find a value of type: [v1.Routes] wire[Routes]
错误:找不到类型的值:[v1.Routes] wire [Routes]
so im not sure how to fix this, does someone know how can I make the loader to work with this structure of routes? 所以我不知道如何解决这个问题,有人知道如何使加载程序与这种路由结构一起工作?
The answer by @marcospereira is correct, but maybe the code is slightly wrong. @marcospereira的答案是正确的,但也许代码有点错误。
The error is due to the routes
file referencing v1.routes
- this is compiled to a Routes
class having a parameter of type v1.Routes
. 该错误是由
routes
文件引用v1.routes
- 这被编译为具有类型为v1.Routes
的参数的Routes
类。
You can see the generated code in target/scala-2.12/routes/main/router
and /v1
. 您可以在
target/scala-2.12/routes/main/router
和/v1
看到生成的代码。
Hence to wire Router
, you need a v1.Router
instance. 因此,要连接
Router
,您需要一个v1.Router
实例。 To create a v1.Route
r instance, you need first to wire it. 要创建
v1.Route
r实例,首先需要连接它。
Here's one way to fix the above example: 以下是修复上述示例的一种方法:
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]
}
You also need to wire the v1.Routes
. 您还需要连接
v1.Routes
。 See how it is done manually here: 在此处查看如何手动完成:
https://www.playframework.com/documentation/2.6.x/ScalaCompileTimeDependencyInjection#Providing-a-router https://www.playframework.com/documentation/2.6.x/ScalaCompileTimeDependencyInjection#Providing-a-router
This should work as you expect: 这应该按预期工作:
lazy val v1Routes = wire[v1.Routes]
lazy val wiredRouter = wire[Routes]
lazy val router = wiredRouter.withPrefix("/my-prefix")
ps.: didn't test it in a real project. ps。:没有在真实项目中测试它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.