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 POST API endpoint

In this recipe, we will explore how to use Play 2.0 to create a RESTful POST endpoint to add new records to our API.

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. Create a new products controller in foo_java/app/controllers/Products.java with the following content:
     package controllers; import java.util.*; import play.data.Form; import play.mvc.*; import models.Product; import static play.libs.Json.toJson; public class Products extends Controller { public static Map<String, Product> products = new HashMap<String, Product>(); @BodyParser.Of(BodyParser.Json.class) public static Result create() { try { Form<Product> form = Form.form(Product.class).bindFromRequest(); ...

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