在本章中,我們將了解如何使用Spring WS建立Web應用程式伺服器。請參考以下步驟:
第1步: 按照Spring WS入門程式章節的介紹,建立一個名稱為:countryService
的專案,並在這個專案中建立一個名為com.yiibai
的包。
第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.yiibai/schemas"
targetNamespace = "http://www.yiibai/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.yiibai</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.yiibai countries.xsd
Maven將開始處理,並將在 com.yiibai
包中建立域類。
D:\eclipse-workspace\countryService\countryService\src\main\resources>
xjc -p com.yiibai countries.xsd
正在解析模式...
正在編譯模式...
com\yiibai\Country.java
com\yiibai\Currency.java
com\yiibai\GetCountryRequest.java
com\yiibai\GetCountryResponse.java
com\yiibai\ObjectFactory.java
com\yiibai\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.yiibai;
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.yiibai;
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.yiibai.Country;
import com.yiibai.CountryRepository;
import com.yiibai.GetCountryRequest;
import com.yiibai.GetCountryResponse;
@Endpoint
public class CountryEndPoint {
private static final String NAMESPACE_URI = "http://www.yiibai/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.yiibai"/>
<sws:annotation-driven/>
<sws:dynamic-wsdl id="countries"
portTypeName = "CountriesPort"
locationUri = "/countryService/"
targetNamespace = "https://www.tw511.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>Yiibai 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.yiibai/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.yiibai/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>