Personal tools

Tutorials:NetbeansWSClient

From Adapt

Jump to: navigation, search

Creating a Webservice client in Netbeans

There are two parts to creating a web service client. First is generating stub classes from the server supplied WSDL. Second is writing code against the stubs. For ADAPT/PAWN we have chosen to create these two as seperate projects. This goes for any autogenerated code. A project should be designed to generate and compile auto-generated code, and a second project created that actually uses the stubs. That's what we'll show here.

  1. Create Stub project Create a new project 'File->New project' create a class library project and name it something along the lines of wsstuff-stubs. You'll need to add the apache axix dependancies, javax.mail, javax.activation, org.apache.axis that are available in subversion. You propabbly already loaded them into the library manager when creating the server, so just going to 'properties->build->compiling sources' and adding them through 'Add Library' should be sufficient.
  2. Load the server project up. Generally you will be developing server and client code at the same time, and the build assumes that. If you only have a wsdl file, you will have to make modifications to your build.xml file
  3. Modify stubs build.xml. Add the following to the client build.xml after the import statement::
        <!-- propject directory of web service server if loaded into netbeans as well -->
        <property 
            name="service.project.basedir"
            value="${basedir}/../WSStuff"/>
            
        <property 
            name="service.name" 
            value="WSStuff"/>
    
        <!-- Path/package of generated code, used to clean/check for changes -->
        <property 
            name="service.java.path" 
            value="edu/umiacs/sample"/>
                            
        <!-- **Do not edit** unless you have to relocate the wsdl file
             Convention would be to put the wsdl file in a directory called 
             input if necessary -->    
        <property 
            name="service.wsdl"
            value="${service.project.basedir}/web/WEB-INF/${service.name}.wsdl"/>  
    
        <!-- **Do not edit below ** -->
        <property 
            name="service.java" 
            value="${basedir}/src/${service.java.path}/${service.name}.java"/>
                    
        <target name="-pre-compile" depends="check-wsdl-uptodate,generate-stubs"/>
            
        <target name="check-wsdl-uptodate">
            <uptodate 
                property="wsdl.uptodate" 
                value="true"
                srcfile="${service.wsdl}" 
                targetfile="${service.java}"/>
        </target>
        
        <target name="generate-stubs" unless="wsdl.uptodate">
                 
            <echo message="Generating client stubs"/>
            
            <taskdef 
                resource="axis-tasks.properties" 
                classpathref="axis.classpath"/>
    
            <path id="axis.classpath">
                <pathelement path="${javac.classpath}"/>
            </path>
    
            <delete includeemptydirs="true"> 
                <fileset dir="${basedir}/src" includes="**/*.java"/>
            </delete>
            
            <axis-wsdl2java
                output="${basedir}/src"
                testcase="false"
                verbose="true"
                url="${service.wsdl}">
            </axis-wsdl2java>
                
        </target>
    
  4. Compile stubs code. Right clich on the project and make sure you can compile.
  5. Commit stubs to subversion (optional) Stubs should be commited without any files generated in src as the build process automatically generates them, and without the nbproject/private directory. Any supplied files in input/ should be commited.

  1. Create Client application. Create new class or main project. Set the build properties to include the three required axis libraries listed above as well as the stubs project you just created.
  2. Create application:
      package wsstuffclient;
      // import stubs
      import edu.umiacs.sample.*;
    
      public class Main {
        ...
        ...
        public static void main(String[] args) throws Exception {
    
            // create service locator for WSStuff
            WSStuffServiceLocator sl = new WSStuffServiceLocator();
    
            // create new WSStuff instance
            WSStuff stuff = sl.getWSStuff();
    
            // remote call to helloWorld Service
            System.out.println(stuff.helloWorld());
        }
      }
    
  3. Compile and be happy. After it works, this project can be commited to subversion as any other project.

-- Main.MikeSmorul - 02 May 2005