| Java, Eclipse and Web programming Tutorials |
Version 0.6
Copyright © 2007 Lars Vogel / Waldemar Geppart
04.07.2009
| Revision History | ||
|---|---|---|
| Revision 0.1- 0.4 | 25.09.2008 | Lars Vogel / Waldemar Geppart |
| First versions | ||
| Revision 0.5 | 27.10.2008 | Lars Vogel |
| Added Axis2 Overview | ||
| Revision 0.6 | 04.07.2009 | Lars Vogel |
| Updated simple web-service example | ||
Table of Contents
Eclipse WTP provides functionality to develop web functionality with Java. For an introduction to Eclipse WTP please see Servlet and JSP with Eclipse WTP .
If not yet down, install now Eclipse WTP. For an installation instruction of Eclipse WTPplease see Installation of Eclipse WTP
Also install Tomcat Tomcat Installation
This chapter requires an Axis2 runtime, the general download site is here .
Extract the Axis 2 zip and point Eclipse WTP on it as shown below.

Click OK.
Select Window -> Preference and set Axis2 as the default engine.

The W3C defines a Web service as a software system designed to support interoperable machine to machine interaction over a network.
In common usage the term refers to clients and servers that communicate using XML messages that follow the SOAP standard. The server usually provides a machine readable description of the operations supported by the server, a description in the Web Services Description Language (WSDL).
Create a new Dynamic Web project "de.vogella.webservice.soap.axis2". Create a package "de.vogella.webservice.soap.axis2" and the following class.
package de.vogella.webservice.soap.axis2;
import java.util.Random;
public class RandomNumber {
public long getNumber(){
Random random = new Random(1000);
return random.nextLong();
}
}
Now you are ready to create a Web Service for your class. Right click on "RandomNumber.java" and select Web Services -> Create Web Service.


Press next.

Click "Next". If your server is not running, you will see this dialog. Click "Start server", wait a few seconds and then click "Next".


Click "Finish".
Congratulations! You created a Web Service.
To use your webservice you need a client to. For this you need the URL of the Web Service. Right click on the project and select Run As -> Run on Server. Check the check box and click "Finish".
Most likely you will find the axis services under the web-address http://localhost:8080/de.vogella.webservice.soap.axis2/axis2-web/index.jsp.
You should see the following Axis2 main page.

Click on the link "Services".

Click on the link "RandomNumber" and you will see the wsdl
Copy the URL, e.g. http://localhost:8080/de.vogella.webservice.soap.axis2/services/RandomNumber?wsdl , because you will need it later.
Right click on your project and select New -> Other… -> Web Services -> Web Service Client. We will now create a client. Through a setting later we will later on create the client in a separate project.

Click "Next".
Paste your copied service URL into the "Service definition" field.

Open the link "Client project: …" and enter "de.vogella.webservice.soap.axis2.client" to create a new project for your client.

Click "OK" and "Next".

Click "Finish".
You have generated a Web Service Client!
Now we are going to test our Web Service using this client. So we create a test class in "de.vogella.webservice.soap.axis2.client" project. Select your package "de.vogella.webservice.soap.axis2.client". Create the following class ServiceTest.java.
package de.vogella.webservice.soap.axis2;
import java.rmi.RemoteException;
import de.vogella.webservice.soap.axis2.RandomNumberStub.GetNumberResponse;
public class ServiceTest {
public static void main(String[] args) throws RemoteException {
RandomNumberStub stub = new RandomNumberStub();
GetNumberResponse res = stub.getNumber();
System.out.println("Counter: "+res.get_return());
}
}
Right click "ServiceTest.java" -> Run As -> Java Application. You should see the result of your call in the command line.
After we created a rather simple Web Service, we will create a complex one. Complex means, that it will use Java Interfaces and Collections.
The first step is to create a "Dynamic Web Project" (e.g. "ComplexWebService") like it is described here .
We need an Interface "ICar" and an implementing class "Car". The "Car" objects will be managed in the "CarFactory" class. That means, "CarFactory" has a Collection of "Cars". But Axis2 cannot handle Java Collections, so we need an Adapter class ("CarFactoryAdapter") to transform our Collection to an Array. The last thing we need is our "CarService" class. Your package structure should look like this:

package data;
public interface ICar {
public int getMaxSpeed();
public String getModel();
public void setMaxSpeed(int maxSpeed);
public void setModel(String model);
}
package data;
public class Car implements ICar {
private String model;
private int maxSpeed;
public Car(String model, int maxSpeed)
{
this.model = model;
this.maxSpeed = maxSpeed;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
package data;
import java.util.Vector;
public class CarFactory {
private Vector<ICar> cars = new Vector<ICar>();
public Vector<ICar> getCars() {
return cars;
}
public void setCars(Vector<ICar> cars) {
this.cars = cars;
}
}
package adapter;
import java.util.Vector;
import data.Car;
import data.CarFactory;
import data.ICar;
public class CarFactoryAdapter {
private ICar[] cars;
public CarFactoryAdapter(CarFactory carFact)
{
Vector<ICar> carList = carFact.getCars();
cars = carList.toArray(new Car[carList.size()]);
}
public ICar[] getCars() {
return cars;
}
public void setCars(ICar[] cars) {
this.cars = cars;
}
}
package service;
import adapter.CarFactoryAdapter;
import data.Car;
import data.CarFactory;
public class CarService {
private CarFactory carFact = new CarFactory();
public CarFactoryAdapter getCarFactory()
{
carFact.getCars().add(new Car("BMW", 250));
carFact.getCars().add(new Car("Porsche", 300));
carFact.getCars().add(new Car("Maserati", 350));
return new CarFactoryAdapter(carFact);
}
}
Create a Web Service for the CarService class, create a webservice client and a test as described Create a first Webservice .
The Test class looks like the following.
package service;
import java.rmi.RemoteException;
import service.CarServiceStub.CarFactoryAdapter;
import service.CarServiceStub.GetCarFactoryResponse;
import service.CarServiceStub.ICar;
public class Test {
public static void main(String[] args) throws RemoteException {
CarServiceStub stub = new CarServiceStub();
GetCarFactoryResponse res = stub.getCarFactory();
CarFactoryAdapter carFact = res.get_return();
ICar[] cars = carFact.getCars();
for (int i = 0; i < cars.length; i++) {
System.out.println(cars.clone()[i].getModel());
System.out.println(cars.clone()[i].getMaxSpeed());
System.out.println("------------------------------");
}
}
}
Eclipse allows to test webservices. In the Java EE perspective select Run-> Launch the Webservice Explorer

Open the WSDL page.

Double-click on the WSDL Main and maintain the URL to your webservice.

Press go. The system will list your operations. Click on them to test the. Here you can maintain your parameter, test and see the return parameter.


Thank you for practicing with this tutorial.
Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.
I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.
http://www.vogella.de/code/codeeclipse.html Source Code of Examples
http://www.ibm.com/developerworks/edu/os-dw-os-eclipse-europa1.html Web development with Eclipse Europa, Part 1: The Java EE for Eclipse
http://www.servlets.com/ Web page about servlets
http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html Eclipse WTP Tutorials - Creating Bottom Up Web Service via Apache Axis2