Tuesday 19 February 2008

Implementing WebServices - Part I

Overview

I have recently been working on jax-ws webservices using Netbeans, Tomcat, and using Ant to build and deploy these systems.
Whilst much of what is involved is pretty straightforward, I could not find one single document that takes the developer through everything so have decided to write my own.

Requirement

Install Java

Install Netbeans

Install Tomcat

Using Netbeans develop a webservice that will deploy to Tomcat

Create a webservice client that can call the webservice


Environment

I am developing on a Dell Latitude 630, with 2GB RAM, and running Windows XP.

Step 1 – Install Java

Go to http://java.sun.com/javase/ and download the latest version of Java SE. I am running JDK 6 Update 4. Install Java and go with all the defaults

Step 2 – Install Netbeans

Go to http://download.netbeans.org/netbeans/6.0/final/ and download the latest version of Netbeans. I have gone with the ‘All’ bundle. Install Netbeans and go with all the defaults

Step 3 – Install Tomcat

Go to http://tomcat.apache.org/ and download ‘Tomcat v5.5’. From Binary distributions, select the Core->zip bundle. Extract the bundle to C:\apache-tomcat-5.5.25

Step 4 – Install JAX-WS

Go to https://jax-ws.dev.java.net/2.1.3/ and click on the link to download the binary. I am running JAX-WS RI 2.1.3. Double click on the downloaded binary and this will extract the contents into a sub-folder jaxws-ri.
If you look in jaxws-ri\lib you will see all the jars that we need for deploying to Tomcat

Step 5 – Copy JAX-WS jars to Tomcat

If necessary create the directory C:\apache-tomcat-5.5.25\shared\lib and copy all the jars from jaxws-ri\lib to C:\apache-tomcat-5.5.25\shared\lib. Tomcat will now support WebServices.

Step 6 – Configure Netbeans to work with Tomcat

Go to services and right click on Servers. Click on Add Server

Select Tomcat 5.5 from the list of available servers and click on Next

Click on the Browse button

Navigate to where you installed Tomcat and click on Open

Create a user for Tomcat by entering the username and password into the Username and Password fields (default admin, admin), and click on Finish

You will now see Tomcat in the list of available servers


Step 7 – Configure Tomcat to run on port 80

Navigate to C:\apache-tomcat-5.5.25\conf and edit server.xml

Change the Connector Port from 8080 to 80:


<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8080"
maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<!-- Note : To disable connection timeouts, set connectionTimeout value 


To:


 
<!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<!-- Note : To disable connection timeouts, set connectionTimeout value


Step 8 – Create the WorkSpace

Create the directory C:\Filing\Workspaces\examples in which all your projects will be stored.

Step 9 – Create the WebService Project

Right click in the Projects tab and click on New Project



Select Web->Web Application and click Next



Fill in the fields as follows, and click Finish:

Project Name: WebService One

Project Location: c:\Filing\Workspaces\examples

Server: Tomcat 5.5

Java EE Version: J2EE 1.4

Context Path: /WebService_One


Set Source Level to 1.4 - unchecked

Set as Main Project - unchecked


The WebService One project will open with the index.jsp page opened for edit.
Change the title and h1 body elements from JSP Page to WebService One



Click on the Save All icon in the toolbar



Right click on index.jsp in the projects screen and select Run File



The project will be deployed to the tomcat server. The server will start and the page will be displayed



Step 10 – Include the JAX-WS Jars in the Project Path to compile Web Services


Right click on the Project Name and select Properties from the pop-up menu



Click on Libraries
Click on ‘Add Jar / Folder’


Navigate to C:\apache-tomcat-5.5.25\shared\lib
Select jaxws-tools.jar and click Open



uncheck the Package



Right click on the WebService One project in the projects screen and select New->Web Service


In the WebService name enter ‘WSOne’
In the Package, enter ‘com.examples.webservices’
Click on Finish



Web Service WSOne will open in the Design Screen
Click on ‘Add Operation’



In the name field enter ‘getMessage’
Click on ‘Add’ to add a parameter and set the parameterName to ‘userName’
Click on OK.



Click on the Source tab to see the source that has been generated:


The generated web service source is as follows:


package com.examples.webservices;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
*
* @author johnd
*/

@WebService()
public class WSOne
{
/**
* Web service operation
*/

@WebMethod(operationName = "getMessage")
public String getMessage(@WebParam(name = "userName") String userName)
{
//TODO write your implementation code here:
return null;
}
}


Step 12 – Compile and Run the WebService

Click on the Save All icon to save the new webservice
Right click on the WebServiceOne project and select Clean and Build


If you get an error at this point stating that WSGen cannot be found, then you have not included the jar file from tomcat/shared/lib in your build path (Step 10)

Created dir: C:\Filing\Workspaces\examples\WebService One\build\generated\wsgen\service
Created dir: C:\Filing\Workspaces\examples\WebService One\build\generated\wsgen\binaries
C:\Filing\Workspaces\examples\WebService One\nbproject\jaxws-build.xml:12:
taskdef class com.sun.tools.ws.ant.WsGen cannot be found
BUILD FAILED (total time: 0 seconds)

Right click on the project and select ‘Undeploy and Deploy’ (since the server is already running)



Expand the Web Service folder in the project screen and right click on the WSOne web service, click on Test Web Service



The browser will open to the web service



Click on the hyperlink to view the WSDL for this web service



The WSDL for this web service is

<?xml version="1.0" encoding="UTF-8" ?>
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1-b03-.
  -->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1-b03-.
 -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservices.examples.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservices.examples.com/" name="WSOneService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservices.examples.com/" schemaLocation="http://localhost:80/WebService_One/WSOne?xsd=1" />
</xsd:schema>
</types>
<message name="getMessage">
<part name="parameters" element="tns:getMessage" />
</message>
<message name="getMessageResponse">
<part name="parameters" element="tns:getMessageResponse" />
</message>
<portType name="WSOne">
<operation name="getMessage">
<input message="tns:getMessage" />
<output message="tns:getMessageResponse" />
</operation>
</portType>
<binding name="WSOnePortBinding" type="tns:WSOne">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="getMessage">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="WSOneService">
<port name="WSOnePort" binding="tns:WSOnePortBinding">
<soap:address location="http://localhost:80/WebService_One/WSOne" />
</port>
</service>
</definitions>

Step 13 – Modify the Web Service to return the message

Modify the getMessage method to return the message Hello ‘name“


package com.examples.webservices;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
/**
*
* @author johnd
*/
@WebService()
public class WSOne
{
/**
* Web service operation
*/
@WebMethod(operationName = "getMessage")
public String getMessage(
@WebParam(name = "userName") String userName)
{
return "Hello "+userName;
}
}