In order to create a RESTful Web Service using Java, we need several tools and library
The tools and library which i used are:
1. Java 1.6
2. Eclipse IDE for Java EE developers (Ganymede)
3. Apache Tomcat (6.0)
4. Jersey API (1.4) . Download the zipped file.
5. Oracle Database 10g or any other DBMS
Create a new DynamicWebProject in Eclipse. If no such project template exists, you should download Eclipse for Java EE developers.
Copy those Jersey API librari in "WEB-INF\lib" folder
Modify web.xml to register our jersey servlet. Put any name on the servlet name tag. It doesn't matter.
Our web service will connect to database and retrieve or insert a new note in NOTE table
Create a new table named : NOTE with the following columns
NOTEID : number
CONTENT : varchar
CREATEDDATE : date
We will create a model class for that table: Note.java
Next, create a class that will handle connection with database: DatabaseAccess.java
After that, create a class that will handle the database operation. A Data Access Object :
NoteDao.java
Finally, we create a resource class that will be able to retrieved by the client using REST web service
Save and deploy the project.
Populate our NOTE table using some dummy data and try to call our resource using the following URL in your web browser.
http://localhost:8080/NoteWS/notes
If u got xml file containing the records in your NOTE table. You have successfully create a REST Web Service.
Now, how can we use the POST method to actually insert a record.
Well, we can create a html form whose method is POST and submit action refers to
http://localhost:8080/NoteWS/notes
Something like this:
Notice that we have to make sure our text field name match the form parameter name defined in NoteResource.java
Note that createddate accept a String but in NoteResource.java it parses the string to primitive type long for creating new Date object. So, for now, just insert any number which will represent the second from January 1, 1970.
access http://localhost:8080/NoteWS/notes again to check our new note.
That is if for creating REST Web Service using Java and Jersey API.
Check out my other tutorial to find out how to consume this REST Web Service From
1. Simple java project
2. Android
3. iPhone
Have fun with REST!
Thanks buddy, it helped a lot.
ReplyDelete-Nisarg
Great Article
ReplyDeleteJava Web Services Online Training
Web Services Course
Web Services Training Courses
Java Web Services Training in Chennai