Play framework routes, and scala predef values

纵饮孤独 提交于 2019-12-03 04:10:45

You can must use parameter with default value:

GET  /posting/          controllers.posting.BlogController.allPosts(number: Int = 1)
GET  /posting/:number   controllers.posting.BlogController.allPosts(number: Int)

You may not use overloaded methods allPosts(Int) and allPosts. Since you declared allPosts with two parameters with default values, Scala sees this method as 4 different methods. You may only use one of them.

You must have all the parameters defined for same function. In your case allPosts. Give a default value for routes where you don't need that parameter.

GET  /posting/          controllers.posting.BlogController.allPosts(number: Int ?= 0)

GET /posting/:number controllers.posting.BlogController.allPosts(number: Int)

You can also use Optional Parameters. Something like

GET  /posting/          controllers.posting.BlogController.allPosts(number: Option[Int])
GET  /posting/:number   controllers.posting.BlogController.allPosts(number: Int)

Then you can call this with or without a parameter in the query:

/posting?number=1
/posting

Make sure you declare an Option as well in your controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!