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

In this recipe, we will implement a RESTful DELETE API endpoint to remove a record from 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:
        public static Result delete(String id) {
            try {
                Product product = products.get(id);
    
                if (product != null) {
                    products.remove(product);
    
                    return noContent();
                } else {
                    return notFound();
                }
            } catch (Exception e) {
                return internalServerError(e.getMessage());
            }
        }
  3. Add a new route for the newly added action in foo_java/conf/routes:
       DELETE  /api/products/:id   controllers.Products.delete(id: String)
  4. Using curl, remove ...

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