In this lesson, you process 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.
Prerequisites
This lesson depends on the steps carried out in lesson 1. See Lesson 1: Setting up a web server to receive SOAP requests and send SOAP responses.
This lesson assumes that you have the roles and privileges listed in the Privileges section at the start of this tutorial: Tutorial: Using JAX-WS to access a SOAP/DISH web service.
Context and remarks
At the time of writing, JAX-WS is included in JDK 1.7.0 and the most recent version of JAX-WS was 2.2.7. The steps that follow
are based on that version. To determine if JAX-WS is present in your JDK, check for the wsimport application in the JDK bin directory. If it is not there then go to http://jax-ws.java.net/ to download and install the latest version of JAX-WS.
This lesson contains several references to localhost. Use the host name or IP address of the web server from lesson 1 instead of localhost if you are not running the web client on the same computer as the web server.
At a command prompt, create a new working directory for your Java code and generated files. Change to this new directory.
Generate the interface that calls the DISH web service and imports the WSDL document using the following command:
wsimport -keep "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. The generated Java source code allows you to understand the generated class files.
The wsimport application creates a new subdirectory structure named localhost\_8082\demo\ws in your current working directory. The following is a list of the contents of directory ws:
EmployeeList.class
EmployeeList.java
EmployeeListDataset$Rowset$Row.class
EmployeeListDataset$Rowset.class
EmployeeListDataset.class
EmployeeListDataset.java
EmployeeListResponse.class
EmployeeListResponse.java
FaultMessage.class
FaultMessage.java
ObjectFactory.class
ObjectFactory.java
package-info.class
package-info.java
WSDish.class
WSDish.java
WSDishSoapPort.class
WSDishSoapPort.java
Write a Java application that accesses table data from the database server based on the dataset object schema defined in the generated source code.
The following is a sample Java application that does this. Save the source code as SASoapDemo.java in the current working directory. Your current working directory must be the directory containing the localhost subdirectory.
// SASoapDemo.java illustrates a web service client that // calls the WSDish service and prints out the data. import java.util.*; import javax.xml.ws.*; import org.w3c.dom.Element; import org.w3c.dom.Node; import javax.xml.datatype.*; import localhost._8082.demo.ws.*; 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 = row.getTerminationDate().getDeclaredType().getSimpleName(); fieldName = row.getTerminationDate().getName().getLocalPart(); fieldDate = row.getTerminationDate().getValue(); } System.out.println( "(" + fieldType + ")" + fieldName + "=" + fieldDate ); System.out.println(); } } catch (Exception x) { x.printStackTrace(); } } } |
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 localhost._8082.demo.ws.* 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 API documentation
at http://docs.oracle.com/javase/.
Compile your Java application using the following command:
javac SASoapDemo.java |
Execute the application using the following command:
java SASoapDemo |
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.
![]() |
Discuss this page in DocCommentXchange.
|
Copyright © 2014, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0 |