Home ProgrammingJava First REST Web Service Using JAX RS

First REST Web Service Using JAX RS

by Log Raj
924 views

Hello this is very first initiative of us to write a simple REST web service. After going through this article you will be able to write your own REST web services. I’ll least explain theoretical part in this post. Don’t worry if you have not read anything about restful services till now. Lets start now.

System requirements for First REST Web Service:

In this post I’m using the eclipse IDE and assuming same for you. Eclipse must be opened in java EE perspective. Sometimes you may not have configured the eclipse for Web projects, then you must have to do some additional installation to make ready your eclipse for Web Development. Another Important requirement is you must have installed the Apache tomcat to go through this tutorial. Installation and setup of server is very important for developing web applications. One last thing is Maven plugin installed in eclipse. This is not mandatory at all but for this tutorial I’m gonna use maven instead of downloading each jars and manually configure them in build path. So I recommend you to use maven if you have not used it.

JAX RS Web Services:

JAX RS is a Java framework or set of specification provided by sun to implement RestFul web services. Don’t think this as other API’s because it consist of only interfaces and it doesn’t contain any implementation class. There are lot of third party implementation of JAX RS.  Among these Implementation jersey is one of popular one and I’m gonna use that. We need do get separate jars for JAX RS because it is bundled with implementation api jersey itself. I’m adding jersey jars using maven dependencies.

Steps:

Below is set of steps, At the end of these steps you will find a small web service running.

    1. You have to go for new maven project. For that File-> New-> Others

First REST Web Service step 1

2. Now You will select Maven project under Maven from select wizard. If you are not seeing Maven there don’t worry, install it. Then click On Next.

First REST Web Service step 2

3. After step 2, a select new Maven project wizard opens. This is the easiest step because you have to do click Next only here.

First REST Web Service step 3

4. Now there is another view in same wizard which is to select Archetype. This is all to add the jersey dependency jars in our project. While typing jersey in the filter box you may not find any archetype there. In such case click Add Archetype button below.

First REST Web Service step 4

5.  After this you will see a small Add Archetype box. That contain 4 fields. First one is Group Id. Input org.glassfish.jersey.archetype in Group Id. Second field is Artifact Id Where you have to put jersey-quickstart-webapp. And in 3rd Version Field input 2.25. Version will change with time. So please follow this link and know the latest version. Leave the last field and click on Ok button.

First REST Web Service step 5

It will take while to search and download the artifact and it will appear in your Select Archetype box.

6. Now select that archetype in box and click next.

First REST Web Service step 6

7. Wow this is the last box and say to input the Archetype parameters. Don’t afraid of this you have to give name of project and package structure like information here.  I give com.pyarb.log.java in the Group Id and TestRest in Artifact Id. This is information about our project which is being created. Select version Dropdown or else give any version. Then click finish to complete. Package field will fill automatically.

First REST Web Service step 7

8.  This may take few minutes to prepare the project so don’t hurry up. Keep calm and wait. Because first time when I created the project using maven I didn’t wait and get troubled.

First REST Web Service step 89. Now you will see a project like this in project explorer where you can further investigate the project hierarchy.

First REST Web Service step 9

View Server

10. If You can not see servers tab in the bottom most view of eclipse You can get it from show view. Click on Window->Show View->Others.

First REST Web Service step 10

11. Now Select Servers from the list of show view window then click on Open.

First REST Web Service step 11

 

 

12. Now you can see Severs view like this. If you do not have already used server you may see like this. In this case you click on create a new server link.

First REST Web Service step 13

13. Then select the tomcat directory and you can see name of server in the server view. You also can start the server from there. Start server by right clicking on it.

First REST Web Service step 14

14. Hope this will be last step. Run the project on server by right clicking on it and then go throw wizard.

First REST Web Service step 15

14. You will see a index page where you will get link to a web service as per template. On clicking it that service will be called.

First REST Web Service step 15

When clicking The Jersey resource you will get a small piece of text that is maintained in the MyResource class. You can change that message and test it.

In Next article I will explain about returning xml data from the  rest service.

First REST Web Service Using JAX RS

by Log Raj time to read: 3 min
0