Scaffolding a Spring Service from WSDL
This tutorial is focused on scaffolding a web service from an existing WSDL. The WSDL implemented is an abbreviated version of the HelloWorldWSDL Contract from the Apache CXF documentation. In this tutorial, you will learn how to:
- Import a WSDL contract
- Deploy and run the web service
1. Import the HelloWorld Web Service Contract
When designing a system consisting of multiple applications communicating with each other using web services, it is common to use contract-first development as a way of defining the messages handled between systems. This includes identifying the service operations and input/output parameters. This is cumulatively referred to as the contract, and it’s defined using a WSDL document. The service consumers and producers use the WSDL document as the contract.
MyEclipse can be used to implement a web service contract by importing the WSDL. This is, of course, different than discovering the web services for the purpose of consuming the services (see the tutorial on importing a SOAP web service).
- Create a new MyEclipse Web project called HelloWorldApp.
- Right-click project, and select MyEclipse>Import WSDL.
- Add the following WSDL URL into the WSDL Location field: http://www.skywayperspectives.org/documentation/me4s90/wsdl/helloworld.wsdl, type org.helloworld in the Target Package field, and click Next.
- Accept the default project configuration options, and click Next.
- Accept the default JAX-WS settings, and click Next.
- Accept the default Spring Capabilities settings, and click Next.
- Accept the default settings for libraries that should be added to the current project, and click Next.
- Click Finish on the Summary page. The necessary source code for implementing the web service is generated.
2. Review Generated Code
The Import Web Services wizard helps consume external SOAP web services from within applications by generating the Java classes needed for interacting with the web services. The Import Web Services wizard also helps you implement a service contract (referred to as Contract-First development) by stubbing out a concrete implementation of the service from a WSDL (SOAP web service contract).
By default the source code is generated in the generated folder. Since the folder doesn’t exist, the wizard creates the folder and sets it up as an Eclipse source folder. A snapshot of the generated Java files are in the figure below. All artifacts created reflect the web service’s definition in the WSDL.
The org.apache.hello_world_soap_http.types package contains the generated source code for the WSDL entities. The package name is based on the target namespace specified in the WSDL.
The org.helloworld package contains the generated Spring service interface, Spring service implementation (Spring @Service), and Junit test.
The org.helloworld.jaxws package contains the generated service endpoint interface and service client. The service endpoint interface should have a Java method for web service operation.
This tutorial is concerned with implementing a web service according to the WSDL, and GreeterImpl.java is the Spring Service that implements the service operations. Open the file in the Java editor and see there’s one method per service operation. The only thing left to do is to implement the method (see the “//TODO Implement this method” comments) with the desired functionality.
3. Deploy and Run the Web Service
After you implement the service methods, you can deploy the web project and test the service.
Note: You can do the following steps without the service methods implemented; however, there will be no service found to test when you open the Web Services explorer.
- Right-click the project, and select Run As>MyEclipse Server Application. Select the MyEclipse Tomcat server.
- After the application is deployed, switch to the JAX-WS Annotations view, and open the GreeterImpl.java file (org.helloworld package) in the Java editor.
- Click the Open Web Service Explorer link in the JAX-WS Annotations view. This opens the WSDL in the Web Service Explorer, which can be used for testing the web service. If you want to test the service in another testing tool, the WSDL URL is: http://localhost:8080/HelloWorldApp/jaxws/IGreeterEndpoint?wsdl