Configuring request parameters

Many applications use additional parameters along with RESTful HTTP GET requests to obtain required information. Play supports configuring these request parameters as well.

Supposing we have a request to search users by their name, we could define this as follows:

GET           /api/search/user    controllers.UserController.search(name)

Therefore, we wouldn't need to get the parameters from the request in the action. We could let Play handle acquiring the parameters from the request and passing them to the action.

What do we do when the request parameters are optional? For example, what happens if we allow a search of users by their name where lastName is optional.

We can specify Option as the type for this request parameter. Therefore, the route definition would be similar to the following:

GET           /api/search/user    controllers.UserController.search(firstName:String, lastName:Option[String])

In addition to this, we can also specify the default value, if any, for request parameters. Suppose we had a limit parameter for the search request as well. In this case, if we wish to set the default value as 10, the route definition would be as follows:

GET           /api/search/user    controllers.UserController.search(firstName:String, lastName:Option[String], limit:Int ?= 10)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.114.19