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

Receiving JSON

For this recipe, we will explore how Play Framework allow us to receive JSON objects easily and enable us to automatically convert them into model instances.

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 creation action:
      @BodyParser.Of(BodyParser.Json.class)
      public static Result postProduct() {
        JsonNode json = request().body().asJson();
        String sku = json.findPath("sku").textValue();
        String title = json.findPath("title").textValue();
    
        Product p = new Product();
        p.sku = sku;
        p.title = title;
        Product.add(p);
        return created(Json.toJson(p));
      }
  3. We need to add the following import ...

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