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

Creating a PUT API endpoint

In this recipe, we will implement a RESTful PUT API endpoint using Play 2.0 to update an existing record in our data store.

How to do it…

For Java, we need to perform the following steps:

  1. Run the foo_java application with Hot-Reloading enabled:
        activator "~run"
    
  2. Modify foo_java/app/controllers/Products.java by adding the following action:
     @BodyParser.Of(BodyParser.Json.class) public static Result edit(String id) { try { Product product = products.get(id); if (product != null) { Form<Product> form = Form.form(Product.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(form.errorsAsJson()); } else { Product productForm = form.get(); product.setTitle(productForm.getTitle()); products.put(product.getSku(), ...

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