Creating a Web Service from a Class File - XML

In most real-life situations, deploying a Web service as a .jws file is insufficient. This implies that these Web services are not able to meet the requirements of a real-life Web service, such as the ability to specify custom type mappings or create handler chains. Therefore, in such complicated cases, a deployment file, as shown in the following code, is used:

Save the file as deploy.wsdd.

In the preceding code, we created a new Web service that uses the Java class: CCheck. Then, the code exposes the isValid() method.

You can expose several methods in the preceding code. To do so, you need to separate the names of methods with spaces.

The next step is to deploy the Web service. The steps to do so are as shown:

  1. Start the Tomcat server if it is not working.
  2. Compile the CCheckServer.Java file.
  3. Copy the resulting CCheckServer class to the TOMCAT _ HOME webapps axis WEBINFclasses folder.
  4. Execute the following command:
  5. java org.apache.axis.client.AdminClient deploy.wsdd

In this case, the requestor and provider applications (endpoints in a Web services scenario) are AxisServlet instead of .jws files. Now, the servlet locates and creates an instance of the class that corresponds to the service name CCheckServer. Next, the servlet converts the SOAP messages into Java calls and invokes the Java calls on the instantiated service class.

Testing the Web Service

Now you can test the Web service created in the previous section by using a client application. The client application used in this case is similar to the client application used to test the Web service in which the endpoints were .jws files. However, please note the change in the endpoint.

The differences in the preceding code and the code used to call a Web service with endpoints as .jws files are in bold type.

Creating a Client Application by Using Axis Tools

In addition to the client application discussed in the previous section, you can create a client application by using the tools that the Axis toolkit provides. You can then use this client application to test the Web service.

To create a client application by using the tools in the Axis toolkit, you first need to create a proxy class for the Web service. To do this, use the following command:

The preceding code uses the Wsdl2java command. Some of the commonly used options with the Wsdl2java command are discussed in Table.

Options Used with the Wsdl2java Command

Options Used with the Wsdl2java CommandOptions Used with the Wsdl2java Command

When you execute the Wsdl2java command, a folder named proxy, which contains three files, is created. The files in the proxy folder represent the SOAP bindings, the port type, and the Web service proxy class. In addition, the Wsdl2java command creates the following:

  • A Java class for each type
  • A Java interface for each port type
  • A stub class for each binding
  • A service interface and implementation for the service interfaces, also called the locator class

The following section discusses the contents of these files in detail.

File That Contains Port Types

As discussed, the Wsdl2java command creates a file that contains a Java class, CCheckServerPortType.Java. The code for this file is as follows:

File That Contains Bindings

In addition, a file that contains stubs is created. The content of the CCheckServerSoapBinding file is as follows:

File That Contains the Web Service Proxy Interface Class

The Wsdl2java command creates a file that contains the code for the Web service proxy class. The content of the CCheckServer.Java file is shown in the following code:

File That Contains the Web Service Proxy Implementation

A client program does not instantiate a stub; it would rather instantiate a service locator class, which exposes a method that would return a stub. This locator class implements the service interface class shown earlier. Following is the code for a service locator class for the CCheck Web service:

Compiling the Files That the Wsdl2java Command Created

When these files are created, you need to compile them in the following order:

  1. java proxy/
  2. java proxy/
  3. javac proxy/

Create a Client Application That Uses the Files

Next, you need to create a client application that calls the Web service by using the classes that are contained within the three files discussed in the previous sections. The code for the client application is as follows:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

XML Topics