1
votes

I am super new to the Play Framework and just started my first experiences with the play-scala-rest-api-example. I copied the post package as a new user package, added the UserRouter to the routes and appended the UserRepository to the Module configurations.

Everything works fine, it's just that I get an 404 error when visiting /v1/users. Only /v1/users/ works. For posts both with and without the slash at the end call the index route. I didn't change anything but renaming the classes and objects. Like I said I just started to play around with the framework and possibly it is something super trivial.

Here the routes file:

GET        /                    controllers.HomeController.index

->         /v1/posts            api.post.PostRouter
->         /v1/users            api.user.UserRouter

# Map static resources from the /public folder to the /assets URL path
GET        /assets/*file        controllers.Assets.at(path="/public", file)

`

2
According to play docs "You can tell the routes file to use a different router under a specific prefix by using “->” followed by the given prefix: ->" So why are you using -> and not simply repeat the GET /v1/posts GET /v1/etc?rekiem87
I expect the error to be in your user router. Have you paid attention to the prefix value stored in there ?Olivier Samyn
@OlivierSamyn, I just copied the router. The prefix is /v1/users and the routes look the same.Lando-L

2 Answers

1
votes

I found what was missing. So for any else using this tutorial and facing the same problem. You have to add your custom prefix to the app/RequestHandler.scala isREST() method. So the handlerForRequest method automatically adds a trailing slash at the end.

private def isREST(request: RequestHeader) = {
    request.uri match {
        case uri: String if uri.contains("post") => true
        case uri: String if uri.contains("user") => true
        case _ => false
    }
}

or

private def isREST(request: RequestHeader) = {
    request.uri match {
        case uri: String if uri.contains("post") | uri.contains("user") => true
        case _ => false
    }
}
0
votes

Your file should be

GET        /                    controllers.HomeController.index

GET         /v1/posts            api.post.PostRouter
GET         /v1/users            api.user.UserRouter

# Map static resources from the /public folder to the /assets URL path
GET        /assets/*file        controllers.Assets.at(path="/public", file)

The -> symbol is for some especial cases that probably you do not need right now. https://www.playframework.com/documentation/2.5.x/ScalaRouting