Monday, August 12, 2013

Java WebService Tutorial - Part 11 (Writing Simple REST WebService using Netbeans & GlassFish)

In this part we will see how to write a Simple REST Style Web Service using NetBeans and GlassFish Server Server and we will test the service.


1. NetBeans ID
2. GlassFish Server.

Usually NetBeans comes along with the GlassFish Server and also in build support for generating REST Services using the Jersey Framework.
So , once we have installed the NetBeans ID and GlassFish we can create our first webservice now.

Note : This is a quick guide of creating REST using Jersey Framework, we are not going in detail about the annotations we are using in the REST Webservice , we will discuss the basic annotation right now ,in later part we will cover in detail about each annotations with respective example.

Step 1:

Open NetBeans ID.

File-->New ---> New Project --> Java Web-->Web Application and click the "Next" Button.

Give a name to the Project say "MyFirstREST" (I am using this name here for this example) and click the "Next" Button.

Select the Server as "GlassFish Server" and select the J2EE Version as "Java EE 5" click the "Finish" Button.
So , now our project folders are created .

Step 2:

Creating Package.

Right click on the Project Node "MyFirstREST" and select :

New ---> Java Package and give name as "com.test" (You can provide your own name for this example i am using this name)
Now we have a package where all our service class will be kept.

Step 3:

Writing the REST Business Logic.

 There are couple of annotations i liked to discussed here, these are the very basic and commonly used annotations.

@Path  -- Mention the Path from which you want to access a REST Service either class level or method level.

@GET -- Performs HTTP get Operation useful for getting info read only.

@Produces - This Produces the Respective output in different format such as XML,JSON,TEXT,HTML etc to the client.

Step 4:

Implementing the Jersey Framework & Writing our First REST Service 

NetBeans generally comes along with the Jersey Framework if you don't have the Framework install , manually you should have to download it and add the jars to the libraries.

But i am not going to discuss about that , i assume you all are smart guys , and you have NetBeans with J2EE Supported version , so basically you will have Jersey supported framework install in the NetBeans Itself.

So , We don't need actually write any code from implementing this frame work in web.xml, NetBeans automatically does for us., that's the advantage of using NetBeans.

So now this can achieve by following these steps :

Right click on the "com.test"(Package i am using for this example, you can use your own)ans select 

New--->Other--->WebService-->RESTful WebServices from Patterns.

Select patterns as "Simple Root Resource" and Press the "Next" Button

Give path Name as"MyPath" and class Name as"MypathResource" and select the MIME Type as "text/plain". and importantly select the check box for Jersey framework and click the "finish" Button.

Step 5:

Develop the code.

By default a class name called "MypathResource" will be created with some methods and instance variable define in it.Delete the code and replace with the following code.

package com.test;
public class MypathResource {
    public String getText() {
        return "My First RESTful Servivce....";

This is the very basic and simplest RESTful Service , it will accept the Path "MyPath" in the URL and invoke the method getText which in turn return a Response as Text with the wording "My First RESTful Servivce..."
Step 6:

Clean & Build , Deploy to check the service.

Right Click on the Project Node "MyFirstREST" and select Clean and build this will clean the directory and compiles the Java files and  creates a WAR Archive.

Once , the Clean and build is finish successfully again RightClick on the ProjectNode "MyFirstREST" and select "Deploy".

Once the Deployment is done we can check the service by:
Right click on the REST Service created under folder called "Restful Web Service" and  select "Test Resource Uri"

or  we can directly access using the URL

Note : The Port Number will differ based upon your Server Configuration Setting.

Result :

Main Components :

How all these is happening ?

The main Gate Keeper or the main Servlet for this is specified in the web.xml which performs the Jersey Mechanism , Please refer the image below for detail explanation of Jersey Servlet define in web.xml .

That's all for the day, In the Next Section we will be seeing different useful annotations and how to produce different form of Outputs.

Thanks for Reading.
Please provide your valuable suggestion and comments.

<< Prev                                   Index                                         Next>>


Technology Blogs


  1. Excellent information for beginners. Thanks for sharing. Keep sharing your knowledge.

    1. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

  2. thank you for your information

  3. Thanks a lot buddy..

  4. I am getting the following error

    WEB0123: WebModule [RestProject1] failed to deploy and has been disabled
    java.lang.NoClassDefFoundError: javax/servlet/Filter
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)........

  5. why have we selected java version as java EE 5, I tried with java EE 7 and netbeans doesnt create .

  6. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    Devops Training courses
    python Training in chennai
    Devops Training in Bangalore

  7. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    Devops Training courses
    python Training in chennai
    Devops Training in Bangalore

  8. You are doing a great job. I would like to appreciate your work for good accuracy
    Best Devops Training in Chennai | Best Devops Training Institute in Chennai

  9. This is quite educational arrange. It has famous breeding about what I rarity to vouch.
    Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved.
    This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.

    Selenium interview questions and answers
    Selenium Online training
    Selenium training in Pune
    selenium training in USA
    selenium training in chennai

  10. Your story is truly inspirational and I have learned a lot from your blog. Much appreciated.
    Python Online training
    python Course institute in Chennai
    Python Course institute in Bangalore

  11. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
    Python Online training
    python Course institute in Chennai
    Python Course institute in Bangalore

  12. Thanks for providing wonderful information with us. Thank you so much.
    Devops Training in Chennai | Devops Certification in Chennai

  13. I feel happy to say this I will deeply learn your blog and it’s really useful for me, keep sharing like this type valuable information regularly, I like to thanks for sharing this superb blog I hope I see you soon again time, thank you so much.
    lg mobile repair
    lg mobile service center near me
    lg mobile service center in velachery

  14. I I'm following your tutorial, i'm using Netbeans 10 with JDK 8 and Glassfish 5.0, so i get this error while building the project:

    error: option -Xbootclasspath/p: not allowed with target 11

    Whitch version of glassfish did you use while making this tutorial?

    Thank you.

  15. For Devops Training in Bangalore - Devops Training in Bangalore

  16. For Hadoop Training in Bangalore Visit : HadoopTraining in Bangalore

  17. It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
    DevOps Training in Chennai

    DevOps Course in Chennai

  18. Choose a Concept - You will have more freedom in deciding what you should write about, if the instructor has not assigned a concept.

  19. Great, thanks for sharing this post.Much thanks again. Awesome.
    core java online course
    core java online training

  20. Thanks a lot for sharing. Private tutor Naples Will check back later for more of your articles.

  21. Thanks for posting! I really like what you've acquired here; You should keep it up forever!Private tutor Sunny Isles Best of luck