Example: Generating SOAP Web Services (Java)

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Implementing SOAP Web Services >

Example: Generating SOAP Web Services (Java)

This example illustrates how to generate a SOAP Web service with MapForce. You will generate Java program code from a sample MapForce project available at the following path: <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\Query Person database.mfp. This project implements a SOAP Web service that exposes two operations: getPerson and putPerson. This Web service communicates in the background with a Microsoft Access database to retrieve or add person details, respectively, through the operations above. For an example of how to create such projects in MapForce, see Creating Web Service Projects from WSDL Files.

 

This example requires Java 7 and Apache Ant to compile the generated Java code. The .aar file will be deployed to a running Apache Tomcat server with Axis2 installed.

 

For simplicity, this example uses a minimum Web server configuration and should not be taken as prescriptive. In a production environment, you may need to use a different approach as demanded by the specifics of your Web server, the security policies in your organization, and other factors.

 

To call the SOAP Web service, this example makes use of XMLSpy Enterprise Edition. If you do not have XMLSpy, you will need a SOAP client to test the Web service, or, alternatively, write program code that can call the Web service.

 

Note:As stated before, this Web service example accesses a Microsoft Access database. With Java 7, connectivity to Access databases is possible through the ODBC-JDBC bridge included in Java. However, the ODBC-JDBC bridge has been removed in Java 8, and Oracle recommends using JDBC drivers provided by the vendor, see http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html. Therefore, to run this example in a Java 8 or newer environment, you will need to find and install third-party JDBC drivers that can connect to a Microsoft Access database.

 

Step 1: Prepare the query.wsdl file

This example Web service originates from the following WSDL file: <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\query.wsdl. By default, the query.wsdl file defines the service details for C# and Java in two separate sections, one of which is commented out. Therefore, before generating Java code, locate the following section in the query.wsdl file and make sure that it is commented out:

 

  <!--service name="WS2DB">
     <port name="WS2DBSoapPort" binding="tns:WS2DBSoapBinding">
        <soap:address location="http://localhost/WS2DB/WS2DB.asmx"/>
     </port>
  </service-->

 

Also, make sure that the following section (only applicable to Java) is NOT commented out:

 

  <service name="WS2DB">
    <port name="WS2DBSoapPort" binding="tns:WS2DBSoapBinding">
        <soap:address location="http://localhost:8080/axis/services/WS2DB"/>
    </port>
  </service>

 

Step 2: Generate Java code and build it

Run MapForce and open the "Query Person database.mfp" project from the folder <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\. Right-click the project in the Project window, and select Generate code in | Java. MapForce generates program code and displays the outcome in the Messages window:

mff_soap_java_ex01

 

By default, code will be generated in a subdirectory called "output", relative to the project's directory. For the scope of this tutorial, we will leave the default settings as is. However, if you want to change the output directory, see Setting the Code Generation Settings.

 

To build the Java code, run Ant at the command line in the same directory as the build.xml file.

mff_soap_java_ex02

 

As a result, an Axis Archive (.aar) file called Query_Person_database.aar is generated in the output directory.

 

Before you can call Ant from any directory as illustrated above, it must be installed and configured correctly. Here is a quick summary (for more details, check the Ant manual from http://ant.apache.org):

 

The PATH environment variable must include the path to the "bin" subdirectory of Ant root directory.
The ANT_HOME environment variable must be set to the directory where Ant is installed.
The JAVA_HOME environment variable must be set to the directory where Java JDK is installed.
Check that Ant is running by typing "ant" at the command line. The message "Buildfile: build.xml does not exist!" means that Ant is configured and is looking for a build file.

 

Step 3: Deploy the Web service to Axis 2

 

In this example, Axis2 v1.7.6 runs on Apache Tomcat 8.5.0 installed on a local machine. In brief, Axis2 can be installed and deployed to a running Tomcat server as follows (for more details, refer to Axis2 documentation):

 

1.Download the Axis2 WAR (Web Archive) distribution from http://axis.apache.org/axis2/java/core/download.html.
2.Unpack the downloaded package to a local directory.
3.Copy the axis2.war file to Tomcat's directory %CATALINA_HOME%\webapps.
4.Check that Axis2 is running, by opening:        http://localhost:8080/axis2

 

Open the Axis2 Web administration page and upload the .aar file generated in the previous step.

mff_soap_java_ex03

A new Web service called "WS2DB" should now be available and active in the Axis2 administration page.

mff_soap_java_ex04

 

Calling the Web service

If you followed the steps above literally, then the WSDL of the Web service is available at http://localhost:8080/axis2/services/WS2DB?wsdl. To call the Web service we just deployed, we will use XMLSpy, although you can use, of course, other SOAP clients as well.

 

Run XMLSpy (Enterprise Edition). On the SOAP menu, click Create New SOAP Request. When prompted, enter the WSDL of the Web service (in this example, http://localhost:8080/axis2/services/WS2DB?wsdl).

mff_soap_java_ex05

 

Click OK. When prompted to select a SOAP Operation, select putPerson.

mff_soap_java_ex06

 

XMLSpy generates a sample request like the one below.

 

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://www.altova.com/WS2DB.xsd">
  <SOAP-ENV:Body>
    <putPerson SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <Put xsi:type="m0:Person">
          <First xsi:type="xsd:string">String</First>
          <Last xsi:type="xsd:string">String</Last>
          <Title xsi:type="xsd:string">String</Title>
          <PhoneEXT xsi:type="xsd:string">String</PhoneEXT>
          <Email xsi:type="xsd:string">String</Email>
        </Put>
    </putPerson>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

Replace "String" with the person details you want to supply as input to the Web service. For the scope of this example, you could use a request like the one below:

 

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://www.altova.com/WS2DB.xsd">
  <SOAP-ENV:Body>
    <putPerson SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <Put xsi:type="m0:Person">
          <First xsi:type="xsd:string">John</First>
          <Last xsi:type="xsd:string">Doe</Last>
          <Title xsi:type="xsd:string">Mr</Title>
          <PhoneEXT xsi:type="xsd:string">123</PhoneEXT>
          <Email xsi:type="xsd:string">john.doe@example.org</Email>
        </Put>
    </putPerson>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

You can now send the request above to the server. On the SOAP menu, click Send Request to Server. The SOAP Request Settings dialog box opens.

mff_soap_java_ex07

 

Click OK. XMLSpy initiates the call, and returns the response in the editor. The image below illustrates a successful response.

mff_soap_java_ex08

Using the same approach as described above, you can also call the getPerson operation exposed by this Web service. For step-by-step instructions, see "Calling the Web Service" section in the C# tutorial. Only the WSDL URL is different in the C# tutorial; all other instructions for calling the Web service are identical.


© 2019 Altova GmbH