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.
Below is set of steps, At the end of these steps you will find a small web service running.
- You have to go for new maven project. For that File-> New-> Others
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.
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.
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.
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.
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.
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.
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.
9. Now you will see a project like this in project explorer where you can further investigate the project hierarchy.
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.
11. Now Select Servers from the list of show view window then click on Open.
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.
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.
14. Hope this will be last step. Run the project on server by right clicking on it and then go throw wizard.
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.
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.