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.

Looking for a better data engineering position and skills?

You have been working as a data engineer but feel stuck? You don't have any new challenges and are still writing the same jobs all over again? You have now different options. You can try to look for a new job, now or later, or learn from the others! "Become a Better Data Engineer" initiative is one of these places where you can find online learning resources where the theory meets the practice. They will help you prepare maybe for the next job, or at least, improve your current skillset without looking for something else.

👉 I'm interested in improving my data engineering skillset

See you there, Bartosz

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:

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.

If you liked it, you should read:

📚 Newsletter Get new posts, recommended reading and other exclusive information every week. SPAM free - no 3rd party ads, only the information about waitingforcode!