This free online course will teach you how to create Java web services using SOAP and Axis. Web services are an extremely popular communication mechanism between remote systems, and a very marketable skill to have on one's resume.
Axis is open source framework for building Java web services using SOAP, which is a standard protocol for XML-based web services that allows Java-based systems to communicate remotely with other systems running other languages such as Microsoft C#.
Course Prep:
- View the Java Web Services lecture in Powerpoint format (also available here in pdf format), which will provide an overview of Java Web Services and several useful code samples.
- Install Tomcat 5.0.28 or later (follow the instructions for creating a Tomcat web application).
- Download Axis 1.4 from our friends at Apache. Inside the zip file you'll find an axis-1_4\webapps\axis folder. Install Axis by expanding the axis folder into your Tomcat webapps folder. If done correctly, you should see a webapps\axis folder on your system.
Course Exercises:
For this course, you will build a SOAP web service that provides an account balance to requesting applications in the form of a SOAP message. You will also build a client application that creates and sends a SOAP message requesting this account balance.
Verify Your Axis Installation
- Point your browser to http://localhost:8080/axis. You should see the Apache-AXIS startup page, that contains a link to List the currently deployed web services (AdminService and Version). Enter http://localhost:8080/axis/happyaxis.jsp in your browser to also verify that all required libraries have been installed.
- Axis comes with an example web service that returns a list of http application headers. Enter http://localhost:8080/axis/EchoHeaders.jws?method=list in your browser to call the "list" method on this web service. Look at the response and notice that it is encoded with standard SOAP tags that have a soapenv namespace (e.g. and ). Notice also that an element called
corresponding to our "list" request is found inside the element, and that each child element is defined as a String using the xsi:type="xsd:string" attribute. This indicates to client applications the variable type that is being returned.
- Web services also have a wsdl (Web Service Description Language) file that describes the methods that can be called on the web service, and the response that can be expected. Enter http://localhost:8080/axis/EchoHeaders.jws?wsdl in your browser to see the wsdl for this web service. Scroll down to the element called and notice that listRequest and listResponse elements are defined. Client applications would need to send a listRequest message in SOAP format, similar to the listResponse message that was returned by the web service when we called the "list" method using the browser URL.
- Open \webapps\axis\EchoHeaders.jws on your system using Wordpad or your favority text editor. Notice that it's just a regular Java class that has a .jws file extension.
- Download AccountService.jws and save it to your \webapps\axis folder. Restart Tomcat and enter http://localhost:8080/axis/AccountService.jws?wsdl in your browser to view the wsdl describing the getBalance method of this web service.
- Enter http://localhost:8080/axis/AccountService.jws?method=getBalance in your browser to call the getBalance method and return a hard-coded dollar amount. Notice that Axis automatically handled the creation of the wsdl file, and loaded the AccountService as a SOAP web service.
SOAP Web Service
- Create a Java class called AccountService that contains a single method called public float getAccountBalance(String username, String password) that returns a hard-coded fictitious account balance. No database connection is required for this assignment, though you are welcome to use the DatabaseManager from the Java Course: JDBC MySQL Database Connectivity course if you'd like your web service to look up an actual account balance from the database.
- Add System.out.println statements to output the username and password to the JBoss or Tomcat output console.
- Convert your Java class to a web service by copying it into your <tomcat>\webapps\axis.war subfolder.
- Verify your Axis installation and Java web service creation using http://localhost:8080/axis/AccountService.jws
- View the wsdl for your AccountService using http://localhost:8080/axis/AccountService.jws?wsdl
- Execute your AccountService using http://localhost:8080/axis/AccountService.jws?method=getAccountBalance&username=Greg&password=biffle and view the output console to verify that the username and password values were received by the AccountServer.
SOAP Client
- Create a J2EE 1.4 project called assign10.
- Add the built-in Axis library to your project by selecting Project --> Properties --> Java Build Path --> Libraries --> Add Library.
- Set Eclipse to use the Java 1.4 SDK, as the built-in Axis libraries do not work with Java 1.5. Alternatively, you can add axis.jar to your project from the Axis 1.4 download, which does work with Java 1.5.
- Create a stand-alone Java class called SoapClient.java with a public static void main method (so it can be run from the command line).
- Create an XML Soap Message that represents a <getAccountBalance> method call.
- Create <username> and <password> child elements containing fictitious values.
- Print the XML Soap Message request to System.out using the writeTo() method of the SoapMessage class.
- Open a connection to the web service, send the XML SOAP message request, and receive the SOAP message response.
- Print the XML Soap Message response to System.out as well.
- Get the account balance value from the response SOAP message, and print it to System.out.
- Run SoapClient.java as a Java application from within Eclipse.
Resources:
Axis User's Guide: http://ws.apache.org/axis/java/user-guide.html
SOAP API: http://ws.apache.org/axis/java/apiDocs/index.html (javax.xml.messaging and jaxax.xml.soap packages)
Timeout
Hi,
I have a standalone client application which will send a SOAP request to the webservice same as the example given in the ppt.
SOAPMessage response = sc.call(soapMsg, endPoint) ;
I want to wait for the response for 90secs.After that wants to close the call and make this as failed.
How to handle timeout in this.
Regards,
Venkat
Set a timeout on the SAAJ Call instance
You can create a Call object and set a timeout on it as follows:
(code found at http://mail-archives.apache.org/mod_mbox/ws-axis-dev/200307.mbox/%3C2003...)