在本章中,我们将了解如何使用Spring WS创建Web应用程序服务器。请参考以下步骤:
第1步: 按照Spring WS入门程序章节的介绍,创建一个名称为:countryService
的项目,并在这个项目中创建一个名为com.zyiz
的包。
第2步: 按照以下步骤中的说明创建:countries.xsd,以及域类:CountryRepository
和CountryEndPoint
。
第3步: 更新/WEB-INF
子文件夹下spring-ws-servlet.xml
。
第4步: 最后一步是为所有源文件和配置文件创建内容,并按照下面的说明导出应用程序。
文件:countries.xsd -
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema" xmlns:tns = "http://www.zyiz/schemas" targetNamespace = "http://www.zyiz/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 = "RMB"/> <xs:enumeration value = "GBP"/> <xs:enumeration value = "USD"/> <xs:enumeration value = "INR"/> </xs:restriction> </xs:simpleType> </xs:schema>
打开Eclise创建一个Maven项目,项目名称为: countryService ,完整的项目目录结构如下 -
更新文件:pom.xml 中的代码 -
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zyiz</groupId> <artifactId>countryService</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>countryService Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>2.4.0.RELEASE</version> </dependency> <dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> </dependencies> <build> <finalName>countryService</finalName> <defaultGoal>compile</defaultGoal> </build> </project>
将文件countries.xsd 复制到 D:\eclipse-workspace\countryService\countryService\src\main\resources , 打开命令控制台,进入到 D:\eclipse-workspace\countryService\countryService\src\main\resources 目录并执行以下xjc
命令,以使用countries.xsd
生成域类。
xjc -p com.zyiz countries.xsd
Maven将开始处理,并将在 com.zyiz
包中创建域类。
D:\eclipse-workspace\countryService\countryService\src\main\resources> xjc -p com.zyiz countries.xsd 正在解析模式... 正在编译模式... com\zyiz\Country.java com\zyiz\Currency.java com\zyiz\GetCountryRequest.java com\zyiz\GetCountryResponse.java com\zyiz\ObjectFactory.java com\zyiz\package-info.java D:\eclipse-workspace\countryService\countryService\src\main\resources>
在D:\eclipse-workspace\countryService\countryService\src\main文件夹中创建一个文件夹:java
。 复制上面生成的所有类到D:\eclipse-workspace\countryService\countryService\src\main\java目录中。 并且再创建两个类:CountryRepository
和CountryEndPoint
分别代表国家数据库和国家/地区服务器。
CountryRepository.java - 类代码如下所示 -
package com.zyiz; import java.util.ArrayList; import java.util.List; import org.springframework.beans.propertyeditors.CurrencyEditor; 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 china = new Country(); china.setName("China"); china.setCapital("Beijing"); china.setCurrency(Currency.RMB); china.setPopulation(138186860); countries.add(china); 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) { Assert.notNull(name); Country result = null; for (Country country : countries) { if (name.trim().equals(country.getName())) { result = country; } } return result; } }
文件:CountryEndPoint.java 的代码如下 -
package com.zyiz; import org.jdom.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.zyiz.Country; import com.zyiz.CountryRepository; import com.zyiz.GetCountryRequest; import com.zyiz.GetCountryResponse; @Endpoint public class CountryEndPoint { private static final String NAMESPACE_URI = "http://www.zyiz/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; } }
文件:/WEB-INF/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.zyiz"/> <sws:annotation-driven/> <sws:dynamic-wsdl id="countries" portTypeName = "CountriesPort" locationUri = "/countryService/" targetNamespace = "http://www.zyiz.net/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>zyiz 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>
创建源文件和配置文件后,构建完成后,发布到Tomcat的服务器中。启动Tomcat服务器完成后,使用标准浏览器进行POST请求 - http://localhost:8080/countryService /
,并通过使用任何SOAP客户端发出以下请求。
<x:Envelope xmlns:x = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns = "http://www.zyiz/schemas"> <x:Header/> <x:Body> <tns:getCountryRequest> <tns:name>United States</tns:name> </tns:getCountryRequest> </x:Body> </x:Envelope>
应该会看到类似以下的结果 -
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <ns2:getCountryResponse xmlns:ns2 = "http://www.zyiz/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>