Creating a JAX-WS Web Service from an Existing WSDL File } }Files Prepare Run Troubleshooting Related Topics
About the Example
This example shows how to use the wsdlc and jwsc Ant tasks in combination to create a JAX-WS based Web Service based on an existing WSDL file.
For the sake of clarity, the WSDL file is very simple. The Web service is called WarehouseService, and it contains a single operation ShipGoods that takes as inputs a product number, quantity and customer name and returns the status of the shipment.
The example shows how to first execute the wsdlc Ant task, which takes as input the WSDL file, and, based on the operations, parameters, return values, and so on in the WSDL, generates a set of artifacts that together provide a partial implementation of the JAX-WS based Web Service described by the WSDL file. These components include:
- A JWS interface file that implements the Web Service described by the WSDL file. The wsdlc task packages the interface into a JAR file, located in the directory specified by the destJwsDir attribute of wsdlc. Other than later specifying this file to the jwsc Ant task, you never need to look at this JAR file.
- Data binding artifacts used by Oracle WebLogic Server to convert between the XML and Java representations of the Web Service parameters and return values. The XML Schema of the data types is specified in the WSDL, and the Java representation is generated by the wsdlc Ant task. The wsdlc Ant task packages these artifacts into the same JAR file which contains the JWS interface, located in the directory specified by the destJwsDir attribute of wsdlc. You pass this file to the jwsc Ant task later; otherwise, you never need to look at this JAR file.
- A JWS file that contains a stubbed-out implementation of the generated JWS interface. You edit this file to add your Java business logic code to make the Web Service behave as you want.
The wsdlc task generates this JWS implementation file in the directory specified by the destImplDir attribute. The subdirectory hierarchy corresponds either to the packageName attribute or to the targetNamespace URL of the WSDL if the packageName attribute is not specified. The name of the JWS file is ServiceName_PortTypeNameImpl.java, where ServiceName and PortTypeName refer to the value of the name attribute of the appropriate <service> and its inner <port> element, respectively, in the WSDL file. The names of the methods and parameters in the JWS file are the same as the names of the operations and input parmeters in the WSDL file.
- Optional Javadocs for the generated JWS interface, generated in the directory specified by the destJavadocDir attribute.
The example shows how to use the type attribute of the wsdlc Ant task to generate a partial JAX-WS implementation of a Web Service, as opposed to the default JAX-RPC Web Service.
After running the wsdlc Ant task and updating the wsdlc-generated JWS file, the example shows how to run the jwsc Ant task against the JWS file to create a deployable Web Service. The only additional attribute specify in this case is the compiledWsdl attribute of the <jws> child element, which points to the wsdlc-generated JAR file that contains the JWS interface file and the data binding artifacts. You must also use the type attribute of the jwsc Ant task to specify that jwsc generate a JAX-WS Web Service, rather than the default JAX-RPC Web Service.
The example then deploys the Web Service to the examplesServer as usual, and generates a standalone Java client application that invokes the shipGoods operation of the WarehouseService Web Service. The WarehouseServiceClient.java application uses the JAX-WS Stubs generated by the clientgen WebLogic Web Service Ant task, based on the WSDL of the WarehouseService Web Service.
Files Used in the Example
Directory Location:
MW_HOME/wlserver_10.3/samples/server/examples/src/examples/webservices/jaxws/wsdl2service/
(where MW_HOME is the directory containing your Oracle WebLogic Server installation)
File
Click source files to view code.
Description
build.xml Ant build file that contains targets for building and running the example. WarehouseServiceClient.java Standalone client application that invokes the deployed WarehouseService Web Service. Uses the JAX-WS Stubs generated by clientgen, based on the WSDL of the Web Service. WarehouseService_WarehouseShipmentsPortImpl.java Prepackaged implementation of WarehouseService. wsdl_files/WarehouseService.wsdl Existing WSDL file from which the wsdlc Ant task generates the needed components, including the JWS interface that represents the WarehouseService Web Service that has a single method, shipGoods.
Prepare the Example
Prerequisites
Before working with this example:
- Install Oracle WebLogic Server, including the examples.
- Start the Examples server.
- Set up your environment.
Configure Oracle WebLogic Server
No special configuration is required for this example
Build and Deploy the Example
Change to the SAMPLES_HOME\server\examples\src\examples\webservices\jaxws\wsdl2service directory, where SAMPLES_HOME refers to the main Oracle WebLogic Server examples directory, such as d:\Oracle\Middleware\wlserver_10.3\samples.
- Execute the following command:
prompt> ant generate.from.wsdl
This command executes the wsdlc Ant task on the specified WSDL file to generate the components described in the introduction above.- Using your favorite Java editor or IDE, open the file output\Warehouse\imp\examples\webservices\jaxws\wsdl2Service\WarehouseService_WarehouseShipmentsPortImpl.java and modify the shipGoods() method:
This simulates adding business logic to the JWS implementation file. This sample has provided an ant target copy.serviceimpl which will copy a prepackaged WarehouseService_WarehouseShipmentsPortImpl.java to the directory output\Warehouse\imp\examples\webservices\jaxws\wsdl\wsdl2Service.public boolean shipGoods(examples.webservices.jaxws.wsdl.Item item,java.lang.String customer) { int pn = item.getProductNumber(); int quantity = item.getQuantity(); System.out.println("Customer " + customer + " wants " + quantity + " pieces of Goods '" + pn + "' to be shipped."); System.out.println("Shipping goods now."); return true; } }- Execute the following command:
prompt> ant build.service client
The build.service target executes the jwsc Ant task that compiles and builds the Web Service; the client target builds the client application that invokes the Web Service.- Execute the following command:
prompt> ant deploy
This command deploys the example on the wl_server domain of your Oracle WebLogic Server installation.
Run the Example
To run the example, follow these steps:
- In your development shell, run the WarehouseServiceClient Java application by using the following command from the main example directory (SAMPLES_HOME\server\examples\src\examples\webservices\jaxws\wsdl2service):
prompt> ant run
- After building and running the example, you can view the WSDL of the WarehouseService Web Service in your Web browser:
http://host:port/WarehouseService/WarehouseService?WSDL
Where:
- host refers to the machine on which Oracle WebLogic Server is running
- port refers to port on which Oracle WebLogic Server is listening
This URL is the same as the value of the location attribute of the <soap:address> element in the original WSDL, except that the host and port of the hosting server is obviously changed to that of Oracle WebLogic Server.
Check the Output
If your example runs successfully, you will get the following message in the command shell from which you ran the client application:
Buildfile: build.xml
run:
[java] Goods has been shipped!BUILD SUCCESSFUL
Total time: 6 seconds
Troubleshooting
Related Topics
(Internet connection required.)
- Getting Started with JAX-WS Web Services for Oracle WebLogic Server
- Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server
- Developing Applications for Oracle WebLogic Server