Tuesday, September 20, 2011

Tutorial 1: Creating Web Services in Eclipse (Bottom Up)

(Please check also the update Tutorial with Helios here)
The purpose of these series Tutorial (updated with eclipse galileo) is to introduce tools needed, setup and initial guidelines for Service Oriented Applications.
Since there are many tutorials out there, this text will provide urls that point to each step and fill any gap between them. Web Services, Service Orchestration, Enterprise Service Buses and Business Rules Engine will be explored during these tutorials. The tool that we will mainly use as development platform is Eclipse. One might debate for NetBeans 6 (i.e. for a fair comparison see http://www.javaworld.com/javaworld/jw-03-2008/jw-03-java-ides0308.html?page=4). It's plugin ecosystem, OSGi based architecture makes it though my personal favorite choice.
We will start by just downloading the latest version of Eclipse. Download the EE bundle. Although one could use Yoxos and create a custom Eclipse download, we will just use Eclipse's update feature and install things as we go. You will need also a JDK. During this tutorial jdk1.6.0_12 is used.

These tutorials use Eclipse 3.5.0 (galileo). Just unzip your Eclipse file on your hard disk. As a rule of thumb, try to have different Eclipse installations for different needs, thus making your environment lighter. I.e. don't load your Java EE Eclipse with C/C++ plugins. (I have it under C:\eclipse-jee-galileo-win32)
Creating Bottom Up web services is the most simpler way, since you just make your POJO and you let to Eclipse the hard work to create WSDL, deploy service, etc.
Before starting your web services development, we need Apache Tomcat. So, download it and install it on your machine. During this tutorial we have used Apache Tomcat 6.0.18 Server. I used Windows Service Installer (since during this tutorial is made on Windows Vista. Linux would be a much more serious choice! ).
To configure your Apache Server in Eclipse,
·        Open Window -> Preferences -> Server -> Runtime Environments to create a Tomcat installed runtime.
·        Click on Add... to open the New Server Runtime dialog, then select your runtime under Apache (Apache 6.0 from the list). “Create a new local server” must be checked when adding the Apache Tomcat runtime environment. (This is usefull so not to mess up your actual web services with your web services that you are developing and testing)
·        Put your Tomcat installation dir C:\Program Files\Apache Software Foundation\Tomcat 6.0 and finish.
·        In the Servers panel, you will see the Tomcat server Stopped. In the Project Explorer view, a Server configuration will appear.
·        If you run the Server you might encounter an error if your Tomcat is already running. So you must configure different ports for your installation , otherwise you must stop your Tomcat server while you are developing and testing your web services. If you double click the server, the local configuration will appear. Change the ports as you wish and the Right Click the server Icon and select Start to start the server
 
 Before continuing we will need Apache Axis( an implementation of the SOAP). Download it from here. (This tutorial uses axis2-1.4.1.)
Save it for example on c:\axis2-1.4.1 and point Eclipse WTP to downloaded Axis2 Runtime. Open Window -> Preferences -> Web Services -> Axis2 Preferences and put there the path of Axis2.
On preferences check Generate an interface for the skeleton and Click OK
A nice Creating Bottom Up Web Service tutorial could be found here with similar action. However in this tutorial things are updated.
Create a project "ResourceA" with the support of Axis2 features. Open File -> New -> Other... -> Web -> Dynamic Web Project
 

 On the configuration click Modify..and check the Axis2 Web Services, click OK and Finish
 
Create a package named pkg (Right click on Java Resources ->New->Package) and a class name MyService (Right click on Java Resources->New->Class) . At the end you will have something like the following:
 

 Write the small Java code:
public class MyService {
public MyService() {
}
public Boolean Configure(String param, String val){
return true;
}
public Boolean StartMe(){
return true;
}
public Boolean StopMe(){
return true;
}
public String getStatus(){
return "No status available!";
}
}
Save it and Select the MyService.java file Right click and (down on the menu) Web Services -> Create Web Service
On the window that appears just click the Web Service Run Time and on the dialog select Apache Axis2

Click Next and leave the default services.xml.
Click Next and if prompted Start the server (remember here the ports otherwise the server will not run!)
The ResourceA now is deployed and start in our Tomcat server (remember the Eclipse copy of Tomcat (listens on 8081) not your system Tomcat (listens on 8080))
   

Now if you Select the ResourceA project, right-click and select Run -> Run As -> Run on Server you will go to http://localhost:8081/ResourceA/ which is a not found. (I don’t know why yet.Is this a bug?)
Just change it with the correct http://localhost:8081/ResourceA/axis2-web/index.jsp . You will get the home page:
 

Click on services. Check that your web service is Active. Check also the available Operations:
 

 If you click on the MyService link you will get the WSDL of your service (http://localhost:8081/ResourceA/services/MyService?wsdl)
 Copy this link on your clipboard.

Go to menu Run-> Launch the Web Services Explorer, and select from the icons the WSDL


Click on WSDL Main and paste the wsdl address, click Go.
 


The service will open. Just go for ie MyServiceSoap11Binding find the getStatus and click Go. A Soap call will be invoked and the message in the Body of the response will appear.



During this tutorial we created our first Web Service with Eclipse Web Tools Platform. In this tutorial here a Java client is also created that utilizes a web service.
To deploy your service on your Tomcat server, just right click the project ResourceA choose Export->WAR file.

Probe further:

The alternative way of Top-Down can be found in other tutorials. Check also a nice tutorial here.
Here you can find a nice getting started guide with Web Tools Platform.

No comments:

Post a Comment