Home Java/J2EE Rest Web Services in Play Framework
formats

Rest Web Services in Play Framework

Published on April 22, 2014, by in Java/J2EE.

Hi Play Framework Experts,

For Creating Rest Web Services in the Play Framework you have follow the given below steps so you can easily crate the web service in the play framework.

Key Features of Rest Web Services in Play Framework

  • You do not need to add any external jar file in the project
  • You do not implement any special mechanism for converting Json array and Json object
  • Play Framework easily converts list of data into Json object and Json array
  • We have easily developed PoJo base rest web service in the Play Framework using model class

 

Follow the below steps to create Rest Web service

  1. Create Project in the play Framework, we have created RestDemo Project in the Play
  2. Create One Controller class in the controller Package under the app folder of the project, we have created one controller class RestWebService
    • package controllers;

      import play.mvc.Controller;

      public class RestWebService extends Controller {

      }

  3. Create one model class which we have use to send Json response , we have created one model class UserDetail
  4. Create one method in the controller class, all the rest response logic are write in this method, we have created one method getUser()
    • public static Result getUser() {
      response().setContentType(“application/json”);
      List<UserDetail>userDetails = new ArrayList<UserDetail>();
      UserDetail useDetail = null;
      useDetail = new UserDetail();
      useDetail.setId(“1”);
      useDetail.setName(“chirag”);
      useDetail.setFname(“gurav”);
      useDetail.setLname(“M”);
      useDetail.setEmail(“chiraggurav@gmail.com”);
      useDetail.setAddress(“Gujarat”);
      useDetail.setMobileno(“9998342122”);
      userDetails.add(useDetail);
      return ok(Json.toJson(userDetails));
      }

  5. Now you have Configure the routes file, This file defines all application routes
    • GET  /RestWebService/getUser controllers.RestWebService.getUser()

  6.  Now your rest web service  is done successfully, and using given below URL you can test the Rest web service
    • http://localhost:9000/RestWebService/getUser

  7. Now you can also test web service using Advanced Rest Client, Rest Response are given below
    • [1]
      0 : {
             id: “1”
             name: “chirag”
             fname: “gurav”
             lname: “M”
             email: “chiraggurav@gmail.com”
             address: “Gujarat”
             mobileno: “9998342122”
      }
  8. You can download the source code of rest web service click here RestDemo
Share!Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterFlattr the authorDigg thisPin on PinterestEmail this to someoneShare on StumbleUponShare on RedditShare on TumblrBuffer this pagePrint this page
Tags:
Comments Off on Rest Web Services in Play Framework.
Copyright © 2017 - Scalsys. All Rights Reserved