How-to: Develop a Web Service Using Eclipse
In this short how-to, I’ll explain how to basically develop and deploy a web service using Eclipse IDE. In this tutorial, these technologies are used:
- Programming Language: Java
- IDE: Eclipse
- Web Service Protocol: SOAP
- SOAP Implementation: Axis
- Client Application Type: Java Servlet
- Servlet Container: Apache Tomcat
First, we should download and run Eclipse. Then, we install the following features of Eclipse using Help -> Software Updates menu:
- Java EE
- Web Developer Tools
- Axis 2
- WTP Web Services
After installing necessary plugins, we should setup Tomcat where we’re going to deploy our applications on. Download Tomcat’s binary distribution and extract the arcive. Now, return back to Eclipse to point our Tomcat installation in order to make them work together. Use Window -> Preferences -> Server -> Runtime Environment screen to your add Tomcat installation.
Now, create a dynamic web application project and choose Tomcat to deploy your application in project wizard. Then, create a Java class under src folder and write the functions which you want to use as a web service.
Choose the new class on project explorer and create a new web service using File -> New -> Other.. -> Web Services -> Web Service menu. Choose your service implementation as your new class and mark publish and monitor options. Then, Eclipse should automatically create wsdl and web service implementation using Axis. Run your web service and open a web browser. You should access Axis front page at http://localhost:8080/PROJECT_NAME/services/CLASS_NAME location. This will be the address of your web service.
Next, try to build a basic web service client to check if everything is working correctly. Use File -> New -> Other.. -> Web Services -> Web Service Client menu and choose your service definition of the recently created web service. Do not forget to select Tomcat server to deploy this client web application. In the new project which will be created by Eclipse, the necessary classes to access our web service should be automatically generated. Create a Servlet by right-clicking your new client package and choose “Servlet. Enter a class name URL mapping. In doGet method of the Servlet class, write a similar code below to access your remote class:
HelloServiceLocator loc = new HelloServiceLocator();
Hello helloClass = loc.getHello();
Now, you are able to use whatever function of the class you provided as a web service. Try to run a function and write its output using response.getWriter().println() method of the servlet. Then, run the application on Tomcat. You should be able to access your new servlet at URL http://localhost:8080/CLIENT_PROJECT_NAME/SERVLET_NAME
Next week, other parts of the application will be merged in order to make a proof-of-concept prototype.