HTTP parameters and routes in Play Framework


HTTP parameters and routes in Play Framework

E-commerce store with static URLs would kill even the most powerful server. It's why, to resolve dynamic behavior, we use HTTP parameters. GET ones are particularly useful.

This article will present how to work with GET parameters in Play Framework. At the begin, we'll see how Play works with query string parameters. The second part of the article will present how GET parameters can be used in the routes definitions.

Query string parameters in Play Framework

We'll start by show how to work with query string parameter. Query string parameters are the parameters placed after "?" in the URL. For example, in, query string parameters will be param=A and name=B. In Play we can get them thanks to play.mvc.Http.Request and its queryString() method. It returns a Map<String, String[]> representing query string params. We can test it in our index page by modifying the action method:

public static Result index() {
  Map<String, String[]> params = ctx().request().queryString();
  if (params.containsKey("categoryId")) {
    try {
      CategoryService categoryService = (CategoryService) ServicesInstances.CATEGORY_SERVICE.getService();
      Category category = categoryService.getById(FromStringConverter.toInt(params.get("categoryId")[0]));
      Logger.debug("Found category: "+category);
    } catch (Exception e) {
      // TODO : redirect to error page
  return ok(index.render("Your new application is ready."));

To see how it works, you can visit http://localhost:9000/?categoryId=1. Normally, you should see the same result as in the article about database and JPA in Play Framework.

Routes with GET parameters in Play Framework

Routing is a little bit more fun than query string handling. However, they have some subtleties and to better understand them, we'll present different routing strategies in following table:

See routes table

This articles introduced us to route management in Play Framework. We discovered how to work with query string parameters. We also saw different methods to define SEO-friendly URLs thanks to routes file.

Share on: