Tutorial Writing and Calling Web Services using Java - PowerPoint

Document Sample
Tutorial Writing and Calling Web Services using Java - PowerPoint Powered By Docstoc
					 Methodologies in the Development of Information
                     Systems




            Tutorial:
Writing and Calling Web Services
           using Java


               Eran Toch
             November 2004
                                            Agenda
•   AXIS Introduction
•   Installing Tomcat
•   Installing AXIS
•   Deploying a Web service
•   Running a Client
•   Creating Server and Client
    Stubs



Writing and Calling Web Services using Java – Eran Toch   2
Methodologies in Information System Development
             Axis Web Services Architecture

                                              Client

                                          JAX-RPC

                                        http port 80


                                   Apache Tomcat

                                             AXIS


                                           Service        Application
Writing and Calling Web Services using Java – Eran Toch                 3
Methodologies in Information System Development
                             Tomcat Installation

• Go to: http://jakarta.apache.org/tomcat/
• Download the latest version (currently 5.5.4)
  by going to: Downloads -> Binaries ->
  Tomcat 5.5.4.
      – For Windows, download the exe version – with a
        Windows setup. The direct link:
        http://apache.fresh.co.il/jakarta/tomcat-
        5/v5.5.4/bin/jakarta-tomcat-5.5.4.exe
      – For Linux, read the setup manual:
        http://jakarta.apache.org/tomcat/tomcat-5.5-
        doc/setup.html
Writing and Calling Web Services using Java – Eran Toch   4
Methodologies in Information System Development
                              Tomcat and Java 5

• Please note that Tomcat v. 5.5.4 requires
  J2SE 1.5 (also known as J2SE 5) or above.
• If you want to use J2SE 1.4, download
  Tomcat 5.0.+




Writing and Calling Web Services using Java – Eran Toch   5
Methodologies in Information System Development
                   Tomcat Installation – cont’d

If you want Tomcat to
startup every time the
computer is rebooted,
check the “service” option.
If you wish to use Tomcat
for only for development,
it’s best to leave the
service option unchecked.




 Writing and Calling Web Services using Java – Eran Toch   6
 Methodologies in Information System Development
                      Tomcat Installation - Port

You can configure the port
that Tomcat will be using. If
you want it to be available
publicly (behind firewalls
etc), change the default port
to 80. Otherwise, leave it as
it is (880). You can always
change it later, in the
[TOMCAT_HOME]/conf/serv
er.xml configuration file.




 Writing and Calling Web Services using Java – Eran Toch   7
 Methodologies in Information System Development
                                 Running Tomcat

• Start Tomcat by running Tomcat monitor
  (Start Menu -> Apache Tomcat -> Monitor
  Tomcat), and click on Start.
     – Point your browser to:
       http://localhost:8080. If
       Tomcat works, you should
       see something like this
     – If not, check out the logs
       (C:\[TOMCAT_HOME]\logs\s
       tdout) and see what went
       wrong.
Writing and Calling Web Services using Java – Eran Toch   8
Methodologies in Information System Development
                               Managing Tomcat

• Go to the management console by clicking
  on the “management” link in the Tomcat
  root homepage, or directly by going to:
  http://localhost:8080/manager/html.
• The default username and password are
  “admin”/””. You can change it by changling
  the [TOMCAT_HOME]\conf\tomcat-
  users.xml.


Writing and Calling Web Services using Java – Eran Toch   9
Methodologies in Information System Development
                    Managing Tomcat Console


                                                   Start, stop, restart
                                                   and undeploy
                                                   applications


Web Application                     Number of
management                          current
                                    sessions




 Writing and Calling Web Services using Java – Eran Toch                  10
 Methodologies in Information System Development
                                     Apache AXIS

• A SOAP Processing Engine
     – JAX-RPC Client System
     – JAX-RPC Server System ( Servlet based )
     – SAAJ implementation
     – Flexible and extensible architecture
     – Tools, Examples, Documentation, …
     – A great place to learn about Web Services !!
• Open-source, hosted by Apache
  Software Foundation
• Packages for Java and C++
Writing and Calling Web Services using Java – Eran Toch   11
Methodologies in Information System Development
                        Download Apache Axis

• Make sure that you have
   – J2SE SDK 1.4 or later
   – Tomcat
• Download latest version (currently 1.1) from
  http://ws.apache.org/axis/. Direct Link:
  http://ws.apache.org/axis/download.cgi
• For Windows, download the “Binary zip”
  version.
• Unzip it somewhere.

Writing and Calling Web Services using Java – Eran Toch   12
Methodologies in Information System Development
                                       Deploy Axis

• Copy webapps\axis                                                    Direcotry Structure:

                                                                          axis-1_1
  tree to webapps
  directory of Tomcat.                                    webapps        lib    docs   samples


• Alternatively, modify                                    axis

  server.xml of Tomcat.                                   WEB-INF

• Run, or restart, Tomcat.                                        lib

                                                                  classes

                                                                  web.xml
                                                                  ……




Writing and Calling Web Services using Java – Eran Toch                                       13
Methodologies in Information System Development
                           Test the Deployment

• Point your browser to http://localhost:8080/axis,
  you should see something like this:

      Click on “Validate” in
      order to see if the
      installation went all
      right


     Click on “view” to see
     all the current
     deployed web
     services


Writing and Calling Web Services using Java – Eran Toch   14
Methodologies in Information System Development
                           Installation problems

• For example, if “activation.jar” is missing,
• Download it from:
  http://java.sun.com/products/javabeans/glasg
  ow/jaf.html
• Unzip it
• Copy “activation.jar” to
  [TOMCAT_HOME]/webapps/axis/WEB-
  INF/lib


Writing and Calling Web Services using Java – Eran Toch   15
Methodologies in Information System Development
                          Deploy a Web Service
Create a Java class using this code:
                               public class AddFunction {
                                 public int addInt(int a, int b){
                                   return (a+b);
                                 }
                               }



• Name the file AddFunction.jws. Notice the filename extension
  – it is .jws ( for Java Web Service). Make sure the name of the
  file is identical to the name of the Java class.
• Deploy it by copying the file to webapps/axis/services
  directory.
• That’s it!

Writing and Calling Web Services using Java – Eran Toch             16
Methodologies in Information System Development
                                   The WSDL File

• Examine its WSDL description. Point your browser
  to http://localhost:8080/axis/AddFunction.jws?wsdl
            - <wsdl:message name="addIntResponse">
             <wsdl:part name="addIntReturn" type="xsd:int" />
             </wsdl:message>
            <wsdl:message name="addIntRequest">
             <wsdl:part name="a" type="xsd:int" />
             <wsdl:part name="b" type="xsd:int" />
             </wsdl:message>
            <wsdl:portType name="AddFunction">
            <wsdl:operation name="addInt" parameterOrder="a b">
             <wsdl:input message="impl:addIntRequest" name="addIntRequest" />
             <wsdl:output message="impl:addIntResponse" name="addIntResponse" />
             </wsdl:operation>
             </wsdl:portType>


Writing and Calling Web Services using Java – Eran Toch                            17
Methodologies in Information System Development
                                   WSDL Refresh

   • A WSDL document                                       WSDL
                                                           Document
     describes
          – What the service can do                       [Types]

          – Where it resides                              {Messages}
          – How to invoke it
   • Defines binding for                                  {Port Types}

     SOAP1.1, HTTP                                        {Bindings}

     GET/POST and MIME
                                                          {Services}




Writing and Calling Web Services using Java – Eran Toch          18
Methodologies in Information System Development
      Debugging the Service with XMLSpy

• In XMLSpy, Click on SOAP -> Create new
  SOAP request.
• Find the WSDL File.
• Choose the operation (there is a single one)




Writing and Calling Web Services using Java – Eran Toch   19
Methodologies in Information System Development
                             Debugging – cont’d

This is how a SOAP message will look like:
      <SOAP-ENV:…>
             <SOAP-ENV:Body>
                    <m:addInt xmlns:m=http://DefaultNamespace…>
                            <a xsi:type="xsd:int">0</a>
                            <b xsi:type="xsd:int">0</b>
                    </m:addInt>
             </SOAP-ENV:Body>
                                                   Change the default
      </SOAP-ENV:Envelope>
                                                          operation values


Click on SOAP -> Send Request to Server

Writing and Calling Web Services using Java – Eran Toch                      20
Methodologies in Information System Development
                             Debugging – cont’d
    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope…>
      <soapenv:Body>
        <ns1:addIntResponse…>
           <ns1:addIntReturn xsi:type="xsd:int">9</ns1:addIntReturn>
        </ns1:addIntResponse>
      </soapenv:Body>
    </soapenv:Envelope>




• This is the SOAP message that was returned
  from the Server


Writing and Calling Web Services using Java – Eran Toch                21
Methodologies in Information System Development
                   Writing the Client Program

• There are many ways to write a Client
  program
      – Using Dynamic Invocation Interface (DII)
      – Using generated Stubs from Service WSDL description




Writing and Calling Web Services using Java – Eran Toch       22
Methodologies in Information System Development
               Client – using DII with Eclipse

 • Create a new
   Eclipse Java
   Project
 • Add all the jars
   under axis_1-1/lib
   to the java build
   path libraries
   (using external
   jars)
 • Create a new
   class, called
   “Client”


Writing and Calling Web Services using Java – Eran Toch   23
Methodologies in Information System Development
                                Client – using DII
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import javax.xml.namespace.QName;

public class Client {
  public static void main(String[] args) {
    try {
      String endpoint = "http://localhost:8080/axis/AddFunction.jws";
      Service service = new Service();
      Call call = (Call) service.createCall();
      call.setOperationName(new QName(endpoint, "addInt"));
      call.setTargetEndpointAddress(new java.net.URL(endpoint));
      Integer ret = (Integer) call.invoke(new Object[] { new Integer(5), new Integer(6) });
      System.out.println("addInt(5, 6) = " + ret);
    } catch (Exception e) {
      System.err.println("Execution failed. Exception: " + e);
    }
  }
}


Writing and Calling Web Services using Java – Eran Toch                                       24
Methodologies in Information System Development
                                   Client - Output



                       addInt(5, 6) = 11




Writing and Calling Web Services using Java – Eran Toch   25
Methodologies in Information System Development
                   Creating Server-Side Stubs

• Use the WSDL2Java command:

       java org.apache.axis.wsdl.WSDL2Java
        --server-side --skeletonDeploy true SRM.wsdl


• Make sure that Axis’ jars are on the classpath
• Another option is to use Eclipse instead




Writing and Calling Web Services using Java – Eran Toch   26
Methodologies in Information System Development
          Quick and Dirty: Using Eclipse for
                Running WSDL2Java
• Open a new
  Java project
• Add all the Axis
  libraries
• Configure a
  Run setting
• Check the
  “Include
  external jars
  option”
Writing and Calling Web Services using Java – Eran Toch   27
Methodologies in Information System Development
                       Quick and Dirty – con’d

Set the
arguments:




Writing and Calling Web Services using Java – Eran Toch   28
Methodologies in Information System Development
                               Server-side Stubs

• The following files will be created:
      –   deploy.wsdd
      –   undeploy.wsdd
      –   SRM.java
      –   SRMService.java
      –   SRMSoapBindingSkeleton.java
      –   CategoryType.java
      –   SRMMessage.java
      –   StaffMemberList.java                            If Eclipse was used,
      –   SRMServiceLocator.java                          Don’t forget to
                                                          refresh the project in
      –   SRMSoapBindingImpl.java
                                                          order to see the files
      –   SRMSoapBindingStub.java

Writing and Calling Web Services using Java – Eran Toch                            29
Methodologies in Information System Development
                       Implement Functionality

• Change the code of SRMSoapBindingImpl,
  implementing the operations:
         public StaffMemberList getStaffMemberList()
           throws java.rmi.RemoteException {
              MySRM mySrmApp = new MySRM();
              String[] members = mySrmApp.getTAs();
              StaffMemberList list = new StaffMemberList();
              list.setStaffMemberName(members);
               return list;
         }




Writing and Calling Web Services using Java – Eran Toch       30
Methodologies in Information System Development
                              Deploy the Service

• Copying:
      – Copy the package (edu.technion…) from where it was created (for
        instance, C:\eclipse\workspace\SRM-WSDL) to:
        [TOMCAT-HOME]/webapps/axis/WEB-INF/classes.
      – It is not important to copy the two wsdd files.
• Run the AdminClient program:

     java org.apache.axis.client.AdminClient deploy.wsdd


• deploy.wsdd should be with the full path, of course


Writing and Calling Web Services using Java – Eran Toch             31
Methodologies in Information System Development
                         Checking Deployment

• Go to http://localhost:8080/axis/servlet/AxisServlet
  and see that the service is actually deployed.
• If not, checkout [TOMCAT-HOME]/logs/stdout for
  errors.
• If the following error occurred, then maybe
  the classes were not copied correctly

- Unable to deploy typemapping: {http://ie.technion.edu/methodologies/srm/}SRMMessage
java.lang.ClassNotFoundException: edu.technion.ie.methodologies.srm.SRMMessage




Writing and Calling Web Services using Java – Eran Toch                           32
Methodologies in Information System Development
                          Creating Client Stubs

• Use the WSDL2Java command:

       java org.apache.axis.wsdl.WSDL2Java SRM.wsdl

• Make sure that Axis’ jars are on the classpath
  (or use Eclipse instead)




Writing and Calling Web Services using Java – Eran Toch   33
Methodologies in Information System Development
                           Client Stub Structure

• The following files will be created:
      –   SRM.java
      –   SRMService.java
      –   SRMSoapBindingSkeleton.java
      –   CategoryType.java
      –   SRMMessage.java
      –   StaffMemberList.java
      –   SRMServiceLocator.java
      –   SRMSoapBindingStub.java


Writing and Calling Web Services using Java – Eran Toch   34
Methodologies in Information System Development
            Write a client that uses the Stub
package edu.technion.ie.methodologies.srm;

import javax.xml.rpc.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.*;

public class SRMClient {
            public static void main(String[] args) {
                         SRMService srmLocator = new SRMServiceLocator();
                         try         {
                                     URL srmUrl = new URL("http://localhost:8080/axis/services/SRM");
                                     SRM service = srmLocator.getSRM(srmUrl);
                                     StaffMemberList list = service.getStaffMemberList();
                                     String[] members = list.getStaffMemberName();
                                     for (int i=0; i<members.length; i++) {
                                                    System.out.println(members[i]);
                                     }
                         }
                         catch (MalformedURLException mue)                {System.out.println(mue);}
                         catch (ServiceException ex)          {System.out.println(ex);}
                         catch (RemoteException rex)          {System.out.println(rex);}
            }
}
Writing and Calling Web Services using Java – Eran Toch                                            35
Methodologies in Information System Development