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 ...

Get Play Framework Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.