Copyright © 2010 Skyway Software
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.
The prerequisites needed to complete this tutorial are
CustomersApp Web Project - this scaffolded Spring MVC application was created using the Spring Scaffolding tutorial that is available in the Eclipse help system and online (under MyEclipse for Spring Education Materials)
Right-click on the WeatherApp project, and choose MyEclipse > Import WSDL...
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 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.
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.
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 test the web service. This going to be accomplished by adding a call to the web service from a preexisting method, specifically the loadCustomers() method in the CustomerServiceImpl class.
Add the following import statements to CustomerServiceImpl.java (generated/org/customersapp/service)
import org.weather.service.jaxws.IWeatherSoapEndpoint; import org.weather.service.jaxws.WeatherSoapClient; import com.cdyne.ws.weatherws.ForecastReturn;
Update the loadCustomers() method in CustomerServiceImpl.java (generated/org/customersapp/service). Add the lines of code that don't exist.
public Set<Customer> loadCustomers() {
WeatherSoapClient wsClient = new WeatherSoapClient();
IWeatherSoapEndpoint service = wsClient.getService();
ForecastReturn fr = service.GetCityForecastByZIP("33710");
System.out.println("Weather is "+fr.getCity()+" "+fr.getState());
return customerDAO.findAllCustomers();
}
This is just a basic example. The web service has other methods that are available.
Deploy the CustomersApp project, and browse to the following URL. This URL will call the service method that was modified in the previous step.
The Java console should show that service was called.
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:
Reference - Eclipse Help (MyEclipse for Spring 10.0 --> MyEclipse for Spring Reference) or Education Materials (online)
Tutorials - Eclipse Help (MyEclipse for Spring 10.0 --> 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)