O'Reilly logo

Play Framework Cookbook - Second Edition by Giancarlo Inductivo, Alexander Reelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Serving JSON

For this recipe, we will explore how Play Framework allows us to easily convert our model objects to JSON. Being able to write web services that deliver data in the JSON data format has been a very common requirement for modern web applications. Play provides a JSON processing library that we will utilize in this recipe.

How to do it…

For Java, we need to take the following steps:

  1. Run the foo_java application with Hot-Reloading enabled.
  2. Modify the products controller in foo_java/app/controllers/Products.java by adding our product listing action:
      public static Result index() {
        return ok(Json.toJson(Product.getProducts()));
      }
  3. We need to add the following import statement for Play's JSON libraries:
        import play.libs.Json;
  4. Add a new route for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required