Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

The Tutorial Web Service

Let's take a slightly deeper look at the example web service that comes with the Spring web services distribution. Figure 1 illustrates the folder structure of the tutorial example. I'm assuming that you've downloaded and unzipped the Spring web services distribution.

Figure 1

Figure 1 The tutorial folder structure.

Figure 1 shows the usual two folders (main and test) underneath the src folder. In fact, this is the standard Maven structure, in which the source code is stored in main and the unit tests are stored in test. To build the tutorial code, you just open a command prompt in the tutorial folder and type the following command:

"mvn package"

This results in the creation of a new folder called target, as shown in Figure 2.

Figure 2

Figure 2 The built tutorial.

As you might have noticed in my earlier article "Spring Web Services with SOAPUI," the target folder contains the WAR file required to deploy and run the web service. To run the web service on Tomcat, simply copy the file tutorial.war into the webapps folder where you installed Tomcat. On my system, Tomcat is installed here:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps

Assuming that you haven't installed Tomcat as an automatically started Windows service, open the bin folder:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin

Double-click the executable file tomcat6.exe to start Tomcat and automatically deploy the WAR file. Successful deployment on Tomcat is indicated by the following excerpt of my console messages:

INFO: Deploying web application archive tutorial.war
LOTS MORE MESSAGES IN HERE
17-Jul-2008 13:08:10 org.apache.catalina.startup.Catalina start
INFO: Server startup in 20426 ms

If you see exception messages or error reports in the Tomcat console window, all is not well with your configuration. One handy hint for checking the applications deployed on Tomcat is to use the Tomcat Manager. Once Tomcat is up and running, the Tomcat Manager is accessible at http://localhost:8080/manager/html (see Figure 3). Just point your browser at that URL.

Figure 3

Figure 3 An excerpt from the Tomcat Manager application.

At the very bottom of Figure 3, notice that the tutorial web application has been deployed and started successfully. I hope you've got it working on your system as well! See my article "Spring Web Services with SOAPUI" for details on how to test the web service using SOAPUI. Briefly, you follow the steps below:

  1. Start SOAPUI from the Windows Start button.
  2. Load the project that corresponds with the tutorial web service.
  3. Open a request window (see Figure 4).
    Figure 4

    Figure 4 Making contact with the web service with SOAPUI.

  4. To send a SOAP message to the tutorial web service, click the green arrow in the request window's toolbar. If all is well, the Tomcat console should display a message set similar to that in Listing 1. The items in bold represent the key elements of the tutorial web service.

Listing 1 Tomcat console output for the tutorial web service.

2008-07-17 13:27:39,661 DEBUG [org.springframework.ws.transport.http.MessageDispatcherServlet] - Initializing servlet 'spring-ws'
2008-07-17 13:27:39,751 INFO [org.springframework.ws.transport.http.MessageDispatcherServlet] - FrameworkServlet 'spring-ws': initialization started
[org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping@1bd06bf] maps request to endpoint [com.mycompany.hr.ws.HolidayEndpoint@d5f9b9]
2008-07-17 13:27:43,767 DEBUG [org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor] - Request:
  <sch:HolidayRequest xmlns:sch="http://mycompany.com/hr/schemas">
     <!--You may enter the following 2 items in any order-->
     <sch:Holiday>
      <sch:StartDate>2006-07-03</sch:StartDate>
      <sch:EndDate>2006-07-23</sch:EndDate>
     </sch:Holiday>
     <sch:Employee>
      <sch:Number>100</sch:Number>
      <sch:FirstName>Meself</sch:FirstName>
      <sch:LastName>Me</sch:LastName>
     </sch:Employee>
  </sch:HolidayRequest>
2008-07-17 13:27:43,777 DEBUG [org.springframework.ws.soap.server.SoapMessageDispatcher] - Testing endpoint adapter
2008-07-17 13:27:43,867 INFO [com.mycompany.hr.service.StubHumanResourceService] -
Booking my next holiday for
[Mon Jul 03 00:00:00 BST 2006-Sun Jul 23 00:00:00 BST 2006]
name [Meself Me]
2008-07-17 13:27:43,867 DEBUG [org.springframework.ws.server.MessageTracing] - MessageDispatcher with name 'spring-ws' sends no response for request [SaajSoapMessage {http://mycompany.com/hr/schemas}HolidayRequest]
2008-07-17 13:27:43,867 DEBUG [org.springframework.ws.transport.http.MessageDispatcherServlet] - Successfully completed request

Notice how the elements of the SOAP message are displayed (they're bold in Listing 1). All this from Tomcat—a piece of free software! You also can deploy the tutorial application on any other J2EE-compliant application server, such as Geronimo, BEA WebLogic, etc. If you deploy on another container, you'll have to change the settings for the web service request message.

So far, so good. But what if you want to extend the tutorial web service? Suppose you want to instantiate an object of a new class and invoke the methods of that class. For example, you might want to write some database access code to provide persistence for the incoming web service request messages. The easy way to do all this is with application contexts.

  • + Share This
  • 🔖 Save To Your Account