MyEclipse Logo

MyEclipse for Spring 10.0: Scaffold a Spring service from WSDL

Scaffold a Spring Service from a WSDL (Contract-First Development)

Niel Eyde

Skyway Software, Inc.

Table of Contents

1. Introduction
2. Goals
3. Prerequisites
4. Create Web Project
5. Import the HelloWorld Web Service Contract
6. Import the HelloWorld Web Service Contract
7. Review Generated Code
8. Deploy and Run JAX-WS Web Service
9. Conclusion
10. Additional Developer Resources

1. Introduction

MyEclipse for Spring has a variety of functions to facilitate SOAP web services development. The JAX-WS annotator helps expose a Spring service (@Service) as a SOAP web service (often referred to as Contract-Last development or Code-First development), and the JAX-WS annotator also provides an Eclipse view for maintaining all the JAX-WS web service annotations. 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).

When designing a system that consists 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 that will be 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 for Spring 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 Import and Use a SOAP Web Service tutorial).

2. Goals

This tutorial is focused on scaffolding a web service from a preexisting WSDL (web service contract). This will be accomplished by using the Import Web Services wizard in MyEclipse for Spring to generate all source code required for implementing the web service defined in the WSDL. Among other files, MyEclipse for Spring will generate a Spring service component (@Service) with a service method corresponding to each web service operation.

The WSDL that will be implemented is an abbreviated version of the HelloWorld WSDL Contract from the Apache CXF documentation.

The WSDL URL is: http://www.skywayperspectives.org/documentation/me4s90/wsdl/helloworld.wsdl

The following diagram shows a graphical depiction of the WSDL using the MyEclipse WSDL editor. As you can see there are three operations: sayHi, greetMe, and pingMe.

Figure 1. HelloWorld WSDL


3. Prerequisites

The prerequisites needed to complete this tutorial are:

4. Create Web Project

  1. Create a new MyEclipse Web Project (or Eclipse Dynamic Web Project) called HelloWorldApp.

5. Import the HelloWorld Web Service Contract

  1. Right-click on the HelloWorldApp project, and choose MyEclipse > Import WSDL...

    Figure 2. Import Web Service Wizard


  2. Add the following WSDL URL into the WSDL Location text box: http://www.skywayperspectives.org/documentation/me4s90/wsdl/helloworld.wsdl As seen in the figure below, type org.helloworld in the Target Package text box,

    Figure 3. Import WSDL URL


  3. The next step is to specify where the application (source code, configuration files, JSP, etc...) should be generated to in the current project and few additional scaffolding options. For this panel the defaults are fine. Click the Next button.

    Figure 4. Select Target Folders


  4. The final configuration step is to specify the libraries that should be added to the current project, how they should be added (classpath containers or copied into project), and the version of Spring MVC to use. For this panel the defaults are fine. Click the Next button.

    Figure 5. Runtime Dependencies


  5. On the Summary screen click the Finish button.

    Figure 6. Summary


That's it. All the necessary source code for implementing the web service has been generated. Next we'll briefly review the generated code.

6. Import the HelloWorld Web Service Contract

  1. Right-click on the HelloWorldApp project, and choose MyEclipse > Import WSDL...

    Figure 7. Import Web Service Wizard


  2. Add the following WSDL URL into the WSDL Location text box: http://www.skywayperspectives.org/documentation/me4s90/wsdl/helloworld.wsdl As seen in the figure below, type org.helloworld in the Target Package text box,

    Figure 8. Import WSDL URL


  3. The next step is to specify where the application (source code, configuration files, JSP, etc...) should be generated to in the current project and few additional scaffolding options. For this panel the defaults are fine. Click the Next button.

    Figure 9. Select Target Folders


  4. The final configuration step is to specify the libraries that should be added to the current project, how they should be added (classpath containers or copied into project), and the version of Spring MVC to use. For this panel the defaults are fine. Click the Next button.

    Figure 10. Runtime Dependencies


  5. On the Summary screen click the Finish button.

    Figure 11. Summary


That's it. All the necessary source code for implementing the web service has been generated. Next we'll briefly review the generated code.

7. Review Generated Code

  1. By default the Import Web Service wizard generates source code to 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.

    Figure 12. Generated Java files from the WSDL


    The org.apache.hello_world_soap_http.types package has 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 has the generated Spring service interface, Spring service implementation (Spring @Service) and Junit test.

    The org.helloworld.jaxws package has the generated service endpoint interface and service client. The service endpoint interface should have a Java method for web service operation.

  2. In this tutorial we are 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 you will see that 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.

    Figure 13. Generated Java files from the WSDL


8. Deploy and Run JAX-WS Web Service

Finally you can redeploy the web project and test the service.

  1. To deploy the application, right-click on the HelloWorldApp project and select Run As --> MyEclipse Server Application.

  2. Once the application is deployed, open the GreeterImpl.java file in the JAVA editor and switch to the JAX-WS Annotations view. Click on the Open Web Service Explorer link on the JAX-WS Annotations view. This will open the WSDL in the Web Service Explorer , which can be used for testing the web service.

    In case you want to test the service in another testing tool, the WSDL URL is: http://localhost:8080/HelloWorldApp/jaxws/IGreeterEndpoint?wsdl

9. Conclusion

That concludes the Scaffold a Spring service from WSDL (Contract-first development) tutorial. You may be interested in checking out the other tutorials related to Web Services, including Import and Use a SOAP Web Services and JAX-WS Annotators. (see Additional Developer Resources)

What was your experience with this tutorial? Share your experience with us by completing a very brief survey.

10. Additional Developer Resources

Thank you for you interest in MyEclipse for Spring. If you are interested in learning more, the following developer resources are available: