Web Services Tutorial

 

Context of this tutorial

This short tutorial aims to show you why and how to build a SOA  Web Service. we will show you how to create a SOAP Web service that uses a SQLite database .

The first concept we introduce is the Service Oriented Architecture (SOA) which can be defined as: "A view of a system in which each application is seen and managed as a service provider".

In a SOA based system, the resources are made available to other participants in the network as independent services that are accessed in a standardized way. This means buildng a standard interface for each resource (Java Objects, Java Methods, Functions, Procedures, etc.) so other systems can use them easily. This provides  a flexible loose coupling of heterogeneous systems.

In this tutorial, we will make a Java resource available to other systems by creating a machine readable interface described in WSDL.

We will create a simple Web service that receives as an input a zip code (from France) and it gives you back the name of the city.  We have implemented this Web service so you can test it and learn how to build it. To test the zipCode service click here

Now, for building this Web service we will use Netbeans 6.5 and a small SQLite file that you can download here.

 

How to build the ZipCode web service

1. Creating the Project

The first step in creating our Web service is to create a Java Project that can act as the service endpoint. From within NetBeans, select the File | New Project menu option and create a new EJB Module by selecting the Next button.




 

 

 

 

 

 

 

 

Enter the name of the project as zipCodeWS. Ensure that either the Sun Java System Application Server or GlassFish V2 (depending upon your version of NetBeans) is selected as the server and that Java EE version 5 is selected. At this point, press the Finish button to create the project.


 

 

 

 

 

 

 

 

 

 

2. Describing the data that will be used

We need to define the type of data that will be used in the SOAP  messages of the service. For doing this, just right-click the zipCode project, then select New | XML Schema

 

 

 

 

 

Now we can define the data that is used in the Web Service. In this example we will only define the data that will be used as input and output of your Web service. This is:

  • As input, the zip code, that we will define as integer

  • As output, the name of the city, that we will define as string

For doing this, Netbeans offers a GUI to add new elements in the XSD file. To add a new element right-click Elements and select Add Element

 

 

Then as name of the element write zipcode and select as Type Use Existing Types | integer

 

 

 

 

 

 

 

 

 

 

 

After doing this you will see this new Element listed as one of the elements of the XML Schema. Now you can create the second element, we call it "village" of type string.

3. Writing the description of your Service (WSDL file)

After defining the type of data that will be used in the service we now can write the description of the service. In other words this means that we create a WDSL file,  to let other machines understand how to use our service.

Within the Projects window, right-click on the zipcode project and select the New | WSDL Document... menu option. If that menu option is not available, select the New | Other menu option and on the resulting dialog, select the XML category and the WSDL Document file type.

For our simple zipCodeService web service, we are going to define one port Type (method) called returnCity. This will be a request/response web service that will use the types defined in the XML schema we created in step 2. Be sure to include this XML schema by checking the Import XML Schema File(s) and selecting the XML Schema file. After filling in the files as shown in the image, click Next

 

 

 

 

 

 

 

 

 

 

In this step, accept all the defaults of the Abstract Configuration and just change the types of the input and output. Select as type of input zipCode and as type of Output village and clic Next. Finally, select the Binding Subtype as Document Literal and accept the defaults for the rest.

 

 

 

 

 

 

 

 

 

 

4. Writing the Java Method that wil use

Now it is time to actually build the Web service which is the logical part that will recieve the zip code and return the name of the city. For doing this just right-click the zipCode project, then select New | Web Service from WSDL

 

 

 

 

This will create an empty method that receives, as parameter, the variable part1 (the integer input that we defined as type zipcode). You can fill the empty method with the following code:

// seting up the default response in case that w doesn't find the zipcode
String resp ="not found";
// setting up the connection to the database
Connection connection = null;
try {
// Mounting the database driver for SQLite. Please remember that you must have included the SQLite library in your imports
Class.forName("org.sqlite.JDBC");
// Setting the location of the SQLite database file and defining other database parameters
connection = DriverManager.getConnection("jdbc:sqlite:C:\\zipcodes.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
// Defining the select query to the database
String command="SELECT * FROM zipcodetable WHERE zipcode="+part1+";";
// Executing the query
ResultSet rs = statement.executeQuery(command);
resp="connecting to the database";
while (rs.next()) {
// Setting the output of the method, the string containing the name of the city
resp= rs.getString("village");
}
} catch (Exception e) {
// In case that the database was not found
e.printStackTrace();
resp="database not found";
} finally {
try {
// Closing the connection to the database
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// connection close failed.
System.err.println(e);
}
}
// returning the value of the method with the string containing the city
return resp;
}
}

5. Testing your web service

Before testing the web service you must do a Clean and Build of your project. Then Deploy the project, and finally right-click the Web Service of the project and select Test Web Service.

 

 

 

 

 



This will open a web browser and a Web page that let you interact with the Web service

©2008 MONTIMAGE, ALL RIGHTS RESERVED