Web Service Steps

Web Services
  • Web applications – are software systems that are designed to be accessed by end users through browsers or other web client software.
  • Some software systems – are also designed to be accessed using web protocols, but are intended to be used by other software applications rather than directly by end users è designed to provide services to software applications
  • Because these services are intended to be accessed using web protocols and technologies, they are called web services.

Web Service Steps:

  • Web service is a special type of web application.
  • At their lowest levels, web services are typically based on transmitting XML documents between clients and servers via HTTP
  • XML vocabulary for describing web services is the Web Service Definition Language (WSDL).
  • A WSDL document for a web service identifies the operations provided by the web service, what the input data to each operation is, and what output data is produced by the operation. The data types in a WSDL document are defined using XML Schema.
  • A web service servlet accepts HTTP requests from clients and provides an HTTP response for each request received.
  • Each HTTP request contains a SOAP (Simple Object Access Protocol) XML document in the body of the request.
  • This SOAP document specifies an operation to be performed by the web service and supplies input data for that operation.
  • After processing a request for an operation, the web service servlet returns the processing results as a SOAP message in the body of the HTTP response.

Creating a Java Web Service involves two steps

  • Create the server side of a web service.
  • Create Java client software

 Tools required – wscompile and wsdeploy from JWSDP 1.3


Creating the Server side of a Java Web service

Create Directory structure

  • Create a WS directory in webapps directory of Apache server.
  • Create a WEB-INF subdirectory and within this create two sub-directories src and classes.
  • Create a myWS subdirectory of src. This will be used as the package name for source files.

Interface

  • Create an interface to specify the operation name that will be provided by the service and the input and output parameters.
  • This is known as the service endpoint interface.
  • The basic rules for the service endpoint interface are:
    • The interface must not contain any public static final declarations (global constants).
    • The interface must extend (directly or indirectly) the java.rmi.Remote interface.
    • Every method in the interface must throw java.rmi.RemoteException.
    • Every method parameter and every return value must conform to restrictions on the allowed data types.

 Class (2)

  • Create a class to implement this interface.
  • Web service returns the result as an object – create a class to define this object.

Compile the source files

  • Place all source files in src/myWS directory.
  • Compile both the class files

javac -d classes src/myWS/*.java

  • This automatically places the class files in the classes directory in WEB-INF

Create 3 xml files in WEB-INF directory – config.xml, jaxrpc-ri.xml and web.xml

config.xml file

  • The root of this document is a configuration element, which specifies a default namespace.
  • The child of this element is named service.
  • The service element provides
    • a name for the service,
    • namespace names to be used in the WSDL that will be generated for the service,
    • identification of the package containing the Java class files and
    • name of the service endpoint interface.
  • Example

<?xml version=”1.0″ encoding=”UTF-8″?>

<configuration xmlns=”<namespace>”>

<service    name=”myWSDL”

. . . // namespaces

packageName=”myWS”>

<interface name=”myWS.myIntf” />

</service>

</configuration>

  • Use wscompile to convert these Java files into a WSDL document along with another intermediate output file called the model

wscompile -define -d WEB-INF -classpath WEB-INF/classes -model WEB-INF/model.xml.gz config.xml

  • The myWSDL.wsdl document generated will be placed in the WEB-INF directory.

jaxrpc-ri.xml file

  • This document specifies the endpoint and endpointMapping elements including
    • the names of the service endpoint interface
    • the class implementing this interface, and
    • the location of the model document generated by wscompile.

web.xml file

  • File contains the display name and description of the web service

Deploying the Web service

  • Place the WEB-INF directory and its descendants in a JAR file.

jar cf ws-temp.jar WEB-INF

  • Process this file using the wsdeploy tool, and then the resulting WAR file must be deployed to a web server.

wsdeploy -o webserv.war ws-temp.jar

 

Place the webserv.war file in the webapps directory of WS and start the server.

 


 

Creating the Java Web Service Client

  • Create Java client software to access the web service using the JWSDP 1.3 wscompile tool.
  • Create an input XML document (config.xml) for wscompile.
    • root configuration element
    • child element is a wsdl element that specifies the URL of a WSDL document to be read
    • wsdl element – location and packageName are required attributes
    • packageName – name of the package in which the wscompile-generated code should be placed

// config.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<configuration xmlns=”. . . namespace . . .”>

<wsdl location = “url”       packageName=”myWSClient” />

</configuration>

  • Create a web application subdirectory WSClient under the webapps
  • Place the config.xml within the WEB-INF subdirectory of WSClient.
  • Create two subdirectories of WEB-INF, named classes and src, to contain the output of the wscompile tool.
  • Input the config.xml file to wscompile and compile è generates a proxy (Java) object, that can be called on by the client software in order to access the web service.

wscompile -gen -keep -d classes -s src config.xml

  • This command
    • instructs wscompile to generate class files from the WSDL specified in config.xml,
    • keep the Java source files generated,
    • store the class files in the classes subdirectory, and
    • store the Java source files in the src subdirectory.
  • The directory structure at this point looks like:

webapps

[[ other web application document base directories ]]

WSClient

WEB-INF

classes

myWSClient

src

myWSClient

  • The myWSClient directories and files are generated automatically by wscompile as specified in config.xml as the package name to be used.
  • The generated .java and .class files are contained in these directories under src and classes, respectively.
  • The names of the generated files are related to the values of various name attributes in the WSDL document
  • The interface file defines one method, getWSPort(), which returns an object of type myWS
  • The wscompile creates a class implementing this interface with a name that is the same as the interface name suffixed with Impl, and this class will have a public no-argument constructor.
  • Obtain a proxy object representing the web service.

WS wsobj = (new myWS_Impl()).getWSPort();

  • Next – call operations on the web service via methods on the proxy object.