Creating a Web service using the WSDL2WebService tool

You can use the WSDL2WebService tool from a command line to generate a Web service from a WSDL file using the IBM WebSphere run-time environment.

Prerequisites:

You use the tool in the following three stages:

  1. Run the tool with the -createService argument to create skeleton Java implementation templates for a Web service described by a particular WSDL document.

  2. Write your implementation code in the templates and compile it using a build script generated in stage 1.

  3. Run the tool again with the -createEar argument to build a Web-service enabled archive from this implementation.

Note: You can perform stage 1 several times to create related Web services in the same project directory. In stage 3, you can then create a separate module for each of these Web services and add these modules to the same EAR file.

WSDL2WebService tool reference
You can use the WSDL2WebService tool from a command line to generate a Web service from a WSDL file using the IBM WebSphere run-time environment.

 

Parent topic

Creating a Web service with command line tools

Command line setup

Before using the WSDL2WebService tool, set up the Web services command line as follows:

  1. From a command prompt, change to Rational Developer product's bin directory.

  2. Run the following batch script:

    • Windows: setupenv.bat

    • Linux: . setupenv.sh

  3. Change to the directory where you want to create the Web service. We will refer to this directory as the command line test directory. Linux: You must have write-access to this directory.

Now, you can run stage 1 of the WSDL2WebService tool.

Creating a skeleton Web service implementation

Note: There are two versions of the WSDL2WebService tool. If you run the WSDL2WebService command, the command will use J2EE 1.4. Alternately you can use the WSDL2WebService13 command which will use J2EE 1.3. For any of the commands below you can substitute WSDL2WebService13 instead of WSDL2WebService if you want to create a Web service that uses J2EE 1.3.

To create a skeleton Web service implementation, enter the following in the command prompt:

  • Windows: WSDL2WebService [<optional arguments>] -createService <ServiceName> -project <ProjectDir> <WSDL URI>

  • Linux: WSDL2WebService.sh [<optional arguments>] -createService <ServiceName> -project <ProjectDir> <WSDL URI>

Note: The final argument must be <WSDL URI>, which is case-sensitive. See The WSDL2WebService tool for a list of optional and mandatory arguments.

Hints:

  • It is recommended that you specify the optional argument: -host <hostname:port>. The default port for the command is 6080, but the default WebSphere Application Server port is 9080.

  • Note the command line test directory path and the name that you specify for -project. You will need this information to deploy and test your Web service.

After you have run the tool with the -createService <ServiceName> argument, a directory named <ServiceName> containing several subdirectories is created under the specified project. These subdirectories contain all the necessary Java templates that are required to build the Web service implementation.

This is an example of how to use the WSDL2WebService tool:

WSDL2WebService -createService ejb1Service -project dir:\myproj\ejbDir -host localhost:9080 -contextRoot ejb1Service -type EJB -verbose dir:\TempConv.wsdl

Compiling the implementation code

You need to modify the skeleton Java files so that they correctly implement the Web service you are creating. Once you have added the correct code to the Java skeletons they need to be compiled. A build script called compile is also generated to help you compile all this code.

To compile the implementation code:

  1. Change to the <ServiceName> directory.

  2. If your implementation code has dependencies such as a JAR file or a directory containing .class files, edit the compile script and add the full path names of these dependencies to the USER_CLASSPATH variable, for example:

    • Windows: set USER_CLASSPATH = c:\MyJars\webservice.jar;c:\MyClasses

    • Linux: USER_CLASSPATH = /home/MyJars/webservice.jar:/home/MyClasses

  3. Enter the following command:

    • Windows: compile

    • Linux: compile.sh

    Note that no parameters are required to run the command.

Creating a Web services enabled archive

Once you have created the service, added the implementation code to the Java skeletons, and compiled the new code, you need to create a Web services enabled archive To create a Web-services enabled archive, enter the following in the command prompt:

  • Windows: WSDL2WebService [<optional arguments>] -createEar <File.ear> -project <ProjectDir>

  • Linux: WSDL2WebService.sh [<optional arguments>] -createEar <File.ear> -project <ProjectDir>

See The WSDL2WebService tool for an explanation of optional and mandatory arguments.

Hint: Note the command line test directory path and the name that you specify for -project if you want to deploy and test your Web service.

After you have written the implementation code, compiled it, and run the tool again with the -createEar <File.ear> argument, the output will be either a new or updated EAR file. This will contain a Web service module for each of the Web service names specified with the -add argument. You will also notice that the server-side classes that implement the Web service have been moved to the WEB-INF\classes directory, which is the normal location. If you do not specify the -server-side-only argument, a client-side directory that contains some extra Java files, including serializer and deserializer classes for your complex types, is also created.

Once you have generated an EAR file, you can deploy it to a server to make it available to others.

Here is an example of how to create a Web-services enabled archive:

WSDL2WebService -createEAR dir:\myear\ejb1ear.ear -project dir:\myproj\ejbDir -add ejb1Service -verbose localhost:9080/ejb1Service/services/TempConv

Support for reusing code

The WSDL2WebService tool supports the reuse of code by allowing you to place JAR or EJB files (which are accessed by one or more of the contained Web services) in the root of an EAR file. You can reuse a Web services implementation by archiving all the classes into a JAR file of the same name as the service. Do the following steps to archive the classes into a JAR file:

  1. After you have run the WSDL2WebService tool, put the JAR files that you want to reuse into the root of the project directory. Note that these may be JAR or EJB files.

  2. For each Web service in your project that requires one of these JAR files, update the USER_CLASSPATH variable in its compile script by adding the full path to the JAR file.

  3. Compile each Web service normally.

  4. Run the WSDL2WebService tool with the -createEAR argument normally. The WSDL2WebService tool will automatically add the JAR files to the EAR file, and update all references to these JAR files.

Important:

  • If you run the WSDL2WebService command to create an EJB Web Service using a set of WSDL files that do a local import the EAR file will be generated but an exception will occur during server start-up once it is imported to the workspace and deployed to the Unit Test Environment or a remote server. This exception is caused by a missing WSDL in the router Web project. To counteract this error do any of the following:

    • Deploy to a stand-alone WebSphere Application Server using the Admin Console.

    • Use a monolithic WSDL - that is, do not use the -splitWsdl argument.

    • Manually copy all WSDL files under the META-INF/wsdl directory in the EJB project to the WEB-INF/wsdl of the router Web project.

  • If you select the -genResolver argument, the EAR cannot be started. The problem is that the -genResolver option cannot handle cases in which a WSDL document imports an XSD using a <wsdl:import>. To avoid this problem do one of the following:

    • Edit the WSDL to use a <types><schema><import> to import the XSD.

    • Do not use the -genResolver option on the command line tool.

Related Tasks
Creating a Web service with command line tools
Testing a Web service generated by the command line

Related Reference
WSDL2WebService tool reference