SIRD路由器无法解析路由

时间:2018-02-19 13:34:07

标签: playframework-2.0

我创建了一个SIRD路由器。但它并没有解决我的路线。以下代码中的错误是什么?

包WSRouters.User中的SIRD路由器文件UserRouter.scala

package WSRouters.User

import javax.inject.Inject
import play.api.mvc._
import play.api.routing.Router.Routes
import play.api.routing.SimpleRouter
import play.api.routing.sird._
import controllers.UserController

class UserRouter @Inject()(controller:UserController) extends SimpleRouter {

  override def routes:Routes = {
    case GET(p"users/add") => {
      println(s"add user request");
      controller.addUser();
    }
  }
}

routes.conf

#using SIRD to handle user related web services
->      /users                      WSRouters.User.UserRouter

控制器

package controllers

import play.api.mvc._
import javax.inject._

import scala.concurrent.{ExecutionContext, Future}

class UserController @Inject()(cc: ControllerComponents)(implicit exec: ExecutionContext) extends AbstractController(cc){

  def addUser = Action.async{ implicit request => Future{Ok("hello from the User controller")}}
}

当我输入路径http://localhost:9000/users/add时,我收到以下错误。尝试的路线甚至没有我的路由器。

Action Not Found

For request 'GET /users/add'
These routes have been tried, in this order:

1  GET / controllers.HomeController.index

2  GET / homecontrollers.HomeController.index

3  GET / countcontrollers.CountController.count

4  GET / messagecontrollers.AsyncController.message

5  GET /assets/$file<.+>  controllers.Assets.versioned(path:String = "/public", file:Asset)

我是否需要进行一些配置以便Play使用我的路由器?

1 个答案:

答案 0 :(得分:0)

错误是路由http://localhost:9000/users/add,路由器中的代码应该是GET(p"/add")而不是case GET(p"users/add"),因为/user已经UserRouter映射到-> /users WSRouters.User.UserRouter在routes.conf中有1}}。我的原始代码case GET(p"users/add")将解析http://localhost:9000/users/users/add