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

For this recipe, we will create the RESTful GET endpoint, which will return a collection of JSON objects.

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 the products controller in foo_java/app/controllers/Products.java by adding the following action method:
        public static Result index() {
            return ok(toJson(products));
        }  
  3. Add a new route entry for the newly added action in foo_java/conf/routes:
        GET     /api/products       controllers.Products.index
  4. Request the new route and examine the response headers to confirm our modifications to the HTTP response header:
        $ curl -v http://localhost:9000/api/products
     * Hostname was NOT found in DNS ...

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