Sunday, September 1, 2013

Sample Program on how to implement RESTful WebService in Java

This tutorial will explain on how to create your first REST based web project. I will also let you know what things we need to take are of while creating the project and its importance.
  1. Start eclipse and select appropriate workspace.

  2. Create your EAR project by selecting File->New->Enterprise Application Project.
  3. Click on "New Runtime" and select the version of JBOSS where you want to deploy this EAR.

  4. Once the EAR project is created, its time to create a Dynamic web Project. Select New->Dynamic Web Project
    • This project name is part of the REST URL. So give it a name that you would like to see in your URL.
      eg: http://localhost:8080/ProjectName/XXX
    • Check the option "Add Project to an EAR". Select the EAR project that you just created.

    Before you proceed further you would need to download the following RESTeasy jars and store them under demorestservice\WebContent\WEB-INF\lib.
    • javassist-3.8.0.GA.jar
    • jaxrs-api-3.0.6.Final.jar
    • resteasy-jaxrs-3.0.6.Final.jar
    • scannotation-1.0.3.jar
    Download the RESTeasy jars from here RESTeasy Jars

  5. Now it is all setup, its time to create the sample RESTful Web Service. Right click on the Dynamic Web Project and select New->Other. Search for "Create a Sample RESTful Web Service" under Web Services and click Ok.
    • The name of the Web Service is part of the REST URL. So give it a name that you would like to see in your URL.
      eg: http://localhost:8080/demorestservice/webservice-name
    At this stage the whole project should be compiling without error. If not first we need to fix that before we proceeding to next step.

  6. Right click on the EAR project and select Run As->Run on Server and select the runtime server we created at start and click finish.

  7. You should be able to access the Hello World Sample RESTful Web Service using the following URL

  8. http://localhost:8080/demorestservice/restservice


Hope this link comes handy, in case of any issue feel free to leave a comment.