Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » SQL Anywhere Server - Programming » HTTP web services » HTTP web service examples » Tutorial: Using JAX-WS to access a SOAP/DISH web service


Lesson 3: Creating a Java application to communicate with the web server

In this lesson, you import the WSDL document generated from the DISH service and create a Java application to access table data based on the schema defined in the WSDL document.

 Import a WSDL document from the database server
  1. At a command prompt, create a new working directory for your Java code and generated files. Change to this new directory.

  2. Generate the interface that calls the DISH web service and imports the WSDL document using the following command:

    wsimport -keep -Xendorsed "http://localhost:8082/demo/WSDish"

    The wsimport application retrieves the WSDL document from the given URL. It generates .java files to create an interface for it, then compiles them into .class files.

    The keep option indicates that the .java files should not be deleted after generating the class files. These files allow you to understand the generated source code.

    The Xendorsed option enables use of the endorsed standards overloaded in a previous lesson.

The wsimport application creates a new subdirectory structure named localhost\_8082\demo\ws in your current working directory. The following is a list of directory contents:

  • EmployeeList.class


  • EmployeeListDataset$Rowset$Row.class

  • EmployeeListDataset$Rowset.class

  • EmployeeListDataset.class


  • EmployeeListResponse.class


  • FaultMessage.class


  • ObjectFactory.class


  • package-info.class


  • WSDish.class


  • WSDishSoapPort.class


 Access database information from the server
  1. Write a Java application that accesses table data from the database server based on the dataset object schema defined in the generated API.

    Save the following Java source code as in the current working directory. Note that your current working directory must be the directory containing the localhost subdirectory.

    // illustrates a web service client that
    // calls the WSDish service and prints out the data.
    import java.util.*;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import javax.xml.datatype.*;
    public class SASoapDemo
      public static void main( String[] args )
        try {
          WSDish service = new WSDish();
          Holder<EmployeeListDataset> response = 
              new Holder<EmployeeListDataset>();
          Holder<Integer> sqlcode = new Holder<Integer>();
          WSDishSoapPort port = service.getWSDishSoap();
          // This is the SOAP service call to EmployeeList
          port.employeeList( response, sqlcode );
          EmployeeListDataset result = response.value;
          EmployeeListDataset.Rowset rowset = result.getRowset();
          List<EmployeeListDataset.Rowset.Row> rows = rowset.getRow();
          String fieldType;
          String fieldName;
          String fieldValue;
          Integer fieldInt;
          XMLGregorianCalendar fieldDate;
          for ( int i = 0; i < rows.size(); i++ ) {
            EmployeeListDataset.Rowset.Row row = rows.get( i );
            fieldType = row.getEmployeeID().getDeclaredType().getSimpleName();
            fieldName = row.getEmployeeID().getName().getLocalPart();
            fieldInt = row.getEmployeeID().getValue();
            System.out.println( "(" + fieldType + ")" + fieldName + 
                                "=" + fieldInt );
            fieldType = row.getSurname().getDeclaredType().getSimpleName();
            fieldName = row.getSurname().getName().getLocalPart();
            fieldValue = row.getSurname().getValue();
            System.out.println( "(" + fieldType + ")" + fieldName + 
                                "=" + fieldValue );
            fieldType = row.getGivenName().getDeclaredType().getSimpleName();
            fieldName = row.getGivenName().getName().getLocalPart();
            fieldValue = row.getGivenName().getValue();
            System.out.println( "(" + fieldType + ")" + fieldName + 
                                 "=" + fieldValue );
            fieldType = row.getStartDate().getDeclaredType().getSimpleName();
            fieldName = row.getStartDate().getName().getLocalPart();
            fieldDate = row.getStartDate().getValue();
            System.out.println( "(" + fieldType + ")" + fieldName + 
                                "=" + fieldDate );
            if ( row.getTerminationDate() == null ) {
              fieldType = "unknown";
              fieldName = "TerminationDate";
              fieldDate = null;
            } else {
              fieldType = 
              fieldName = row.getTerminationDate().getName().getLocalPart();
              fieldDate = row.getTerminationDate().getValue();
            System.out.println( "(" + fieldType + ")" + fieldName + 
                                "=" + fieldDate );
        catch (Exception x) {

    This application prints all server-provided column data to the standard system output.


    This application assumes that your SQL Anywhere web server is listening on port 8082, as instructed in lesson one. Replace the 8082 portion of the import* code line with the port number you specified when you started the SQL Anywhere web server.

    For more information about the Java methods used in this application, see the javax.xml.bind.JAXBElement class documentation at [external link]

  2. Compile your Java application using the following command:

  3. Execute the application using the following command:

    java SASoapDemo
  4. The application sends its request to the web server. It receives an XML result set response that consists of an EmployeeListResult with a rowset containing several row entries. The SQLCODE result from executing the query is included in the response.

The following is an example of the generated output:




The TerminationDate column is only sent when its value is not NULL. The Java application is designed to detect when the TerminationDate column is not present. For this example, the last row in the Employees table was altered such that a non-NULL termination date was set.

The following is an example of a SOAP response from the web server:

 <tns:EmployeeListResult xsi:type='tns:EmployeeListDataset'>
    <tns:row> ... </tns:row>
      <tns:EmployeeID xsi:type="xsd:int">1751</tns:EmployeeID>
      <tns:Surname xsi:type="xsd:string">Ahmed</tns:Surname>
      <tns:GivenName xsi:type="xsd:string">Alex</tns:GivenName>
      <tns:StartDate xsi:type="xsd:dateTime">1994-07-12</tns:StartDate>
      <tns:TerminationDate xsi:type="xsd:dateTime">2010-03-22</tns:TerminationDate>

Column names and data types are included in each rowset.


You can observe the response shown above through the use of proxy software that logs the XML message traffic. The proxy inserts itself between your client application and the web server.