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.
For Java, we need to perform the following steps:
Run the
foo_java
application with Hot-Reloading enabled:activator "~run"
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(); if (form.hasErrors()) { return badRequest(form.errorsAsJson()); ...