Introduction
Web services are Web based applications that use open, XML-based standards and
transport protocols to exchange data with clients. Web services are developed using
Java Technology APIs and tools provided by an integrated Web Services Stack.
The term Web services describes a standardized way of integrating Web-based
applications using the XML, SOAP, WSDL and UDDI open standards over an Internet
protocol backbone. XML is used to tag the data, SOAP is used to transfer the data,
WSDL is used for describing the services available and UDDI is used for listing what
services are available. The SOAP engine generally used is AXIS - Apache EXtensible
Interaction System.
Web Services are used primarily as a means for businesses to communicate with each
other and with clients; Web services allow organizations to communicate data without
intimate knowledge of each other's IT systems behind the firewall.
What is SOAP?
SOAP is an XML-based communication protocol and encoding format for inter-
application communication. Originally conceived by Microsoft and Userland software, it
has evolved through several generations; the current spec is version, SOAP 1.2, though
version 1.1 is more widespread. The W3C's XML Protocol working group is in charge of
the specification.
SOAP is widely viewed as the backbone to a new generation of cross-platform cross-
language distributed computing applications, termed Web Services.
What is AXIS?
Axis is essentially a SOAP engine -- a framework for constructing SOAP processors
such as clients, servers, gateways, etc. The current version of Axis is written in Java,
but a C++ implementation of the client side of Axis is being developed.
But Axis isn't just a SOAP engine -- it also includes:
2000, the committers of Apache SOAP v2 began discussing how to make the engine
much more flexible, configurable, and able to handle both SOAP and the upcoming
XML Protocol specification from the W3C.
Axis delivers the following key features :-
2.1.3.
What is a WSDL File?
The Web Services Description Language is an XML-based language that provides a
model for describing Web services.
WSDL is often used in combination with SOAP and XML Schema to provide web
services over the Internet. A client program connecting to a web service can read the
WSDL to determine what functions are available on the server.
Any special data types used are embedded in the WSDL file in the form of XML
Schema. The client can then use SOAP to actually call one of the functions listed in the
WSDL.
4.1 Using WSDL with Axis
When you make a service available using Axis, there is a unique URL associated with
that service, users may then access the service's URL with a standard web browser and
by appending "?WSDL" to the end of the URL, Axis will automatically generate a service
description for the deployed service, and return it as XML in your browser.
The resulting description may be saved or used as input to proxy-generation, described
next. You can give the WSDL-generation URL to your online partners, and they'll be
able to use it to access your service with toolkits like .NET, SOAP::Lite, or any other
software which supports using WSDL.
Steps for Writing Java Code to Invoke Web Services
Step 1 - Get Apache Axis2
Download Apache Axis. It can be downloaded from the link http://ws.apache.org/axis2/.
Just unzip it in a dir. Let’s say your Apache Axis is unzipped here: C:\axis2. Let’s call
this dir <AXIS_HOME>.
Step 2 - Get hold of the wsdl
Get hold of the wsdl file that describes the web-service. Every web-service has an
associated wsdl file (as explained above). Normally, the web-service URL with a
‘?wsdl’ suffix appended to it, will show you the wsdl file. So, if a web-service you want to
invoke is available at: http://host:port/superservice/servicemall. You can see the wsdl via
a URL: http://host:port/superservice/servicemall?wsdl
Step 3 - Create the Stub
Stub is nothing but the web-service invocation code that you are going to generate now.
Go to your command line window, change to dir: <AXIS_HOME>\bin, and run this
command:
¾ WSDL2Java -uri servicemall.wsdl
NOTE: servicemall.wsdl should be present in AXIS_HOME\bin directory. Instead of
pointing to that file, you could even use a URL format pointing to the wsdl, now the
command to be run on your command line window is:
¾ WSDL2Java -uri http://host:port/superservice/servicemall?wsdl
You will see that stub code has been generated here: <AXIS_HOME>\bin\src. Here
you will see that a file ends with *Stub.java name, this is the stub file you need. The
contents of this java file look quite complex, but you don’t need to worry about all its
contents. You will only need to know the Constructor, Parameter, and Response related
sub-classes in this file.
Step 4 - Use the Stub
Let’s write a MyWebServiceInvoker.java file now, that will invoke the Stub (which in turn
invokes the actual web-service). Assuming the name of the stub file generated was:
ServiceMallStub, and that the service we want to invoke takes one param (item id),
and returns item details when its method getItemDetails is invoked.
You will notice that parameter and the response are also inner classes in the
generated stub java file, and they have appropriate methods for your use.
Step 5 - Run
Just compile and run the MyWebServiceInvoker. To compile, you must put the
<AXIS_HOME>\lib\ jar files in the classpath. To run in addition you must have the
compiled stub classes in your classpath too. (You could also use the build.xml that is
generated along with source code for compilation).
Tested with : Apache Axis 2.1.3 and JDK 1.5.
Common Causes of Exception
A common cause of exception is HTTP Protocol mismatch (although the exception
message doesn’t give suitable hint about it!). You may have to ask your stub to use
HTTP 1.0 protocol, instead of the default HTTP 1.1 protocol it uses. You can do this in
the constructor class of the stub. For setting the required Protocol follow these steps :-
Step 1
Search for this line (in the constructor class of the stub): //Set the SOAP version
_serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP12Const
ants.SOAP_ENVELOPE_NAMESPACE_URI);
Step 2
Now, just below this line, add this line:
_serviceClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants
.HTTP_PROTOCOL_VERSION,
org.apache.axis2.transport.http.HTTPConstants.HEADER_PROTOCOL_10);
Step 3
Compile, and try again.
Web services are Web based applications that use open, XML-based standards and
transport protocols to exchange data with clients. Web services are developed using
Java Technology APIs and tools provided by an integrated Web Services Stack.
The term Web services describes a standardized way of integrating Web-based
applications using the XML, SOAP, WSDL and UDDI open standards over an Internet
protocol backbone. XML is used to tag the data, SOAP is used to transfer the data,
WSDL is used for describing the services available and UDDI is used for listing what
services are available. The SOAP engine generally used is AXIS - Apache EXtensible
Interaction System.
Web Services are used primarily as a means for businesses to communicate with each
other and with clients; Web services allow organizations to communicate data without
intimate knowledge of each other's IT systems behind the firewall.
What is SOAP?
SOAP is an XML-based communication protocol and encoding format for inter-
application communication. Originally conceived by Microsoft and Userland software, it
has evolved through several generations; the current spec is version, SOAP 1.2, though
version 1.1 is more widespread. The W3C's XML Protocol working group is in charge of
the specification.
SOAP is widely viewed as the backbone to a new generation of cross-platform cross-
language distributed computing applications, termed Web Services.
What is AXIS?
Axis is essentially a SOAP engine -- a framework for constructing SOAP processors
such as clients, servers, gateways, etc. The current version of Axis is written in Java,
but a C++ implementation of the client side of Axis is being developed.
But Axis isn't just a SOAP engine -- it also includes:
- a simple stand-alone server,
- a server which plugs into servlet engines such as Tomcat,
- extensive support for the Web Service Description Language (WSDL),
- emitter tooling that generates Java classes from WSDL.
- some sample programs, and
- a tool for monitoring TCP/IP packets.
2000, the committers of Apache SOAP v2 began discussing how to make the engine
much more flexible, configurable, and able to handle both SOAP and the upcoming
XML Protocol specification from the W3C.
Axis delivers the following key features :-
- Speed. Axis uses SAX (event-based) parsing to achieve significantly greater speed than earlier versions of Apache SOAP.
- Flexibility. The Axis architecture gives the developer complete freedom to insert extensions into the engine for custom header processing, system management, or anything else you can imagine.
- Stability. Axis defines a set of published interfaces which change relatively slowly compared to the rest of Axis.
- Component-oriented deployment. You can easily define reusable networks of Handlers to implement common patterns of processing for your applications, or to distribute to partners.
- Transport framework. We have a clean and simple abstraction for designing transports (i.e., senders and listeners for SOAP over various protocols such as SMTP, FTP, message-oriented middleware, etc), and the core of the engine is completely transport-independent.
- WSDL support. Axis supports the Web Service Description Language, version 1.1, which allows you to easily build stubs to access remote services, and also to automatically export machine-readable descriptions of your deployed services from Axis.
2.1.3.
What is a WSDL File?
The Web Services Description Language is an XML-based language that provides a
model for describing Web services.
WSDL is often used in combination with SOAP and XML Schema to provide web
services over the Internet. A client program connecting to a web service can read the
WSDL to determine what functions are available on the server.
Any special data types used are embedded in the WSDL file in the form of XML
Schema. The client can then use SOAP to actually call one of the functions listed in the
WSDL.
4.1 Using WSDL with Axis
When you make a service available using Axis, there is a unique URL associated with
that service, users may then access the service's URL with a standard web browser and
by appending "?WSDL" to the end of the URL, Axis will automatically generate a service
description for the deployed service, and return it as XML in your browser.
The resulting description may be saved or used as input to proxy-generation, described
next. You can give the WSDL-generation URL to your online partners, and they'll be
able to use it to access your service with toolkits like .NET, SOAP::Lite, or any other
software which supports using WSDL.
Steps for Writing Java Code to Invoke Web Services
Step 1 - Get Apache Axis2
Download Apache Axis. It can be downloaded from the link http://ws.apache.org/axis2/.
Just unzip it in a dir. Let’s say your Apache Axis is unzipped here: C:\axis2. Let’s call
this dir <AXIS_HOME>.
Step 2 - Get hold of the wsdl
Get hold of the wsdl file that describes the web-service. Every web-service has an
associated wsdl file (as explained above). Normally, the web-service URL with a
‘?wsdl’ suffix appended to it, will show you the wsdl file. So, if a web-service you want to
invoke is available at: http://host:port/superservice/servicemall. You can see the wsdl via
a URL: http://host:port/superservice/servicemall?wsdl
Step 3 - Create the Stub
Stub is nothing but the web-service invocation code that you are going to generate now.
Go to your command line window, change to dir: <AXIS_HOME>\bin, and run this
command:
¾ WSDL2Java -uri servicemall.wsdl
NOTE: servicemall.wsdl should be present in AXIS_HOME\bin directory. Instead of
pointing to that file, you could even use a URL format pointing to the wsdl, now the
command to be run on your command line window is:
¾ WSDL2Java -uri http://host:port/superservice/servicemall?wsdl
You will see that stub code has been generated here: <AXIS_HOME>\bin\src. Here
you will see that a file ends with *Stub.java name, this is the stub file you need. The
contents of this java file look quite complex, but you don’t need to worry about all its
contents. You will only need to know the Constructor, Parameter, and Response related
sub-classes in this file.
Step 4 - Use the Stub
Let’s write a MyWebServiceInvoker.java file now, that will invoke the Stub (which in turn
invokes the actual web-service). Assuming the name of the stub file generated was:
ServiceMallStub, and that the service we want to invoke takes one param (item id),
and returns item details when its method getItemDetails is invoked.
You will notice that parameter and the response are also inner classes in the
generated stub java file, and they have appropriate methods for your use.
public class MyWebServiceInvoker { public static void main(String[] args) { try { com.servicemall.ServiceMallStub serviceMallStub = new com.servicemall.ServiceMallStub(); com.servicemall.ServiceMallStub.MyItem theParam = new com.servicemall.ServiceMallStub.MyItem(); theParam.setItemid("11"); theParam.MyItemResponse theResp = serviceMallStub.getItemDetails(theParam); boolean result = theResp.getItemResult(); System.out.println("Result is:" + result); } catch (Throwable t) { System.out.println(t); } }
Step 5 - Run
Just compile and run the MyWebServiceInvoker. To compile, you must put the
<AXIS_HOME>\lib\ jar files in the classpath. To run in addition you must have the
compiled stub classes in your classpath too. (You could also use the build.xml that is
generated along with source code for compilation).
Tested with : Apache Axis 2.1.3 and JDK 1.5.
Common Causes of Exception
A common cause of exception is HTTP Protocol mismatch (although the exception
message doesn’t give suitable hint about it!). You may have to ask your stub to use
HTTP 1.0 protocol, instead of the default HTTP 1.1 protocol it uses. You can do this in
the constructor class of the stub. For setting the required Protocol follow these steps :-
Step 1
Search for this line (in the constructor class of the stub): //Set the SOAP version
_serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP12Const
ants.SOAP_ENVELOPE_NAMESPACE_URI);
Step 2
Now, just below this line, add this line:
_serviceClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants
.HTTP_PROTOCOL_VERSION,
org.apache.axis2.transport.http.HTTPConstants.HEADER_PROTOCOL_10);
Step 3
Compile, and try again.