Copyright © 2010 Skyway Software
Abstract
This tutorial details how to import a web service and use from within a Spring application using MyEclipse for Spring 8.6.
Table of Contents
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).
This tutorial is specifically focused on consuming a third party web service from within a Spring application. This will be accomplished by using the Import Web Services wizard in MyEclipse for Spring.
This tutorial will make use of the free CDYNE Weather Web Service, which provides weather information in the United States by zip code. Please read the notes section on the CDYNE wiki site for information regarding this Web Service.
The WSDL for the CDYNE Weather Web Service is http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl. If this does not work, please consult the CDYNE wiki site mentioned in the previous paragraph.
Create a new MyEclipse Web Project (or Eclipse Dynamic Web Project) called WeatherApp.
Right-click on the WeatherApp project, and choose MyEclipse > Import Web Service...
Add the following WSDL URL into the WSDL Location text box: http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl As seen in the figure below, type org.weather.service in the Target Package text box,
The final configuration step is to specify where the application (source code, configuration files) should be generated to. For this panel the defaults are fine. Click the Next button.
On the Summary screen click the Finish button.
That's it. All the necessary source code for using the web service has been generated. Next we'll briefly review the generated code.
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.
The com.dyne.ws.weatherws package has the source code generated for the WSDL entities. The package name is based on the target namespace specified in the WSDL.
The org.weather.service package has the Spring service interface, Spring service implementation (Spring @Service) and Junit test. This source code is only used if you are implementing the service based on the contract (Contract-First Development).
The org.weather.service.jaxws package has the service endpoint interface and service client. The service endpoint interface should have a Java method for web service operation.
A copy of the WSDL can be found in the resources/wsdls/com/cdyne/ws/wsdl/asmx/weather/weatherws/ folder. Right-click on weatherws.wsdl, and select Open With --> MyEclipse WSDL Editor. The WSDL editor will show you a graphical view if the service, including operations, complex types and relationships.
This section will show you how to use the generated source code to call the web service
From a Java method in your web application, add the following code to call the GetCityForecastByZIP operation.
public someMethod() {
WeatherSoapClient wsClient = new WeatherSoapClient();
IWeatherSoapEndpoint service = wsClient.getService();
ForecastReturn fr = service.GetCityForecastByZIP("33710");
}
This is just one example. The web service has other methods that are available.
When the Java method is invoked, a call will be made to the web service. The fr variable will store the results of the web service.
That concludes the Import and Use a SOAP Web Service tutorial. You may also be interested in checking out the other tutorials related to Web Services, including Scaffold a Spring service from WSDL (Contract-first development) 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.
Thank you for you interest in MyEclipse for Spring. If you are interested in learning more, the following developer resources are available:
Developer Resources
Reference - Eclipse Help (MyEclipse for Spring 8.6 --> MyEclipse for Spring Reference) or Education Materials (online)
Tutorials - Eclipse Help (MyEclipse for Spring 8.6 --> Tutorials) or Education Materials (online)
Screencasts - MyEclipse for Spring YouTube Channel (online)
Support Forums - MyEclipse for Spring Forums (online)
Blog - MyEclipse for Spring Posts (online)