- Spring WS - Home
- Spring WS - Overview
- Spring WS - Environment Setup
- Spring WS - First Application
- Spring WS - Static WSDL
- Spring WS - Writing Server
- Spring WS - Unit Test Server
- Spring WS - Writing Client
- Spring WS - Unit Test Client
Spring WS - Resources
Spring WS - Quick Guide
Spring WS - Overview
Spring Web Services (Spring-WS) is one of the projects developed by the Spring Community. Its prime focus is to create document-driven Web Services. The Spring Web Services project facilitates contract-first SOAP Service Development, provides multiple ways to create flexible web services, which can manipulate XML payloads in multiple ways.
The Spring web services uses Spring concepts like dependency injection and configurations seamlessly. The Spring-WS requires Spring 3.0 Version. With contract-first development, we start with WSDL Contract and then will use JAVA to implement the required contract.
As opposed to the contract-last approach where JAVA interfaces generate WSDL/XSD contract. The WSDL based contract remains independent of JAVA implementation in the contract-first approach. In case we require changing the JAVA interfaces, then there is no need to communicate the changes made in the existing WSDL contract to the web services users. Spring-WS aims to provide loose coupling between the WSDL contract and its JAVA based implementation.
Features
Following are the features of Spring Web Services −
XML Mapping to Objects − XML based requests can be mapped to any object using the information stored in the Message Payload, SOAP Action Header or by using an XPath Expression.
Multiple API Support to parse XML − Apart from the standard JAXP APIs (DOM, SAX, StAX) to parse the incoming XML requests, other libraries like JDOM, dom4j, XOM are also supported.
Multiple API Support to marshal XML− Spring Web Services supports JAXB 1 and 2, Castor, XMLBeans, JiBX, and XStream libraries using its Object/XML Mapping module. The Object/XML Mapping module can also be used in non-web services code as well.
Spring based configurations − Spring Web Services uses the Spring Application Contexts for its configurations having a similar architecture as that of the Spring Web MVC.
Integrated WS-Security module − Using the WS-Security module, you can Sign, Encrypt, Decrypt SOAP Messages or Authenticate them.
Support for Acegi Security − Using the WS-Security implementation of Spring Web Services, Acegi configuration can be used for your SOAP services.
Architecture
The Spring-WS project consists of five major modules, which are explained below.
Spring-WS Core − It is the primary module and provides the Central Interfaces like WebServiceMessage and SoapMessage, the server-side framework, powerful message dispatching capability and support classes to implement Web service endpoints. It also provides Web Service consumer client as WebServiceTemplate.
Spring-WS Support − This module provides supports for JMS, emails, etc.
Spring-WS Security − This module is responsible to provide WS-Security implementation integrated with core Web Service Module. Using this module, we can add principal tokens, sign, encrypt and decrypt SOAP messages. This module allows using the existing Spring Security Implementation for authentication and authorization.
Spring XML − This module provides XML support classes for Spring Web Services. This module is internally used by Spring-WS framework.
Spring OXM − This module provides support classes for XML vs Object Mapping.
Spring WS - Environment Setup
This chapter will guide you on how to prepare a development environment to start your work with Spring Web Services. It will also teach you how to set up JDK on your machine before you set up spring WS −
Setup Java Development Kit (JDK)
You can download the latest version of SDK from Oracle's Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
If you are running Windows and have installed the JDK in C:\jdk-24, you would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk-24;%PATH% set JAVA_HOME=C:\jdk-24
Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-24 and you use the C shell, you will have to put the following into your .cshrc file.
setenv PATH /usr/local/jdk-24/bin:$PATH setenv JAVA_HOME /usr/local/jdk-24
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.
Popular Java Editors
To write your Java programs, you need a text editor. There are many sophisticated IDEs available in the market. But for now, you can consider one of the following −
Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
Netbeans − It is a Java IDE that is open-source and free, which can be downloaded from www.netbeans.org/index.html.
Eclipse − It is also a Java IDE developed by the eclipse open-source community and can be downloaded from www.eclipse.org.
Install Eclipse
In this chapter, we will explain how to set Spring environment in Eclipse IDE. Before proceeding with the installation, make sure that you already have Eclipse installed in your system. If not, download and install Eclipse.
For more information on Eclipse, please refer our Eclipse Tutorial
Set Maven
In this tutorial, we are using maven to run and build the spring based examples. Follow the Maven - Environment Setup to install maven.
Spring WS - First Application
Let us start writing an actual SOAP based web service with Spring-WS Framework. Before we start writing our first example using the Spring-WS framework, we have to ensure that the Spring-WS environment is setup properly as explained in Spring Web Services - Environment Setup chapter. We are assuming that the readers have some basic working knowledge with the Eclipse IDE.
Therefore, let us proceed to write a simple Spring WS Application which will expose a web service method to book a leave in an HR Portal.
Contract-first Approach
Spring-WS uses the Contract-first approach, which means we should have our XML Structures ready before writing any JAVA based implementation code. We are defining a LeaveRequest Object, which has sub-objects Leave and Employee.
Following are the required XML constructs −
Leave.xml
<Leave xmlns = "http://tutorialspoint.com/hr/schemas"> <StartDate>2016-07-03</StartDate> <EndDate>2016-07-07</EndDate> </Leave>
Employee.xml
<Employee xmlns = "http://tutorialspoint.com/hr/schemas"> <Number>404</Number> <FirstName>Mahesh</FirstName> <LastName>Parashar</LastName> </Employee>
LeaveRequest.xml
<LeaveRequest xmlns = "http://tutorialspoint.com/hr/schemas">
<Leave>
<StartDate>2016-07-03</StartDate>
<EndDate>2016-07-07</EndDate>
</Leave>
<Employee>
<Number>404</Number>
<FirstName>Mahesh</FirstName>
<LastName>Parashar</LastName>
</Employee>
</LeaveRequest>
hr.xsd
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
xmlns:hr = "http://tutorialspoint.com/hr/schemas"
elementFormDefault = "qualified"
targetNamespace = "http://tutorialspoint.com/hr/schemas">
<xs:element name = "LeaveRequest">
<xs:complexType>
<xs:all>
<xs:element name = "Leave" type = "hr:LeaveType"/>
<xs:element name = "Employee" type = "hr:EmployeeType"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name = "LeaveType">
<xs:sequence>
<xs:element name = "StartDate" type = "xs:date"/>
<xs:element name = "EndDate" type = "xs:date"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name = "EmployeeType">
<xs:sequence>
<xs:element name = "Number" type = "xs:integer"/>
<xs:element name = "FirstName" type = "xs:string"/>
<xs:element name = "LastName" type = "xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Create the Project
| Step | Description |
|---|---|
| 1 | Create a Maven Based Project using maven-archetype-webapp archetype with a groupid com.tutorialspoint.hr, artifactid leaveservice. |
| 2 | Create a Java interface HumanResourceService.java and Java Classes HumanResourceServiceImpl under the com.tutorialspoint.hr.service package. |
| 3 | Create Spring configuration files web.xml and spring-ws-servlet.xml under the WEB-INF folder. |
| 4 | Create WSDL file hr.xsd under the WEB-INF folder. |
| 5 | The final step is to create the content of the source and configuration files and export the application as explained below. |
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.hr</groupId>
<artifactId>leaveservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>leaveService Spring-WS Application</name>
<url>http://www.springframework.org/spring-ws</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>4.0.11</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>leaveservice</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
HumanResourceService.java
package com.tutorialspoint.hr.service;
import java.util.Date;
public interface HumanResourceService {
void bookLeave(Date startDate, Date endDate, String name);
}
HumanResourceServiceImpl.java
package com.tutorialspoint.hr.service;
import java.util.Date;
import org.springframework.stereotype.Service;
@Service
public class HumanResourceServiceImpl implements HumanResourceService {
public void bookLeave(Date startDate, Date endDate, String name) {
System.out.println("Booking holiday for [" + startDate + "-"
+ endDate + "] for [" + name + "] ");
}
}
spring-ws-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:sws = "http://www.springframework.org/schema/web-services"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "com.tutorialspoint.hr"/>
<bean id = "humanResourceService"
class = "com.tutorialspoint.hr.service.HumanResourceServiceImpl" />
<sws:annotation-driven/>
<sws:dynamic-wsdl id = "leave"
portTypeName = "HumanResource"
locationUri = "/leaveService/"
targetNamespace = "http://tutorialspoint.com/hr/definitions">
<sws:xsd location = "/WEB-INF/hr.xsd"/>
</sws:dynamic-wsdl>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<display-name>TutorialsPoint HR Leave Service</display-name>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>
org.springframework.ws.transport.http.MessageDispatcherServlet
</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
hr.xsd
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
xmlns:hr = "http://tutorialspoint.com/hr/schemas"
elementFormDefault = "qualified"
targetNamespace = "http://tutorialspoint.com/hr/schemas">
<xs:element name = "LeaveRequest">
<xs:complexType>
<xs:all>
<xs:element name = "Leave" type = "hr:LeaveType"/>
<xs:element name = "Employee" type = "hr:EmployeeType"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name = "LeaveType">
<xs:sequence>
<xs:element name = "StartDate" type = "xs:date"/>
<xs:element name = "EndDate" type = "xs:date"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name = "EmployeeType">
<xs:sequence>
<xs:element name = "Number" type = "xs:integer"/>
<xs:element name = "FirstName" type = "xs:string"/>
<xs:element name = "LastName" type = "xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Running the Project
Once you are done with creating source and configuration files, create the war file using maven install command and save your leaveservice.war file available in /target folder in Tomcat's webapps folder.
Now start your Tomcat server and make sure you are able to access other webpages from webapps folder using a standard browser. Now, try to access the URL − http://localhost:8080/leaveService/leave.wsdl. If everything is fine with the Spring Web Application, we will see the following screen.
Spring WS - Static WSDL
In the previous chapter Spring -WS - First Application, we have generated WSDL automatically using the Spring WS Configuration. In this case, we will display how to expose the existing WSDL using the Spring WS.
| Step | Description |
|---|---|
| 1 | Create a project with a name leaveService under a package com.tutorialspoint as explained in the Spring WS - First Application chapter. |
| 2 | Create a WSDL leave.wsdl under the /WEB-INF folder. |
| 3 | Update spring-ws-servlet.xml under the /WEB-INF folder. We are using the static-wsdl tag here instead of the dynamic-wsdl. |
| 4 | The final step is to create content of all source and configuration files and export the application as explained below. |
leave.wsdl
<wsdl:definitions xmlns:wsdl = "http://schemas.xmlsoap.org/wsdl/"
xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:schema = "http://tutorialspoint.com/hr/schemas"
xmlns:tns = "http://tutorialspoint.com/hr/definitions"
targetNamespace = "http://tutorialspoint.com/hr/definitions">
<wsdl:types>
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<xsd:import namespace = "http://tutorialspoint.com/hr/schemas"
schemaLocation = "hr.xsd"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name = "LeaveRequest">
<wsdl:part element = "schema:LeaveRequest" name = "LeaveRequest"/>
</wsdl:message>
<wsdl:portType name = "HumanResource">
<wsdl:operation name = "Leave">
<wsdl:input message = "tns:LeaveRequest" name = "LeaveRequest"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name = "HumanResourceBinding" type = "tns:HumanResource">
<soap:binding style = "document"
transport = "http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name = "Leave">
<soap:operation soapAction = "http://mycompany.com/RequestLeave"/>
<wsdl:input name = "LeaveRequest">
<soap:body use = "literal"/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name = "HumanResourceService">
<wsdl:port binding = "tns:HumanResourceBinding" name = "HumanResourcePort">
<soap:address location = "http://localhost:8080/leaveService/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
spring-ws-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:sws = "http://www.springframework.org/schema/web-services" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package = "com.tutorialspoint.hr"/> <sws:annotation-driven/> <sws:static-wsdl id = "leave" location = "/WEB-INF/leave.wsdl"/> </beans>
Running the Project
Once you are done with creating source and configuration files, create the war file using maven install command and save your leaveservice.war file available in /target folder in Tomcat's webapps folder.
Now start your Tomcat server and make sure you are able to access other webpages from webapps folder using a standard browser. Now, try to access the URL − http://localhost:8080/leaveService/leave.wsdl. If everything is fine with the Spring Web Application, we will see the following screen.
Spring WS - Writing Server
In this chapter, we will understand how to create a web application server using Spring WS.
| Step | Description |
|---|---|
| 1 | Create a project with a name leaveService under a package com.tutorialspoint as explained in the Spring WS - First Application chapter. |
| 2 | Create a Java Classes CountryRepository.java, and CountryEndPoint.java under the com.tutorialspoint and com.tutorialspoint.ws packages respectively. |
| 3 | Create countries.xsd under the /WEB-INF folder. |
| 4 | Update spring-ws-servlet.xml under the /WEB-INF folder. |
| 5 | The final step is to create content of all source and configuration files and export the application as explained below. |
countries.xsd
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
xmlns:tns = "http://tutorialspoint/schemas"
targetNamespace = "http://tutorialspoint/schemas"
elementFormDefault = "qualified">
<xs:element name = "getCountryRequest">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name = "getCountryResponse">
<xs:complexType>
<xs:sequence>
<xs:element name = "country" type = "tns:country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name = "country">
<xs:sequence>
<xs:element name = "name" type = "xs:string"/>
<xs:element name = "population" type = "xs:int"/>
<xs:element name = "capital" type = "xs:string"/>
<xs:element name = "currency" type = "tns:currency"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name = "currency">
<xs:restriction base = "xs:string">
<xs:enumeration value = "GBP"/>
<xs:enumeration value = "USD"/>
<xs:enumeration value = "INR"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Create Domain Classes
Download jaxb-ri.zip from , and set the path till jaxb-ri\bin which contains xjc tool.
Copy the countries.xsd in \leaveservice\webapp\WEB-INF folder. Let us open the command console, go the leaveservice\leaveservice\webapp\WEB-INF directory and execute the following xjc command to generate domain classes using the countries.xsd.
xjc -p com.tutorialspoint countries.xsd
Maven will start processing and will create the domain classes in com.tutorialspoint package.
parsing a schema... compiling a schema... com\tutorialspoint\Country.java com\tutorialspoint\Currency.java com\tutorialspoint\GetCountryRequest.java com\tutorialspoint\GetCountryResponse.java com\tutorialspoint\ObjectFactory.java com\tutorialspoint\package-info.java
Move all the classes to the leaveservice\src\main\java folder. Create CountryRepository and CountryEndPoint to represent the country database and country server respectively.
CountryRepository.java
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
@Component
public class CountryRepository {
private static final List<Country> countries = new ArrayList<Country>();
public CountryRepository(){
initData();
}
public void initData() {
Country us = new Country();
us.setName("United States");
us.setCapital("Washington");
us.setCurrency(Currency.USD);
us.setPopulation(46704314);
countries.add(us);
Country india = new Country();
india.setName("India");
india.setCapital("New Delhi");
india.setCurrency(Currency.INR);
india.setPopulation(138186860);
countries.add(india);
Country uk = new Country();
uk.setName("United Kingdom");
uk.setCapital("London");
uk.setCurrency(Currency.GBP);
uk.setPopulation(63705000);
countries.add(uk);
}
public Country findCountry(String name) {
Country result = null;
for (Country country : countries) {
if (name.trim().equals(country.getName())) {
result = country;
}
}
return result;
}
}
CountryEndPoint.java
package com.tutorialspoint.ws;
import org.jdom2.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import com.tutorialspoint.Country;
import com.tutorialspoint.CountryRepository;
import com.tutorialspoint.GetCountryRequest;
import com.tutorialspoint.GetCountryResponse;
@Endpoint
public class CountryEndPoint {
private static final String NAMESPACE_URI = "http://tutorialspoint/schemas";
private CountryRepository countryRepository;
@Autowired
public CountryEndPoint(CountryRepository countryRepository) throws JDOMException {
this.countryRepository = countryRepository;
}
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request)
throws JDOMException {
Country country = countryRepository.findCountry(request.getName());
GetCountryResponse response = new GetCountryResponse();
response.setCountry(country);
return response;
}
}
spring-ws-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:sws = "http://www.springframework.org/schema/web-services"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "com.tutorialspoint"/>
<sws:annotation-driven/>
<sws:dynamic-wsdl id="countries"
portTypeName = "CountriesPort"
locationUri = "/countryService/"
targetNamespace = "http://tutorialspoint.com/definitions">
<sws:xsd location = "/WEB-INF/countries.xsd"/>
</sws:dynamic-wsdl>
</beans>
/WEB-INF/web.xml
<web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version = "2.4">
<display-name>TutorialsPoint Country Service</display-name>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet
</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Running the Project
Once you are done with creating source and configuration files, create the war file using maven install command and save your leaveservice.war file available in /target folder in Tomcat's webapps folder.
Now, start the Tomcat server and ensure if we can access other webpages from the webapps folder using a standard browser. Make a POST request to the URL http://localhost:8080/leaveservice/countryService/ and by using any SOAP client make the following request.
<x:Envelope xmlns:x = "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns = "http://tutorialspoint/schemas">
<x:Header/>
<x:Body>
<tns:getCountryRequest>
<tns:name>United States</tns:name>
</tns:getCountryRequest>
</x:Body>
</x:Envelope>
You will see the following result.
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:getCountryResponse xmlns:ns2 = "http://tutorialspoint/schemas">
<ns2:country>
<ns2:name>United States</ns2:name>
<ns2:population>46704314</ns2:population>
<ns2:capital>Washington</ns2:capital>
<ns2:currency>USD</ns2:currency>
</ns2:country>
</ns2:getCountryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Spring WS - Unit Test Server
In this chapter, we will understand how to unit test a web application service created by using the Spring WS.
| Step | Description |
|---|---|
| 1 | Create a project with a name leaveService under a package com.tutorialspoint as explained in the Spring WS - First Application chapter. |
| 2 | Create a Java Classes CustomerEndPointTest.java under the com.tutorialspoint.ws package under src/test/java folder. |
| 3 | Create countries.xsd and spring-context.xml under the src/main/resources folder. |
| 4 | The final step is to create content of all source and configuration files and export the application as explained below. |
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.hr</groupId>
<artifactId>leaveservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>leaveService Spring-WS Application</name>
<url>http://www.springframework.org/spring-ws</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>4.0.11</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-test</artifactId>
<version>4.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>leaveservice</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
spring-context.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:sws = "http://www.springframework.org/schema/web-services"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "com.tutorialspoint"/>
<sws:annotation-driven/>
<bean id = "schema" class = "org.springframework.core.io.ClassPathResource">
<constructor-arg index = "0" value = "countries.xsd" />
</bean>
</beans>
CustomerEndPointTest.java
package com.tutorialspoint.ws;
import static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.payload;
import javax.xml.transform.Source;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;
@ExtendWith(SpringExtension.class)
@ContextConfiguration( locations = "/spring-context.xml" )
public class CustomerEndPointTest {
@Autowired
private ApplicationContext applicationContext;
private MockWebServiceClient mockClient;
@BeforeEach
public void createClient() {
mockClient = MockWebServiceClient.createClient(applicationContext);
GenericApplicationContext ctx = (GenericApplicationContext) applicationContext;
final XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(ctx);
definitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
definitionReader.setNamespaceAware(true);
}
@Test
public void testCountryEndpoint() throws Exception {
Source requestPayload = new StringSource(
"<getCountryRequest xmlns = 'http://tutorialspoint/schemas'>"+
"<name>United States</name>"+
"</getCountryRequest>");
Source responsePayload = new StringSource(
"<getCountryResponse xmlns='http://tutorialspoint/schemas'>" +
"<country>" +
"<name>United States</name>"+
"<population>46704314</population>"+
"<capital>Washington</capital>"+
"<currency>USD</currency>"+
"</country>"+
"</getCountryResponse>");
mockClient.sendRequest(withPayload(requestPayload)).andExpect(payload(responsePayload));
}
}
Running the Project
Once you are done with creating source and configuration files, Run the test cases using maven test
[INFO] Scanning for projects... [INFO] [INFO] [1m-----------------< [0;36mcom.tutorialspoint.hr:leaveservice[0;1m >-----------------[m [INFO] [1mBuilding leaveService Spring-WS Application 0.0.1-SNAPSHOT[m [INFO] from pom.xml [INFO] [1m--------------------------------[ war ]---------------------------------[m [INFO] [INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mleaveservice[0;1m ---[m [INFO] Copying 2 resources from src\main\resources to target\classes [INFO] [INFO] [1m--- [0;32mcompiler:3.13.0:compile[m [1m(default-compile)[m @ [36mleaveservice[0;1m ---[m [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36mleaveservice[0;1m ---[m [INFO] skip non existing resourceDirectory C:\Users\mahes\eclipse-workspace\leaveservice\src\test\resources [INFO] [INFO] [1m--- [0;32mcompiler:3.13.0:testCompile[m [1m(default-testCompile)[m @ [36mleaveservice[0;1m ---[m [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] [1m--- [0;32msurefire:3.3.0:test[m [1m(default-test)[m @ [36mleaveservice[0;1m ---[m [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.ws.[1mCustomerEndPointTest[m Oct 19, 2025 3:05:18 PM org.springframework.ws.soap.addressing.server.AbstractAddressingEndpointMapping afterPropertiesSet INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0] Oct 19, 2025 3:05:19 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.090 s -- in com.tutorialspoint.ws.[1mCustomerEndPointTest[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 3.246 s [INFO] Finished at: 2025-10-19T15:05:19+05:30 [INFO] [1m------------------------------------------------------------------------[m
Spring WS - Writing Client
In this chapter, we will learn how to create a client for the web application server created in the Spring WS - Writing Server using Spring WS.
| Step | Description |
|---|---|
| 1 | Update the project leaveService under the package com.tutorialspoint as explained in the Spring WS - Writing Server chapter. |
| 2 | Create CountryServiceClient.java under the package com.tutorialspoint.client and MainApp.java under the package com.tutorialspoint as explained in the following steps. |
CountryServiceClient.java
package com.tutorialspoint.client;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import com.tutorialspoint.GetCountryRequest;
import com.tutorialspoint.GetCountryResponse;
public class CountryServiceClient extends WebServiceGatewaySupport {
public GetCountryResponse getCountryDetails(String country){
String uri = "http://localhost:8080/leaveservice/countryService/";
GetCountryRequest request = new GetCountryRequest();
request.setName(country);
GetCountryResponse response =(GetCountryResponse) getWebServiceTemplate()
.marshalSendAndReceive(uri, request);
return response;
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.tutorialspoint.client.CountryServiceClient;
public class MainApp {
public static void main(String[] args) {
CountryServiceClient client = new CountryServiceClient();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.tutorialspoint");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
GetCountryResponse response = client.getCountryDetails("United States");
System.out.println("Country : " + response.getCountry().getName());
System.out.println("Capital : " + response.getCountry().getCapital());
System.out.println("Population : " + response.getCountry().getPopulation());
System.out.println("Currency : " + response.getCountry().getCurrency());
}
}
Start the Web Service
Start the Tomcat server and ensure that we can access other webpages from the webapps folder using a standard browser.
Test Web Service Client
Right click on the MainApp.java in your application under Eclipse and use run as Java Application command. If everything is ok with the application, it will print the following message.
Country : United States Capital : Washington Population : 46704314 Currency : USD
Here, we have created a Client CountryServiceClient.java for the SOAP based web service. MainApp uses CountryServiceClient to make a hit to the web service, makes a post request and gets the data.
Spring WS - Unit Test Client
In this chapter, we will learn how to unit test a client created in the Spring WS - Writing Client for the web application server created in chapter Spring WS - Writing Server using Spring WS.
| Step | Description |
|---|---|
| 1 | Update the project leaveService under theleaveService under a package com.tutorialspoint as explained in the Spring WS - Write Client chapter. |
| 2 | Create CountryServiceClientTest.java under the package com.tutorialspoint under folder src → Test → Java as explained in steps given below. |
CountryServiceClientTest.java
package com.tutorialspoint;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.tutorialspoint.client.CountryServiceClient;
public class CountryServiceClientTest {
CountryServiceClient client;
@BeforeEach
public void setUp() throws Exception {
client = new CountryServiceClient();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.tutorialspoint");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
}
@Test
public void test() {
GetCountryResponse response = client.getCountryDetails("United States");
Country expectedCountry = new Country();
expectedCountry.setCapital("Washington");
Country actualCountry = response.getCountry();
Assertions.assertEquals(expectedCountry.getCapital(), actualCountry.getCapital());
}
}
Unit Test Web Service Client
Run the mvn test
command from eclipse Run As option to run and validate the test cases.[INFO] Scanning for projects... [INFO] [INFO] [1m-----------------< [0;36mcom.tutorialspoint.hr:leaveservice[0;1m >-----------------[m [INFO] [1mBuilding leaveService Spring-WS Application 0.0.1-SNAPSHOT[m [INFO] from pom.xml [INFO] [1m--------------------------------[ war ]---------------------------------[m [INFO] [INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mleaveservice[0;1m ---[m [INFO] Copying 2 resources from src\main\resources to target\classes [INFO] [INFO] [1m--- [0;32mcompiler:3.13.0:compile[m [1m(default-compile)[m @ [36mleaveservice[0;1m ---[m [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36mleaveservice[0;1m ---[m [INFO] skip non existing resourceDirectory C:\Users\mahes\eclipse-workspace\leaveservice\src\test\resources [INFO] [INFO] [1m--- [0;32mcompiler:3.13.0:testCompile[m [1m(default-testCompile)[m @ [36mleaveservice[0;1m ---[m [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] [1m--- [0;32msurefire:3.3.0:test[m [1m(default-test)[m @ [36mleaveservice[0;1m ---[m [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.[1mCountryServiceClientTest[m Oct 19, 2025 4:07:54 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.489 s -- in com.tutorialspoint.[1mCountryServiceClientTest[m [INFO] Running com.tutorialspoint.ws.[1mCustomerEndPointTest[m Oct 19, 2025 4:07:55 PM org.springframework.ws.soap.addressing.server.AbstractAddressingEndpointMapping afterPropertiesSet INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0] Oct 19, 2025 4:07:55 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.651 s -- in com.tutorialspoint.ws.[1mCustomerEndPointTest[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 2, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 3.083 s [INFO] Finished at: 2025-10-19T16:07:55+05:30 [INFO] [1m------------------------------------------------------------------------[m