singhakash
singhakash

Reputation: 7919

Optional parameter with url rewriting in play 2.4

I want to use the optional parameter in play 2.4 java. After reading docs, I add the following routes:

GET  /api/users/:page  controllers.EmployeeController.getUsers(page:Int,pageSize:Int ?= 10)

I want the page size to be optional in this url. The url I can access are:

/api/users/1
/api/users/1?pageSize=5

But the problem is that I want to use the second url like:

/api/users/1/5

Which currently gave me action not found.

Is there a way I can achieve this?

Note: I don't want to create a separated url as @Salem mentioned. I want to use this single url in routes file.

Upvotes: 1

Views: 99

Answers (1)

Salem
Salem

Reputation: 12986

Just add a second mapping to the same controller (this supposes you want to use "10" as the pageSize value if none is provided)

GET     /api/users/:page              controllers.EmployeeController.getUsers(page:Int,pageSize:Int = 10)
GET     /api/users/:page/:pageSize    controllers.EmployeeController.getUsers(page:Int,pageSize:Int)

Note that the second parameter if getUsers is not optional anymore

Upvotes: 1

Related Questions