这是我的 common.routes
文件:
GET /emails/:deflated controllers.common.Emails.viewInBrowser(deflated)
...这是我的 Controller :
package controllers.common
object Emails extends Controller {
def viewInBrowser(deflated: String) = Action { implicit request =>
GZip.inflate(deflated) match {
case Success(inflated) => Ok(Html(inflated))
case Failure(e) => {
Logger.warn(s"error inflating email from url", e)
val support = configuration.getString("application.emails.support")
BadRequest(views.html.badRequest(Messages("common.error.invalidOrMalformedUrl", support)))
}
}
}
}
上面的代码工作正常......但是如果我像这样反向路由
import controllers.common.routes
...
val sslEnabled = configuration.getBoolean("ssl").getOrElse(false)
val emailUrl = routes.Emails.viewInBrowser(true).absoluteURL(sslEnabled)
...我总是出现以下异常:
java.lang.NoSuchFieldError: Emails
at utils.common.RoutesHelper$.emailUrl(RoutesHelper.scala:51)
at services.common.DefaultEmailComponent$DefaultRichBody.apply(EmailComponent.scala:64)
at services.common.DefaultEmailServiceComponent$DefaultEmailService.sendEmail(EmailServiceComponent.scala:81)
at utils.auth.EmailHelper$.sendUserVerificationEmail(EmailHelper.scala:32)
at controllers.auth.Users$$anonfun$create$1$$anonfun$8$$anonfun$apply$60$$anonfun$apply$61.apply(Users.scala:381)
at controllers.auth.Users$$anonfun$create$1$$anonfun$8$$anonfun$apply$60$$anonfun$apply$61.apply(Users.scala:379)
at scala.util.Success$$anonfun$map$1.apply(Try.scala:206)[ERROR] [05/30/2014 16:35:05.350] [play-akka.actor.default-dispatcher-4] [ActorSystem(play)] Uncaught error from thread [play-akka.actor.default-dispatcher-4] shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled
有什么想法吗?我验证了我能做的一切(配置文件、路由、 Controller )……但无法找出问题所在。我正在使用 Play 2.2.3。
更新
非常奇怪的是,我试图将我的 Emails
Controller 移动到另一个包(即 models
),在这种情况下,反向路由再次起作用。 Play 中是否存在错误?
最佳答案
据我了解,如果在根项目和其中一个子项目中的同一包中定义了路由,则生成的子项目 routes
对象会隐藏根项目中的同一对象,并且所有引用旧 routes
对象的代码都会出错。
例如如果根项目中的 routes
文件包含一个路由
GET /path mypackage.MyController.someAction
subproject.routes
文件包含一个路由
GET /otherPath mypackage.MyOtherController.otherAction
两个项目都在构建期间生成了 mypackage.routes
对象,其中一个在运行时隐藏了另一个。
解决方案是在其自己的包中定义所有未在根项目中呈现的子项目路由。
关于playframework - Play Framework : NoSuchFieldError when reverse routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957426/