Spring WS 快速指南



Spring WS - 概述

Spring Web Services (Spring-WS) 是 Spring 社群開發的專案之一。它的主要目標是建立文件驅動的 Web 服務。Spring Web Services 專案促進了契約優先的SOAP 服務開發,提供了多種建立靈活 Web 服務的方法,這些方法可以以多種方式操作 XML 有效負載。

Spring Web 服務無縫地使用了 Spring 概念,例如依賴注入和配置。Spring-WS 需要 Spring 3.0 版本。在契約優先開發中,我們從WSDL 契約開始,然後將使用 JAVA 實現所需的契約。

與 JAVA 介面生成 WSDL/XSD 契約的後契約方法相反。在契約優先方法中,基於 WSDL 的契約獨立於 JAVA 實現。如果我們需要更改 JAVA 介面,則無需將對現有 WSDL 契約所做的更改傳達給 Web 服務使用者。Spring-WS 旨在提供 WSDL 契約與其基於 JAVA 的實現之間的松耦合。

特性

以下是 Spring Web Services 的特性:

  • 將 XML 對映到物件 - 使用儲存在訊息有效負載、SOAP 操作標頭中的資訊或使用 XPath 表示式,可以將基於 XML 的請求對映到任何物件。

  • 支援多種解析 XML 的 API - 除了用於解析傳入 XML 請求的標準 JAXP API(DOM、SAX、StAX)之外,還支援其他庫,例如 JDOM、dom4j、XOM。

  • 支援多種編組 XML 的 API - Spring Web Services 使用其物件/XML 對映模組支援 JAXB 1 和 2、Castor、XMLBeans、JiBX 和 XStream 庫。物件/XML 對映模組也可以在非 Web 服務程式碼中使用。

  • 基於 Spring 的配置 - Spring Web Services 使用 Spring 應用程式上下文進行其配置,其架構與 Spring Web MVC 類似。

  • 整合的 WS-Security 模組 - 使用 WS-Security 模組,您可以簽署、加密、解密 SOAP 訊息或對其進行身份驗證。

  • 支援 Acegi 安全性 - 使用 Spring Web Services 的 WS-Security 實現,可以將 Acegi 配置用於您的 SOAP 服務。

架構

Spring-WS 專案包含五個主要模組,如下所述。

  • Spring-WS Core - 它是主要模組,並提供核心介面,如WebServiceMessageSoapMessage、伺服器端框架、強大的訊息分發功能和用於實現 Web 服務端點的支援類。它還提供 Web 服務使用者客戶端作為WebServiceTemplate

  • Spring-WS 支援 - 此模組提供對 JMS、電子郵件等的支援。

  • Spring-WS 安全性 - 此模組負責提供與核心 Web 服務模組整合的 WS-Security 實現。使用此模組,我們可以新增主體令牌、簽署、加密和解密 SOAP 訊息。此模組允許使用現有的 Spring Security 實現進行身份驗證和授權。

  • Spring XML - 此模組為 Spring Web Services 提供 XML 支援類。此模組由 Spring-WS 框架內部使用。

  • Spring OXM - 此模組為 XML 與物件對映提供支援類。

Spring Web Services Architecture

Spring WS - 環境設定

在本章中,我們將瞭解在基於 Windows 和 Linux 的系統上設定 Spring-WS 的過程。透過遵循一些簡單的步驟,無需任何複雜的設定過程,Spring-WS 可以輕鬆安裝並與您當前的Java 環境MAVEN整合。安裝過程中需要使用者管理。

系統需求

下表列出了系統需求,後續步驟將指導我們完成環境設定過程。

JDK Java SE 2 JDK 1.5 或更高版本
記憶體 1 GB RAM(推薦)
磁碟空間 無最低要求
作業系統版本 Windows XP 或更高版本,Linux

現在讓我們繼續執行安裝 Spring-WS 的步驟。

步驟 1:驗證 Java 安裝

首先,您需要在系統上安裝 Java 軟體開發工具包 (SDK)。要驗證這一點,請根據您正在使用的平臺執行以下兩個命令中的任何一個。

如果 Java 安裝已正確完成,則它將顯示 Java 安裝的當前版本和規範。示例輸出在下表中給出。

平臺 命令 示例輸出
Windows

開啟命令控制檯並鍵入:

\>java -version

Java version "1.7.0_60"

Java (TM) SE Run Time Environment (build 1.7.0_60-b19)

Java Hotspot (TM) 64-bit Server VM (build 24.60-b09,mixed mode)

Linux

開啟命令終端並鍵入:

$java -version

java version "1.7.0_25"

Open JDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64)

Open JDK 64-Bit Server VM (build 23.7-b01, mixed mode)

步驟 2:設定 Java 環境

設定環境變數JAVA_HOME以指向 Java 安裝在您機器上的基本目錄位置。

序號 平臺和描述
1

Windows

將 JAVA_HOME 設定為 C:\ProgramFiles\java\jdk1.7.0_60

2

Linux

匯出 JAVA_HOME=/usr/local/java-current

將 Java 編譯器位置的完整路徑附加到系統路徑。

序號 平臺和描述
1

Windows

將字串“C:\Program Files\Java\jdk1.7.0_60\bin”附加到系統變數 PATH 的末尾。

2

Linux

匯出 PATH=$PATH:$JAVA_HOME/bin/

從命令提示符執行命令java -version,如上所述。

步驟 3:下載 Maven 存檔

https://maven.apache.org/download.cgi下載 Maven 3.3.3

作業系統 存檔名稱
Windows apache-maven-3.3.3-bin.zip
Linux apache-maven-3.3.3-bin.tar.gz
Mac apache-maven-3.3.3-bin.tar.gz

步驟 4:解壓縮 Maven 存檔

將存檔解壓縮到您希望安裝 Maven 3.3.3 的目錄中。子目錄 apache-maven-3.3.3 將從存檔中建立。

作業系統 位置(根據您的安裝可能有所不同)
Windows C:\Program Files\Apache Software Foundation\apache-maven-3.3.3
Linux /usr/local/apache-maven
Mac /usr/local/apache-maven

步驟 5:設定 Maven 環境變數

將 M2_HOME、M2 和 MAVEN_OPTS 新增到環境變數中。

作業系統 輸出
Windows

使用系統屬性設定環境變數。

M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.3.3

M2=%M2_HOME%\bin

MAVEN_OPTS=-Xms256m -Xmx512m

Linux

開啟命令終端並設定環境變數。

export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.3

export M2=$M2_HOME/bin

export MAVEN_OPTS=-Xms256m -Xmx512m

Mac

開啟命令終端並設定環境變數。

export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.3

export M2=$M2_HOME/bin

export MAVEN_OPTS=-Xms256m -Xmx512m

步驟 6:將 Maven bin 目錄位置新增到系統路徑

現在將 M2 變數附加到系統路徑。

作業系統 輸出
Windows 將字串 ;%M2% 附加到系統變數 Path 的末尾。
Linux export PATH=$M2:$PATH
Mac export PATH=$M2:$PATH

步驟 7:驗證 Maven 安裝

現在開啟控制檯,執行以下mvn命令。

作業系統 任務 命令
Windows 開啟命令控制檯 c:\> mvn --version
Linux 開啟命令終端 $ mvn --version
Mac 開啟終端 machine:< joseph$ mvn --version

最後,驗證上述命令的輸出,它應該類似於以下內容:

作業系統 輸出
Windows

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30)

Maven home: C:\Program Files\Apache Software Foundation\apache-maven-3.3.3

Java version: 1.7.0_75, vendor: Oracle Corporation

Java home: C:\Program Files\Java\jdk1.7.0_75\jre

Default locale: en_US, platform encoding: Cp1252

Linux

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30)

Maven home: /usr/local/apache-maven/apache-maven-3.3.3

Java version: 1.7.0_75, vendor: Oracle Corporation

Java home: /usr/local/java-current/jdk1.7.0_75/jre

Mac

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30)

Maven home: /usr/local/apache-maven/apache-maven-3.3.3

Java version: 1.7.0_75, vendor: Oracle Corporation

Java home: /Library/Java/Home/jdk1.7.0_75/jre

步驟 8:設定 Eclipse IDE

本教程中的所有示例均使用 Eclipse IDE 編寫。建議讀者在其機器上安裝最新版本的 Eclipse。要安裝 Eclipse IDE,請從以下連結https://www.eclipse.org/downloads/下載最新的 Eclipse 二進位制檔案。下載安裝程式後,將二進位制分發版解壓縮到方便的位置。

例如,在 Windows 上的C:\eclipse或 Linux/Unix 上的/usr/local/eclipse中,最後相應地設定 PATH 變數。可以透過在 Windows 機器上執行以下命令來啟動 Eclipse,或者您可以簡單地雙擊 eclipse.exe。

%C:\eclipse\eclipse.exe

可以透過在 UNIX(Solaris、Linux 等)機器上執行以下命令來啟動 Eclipse:

$/usr/local/eclipse/eclipse

成功啟動後,如果一切正常,則應顯示以下螢幕:

Eclipse Home page

步驟 9:設定 Apache Tomcat

我們可以從https://tomcat.apache.org/下載最新版本的 Tomcat。下載安裝程式後,將二進位制分發版解壓縮到方便的位置。例如,在 Windows 機器上的C:\apache-tomcat-7.0.59或 Linux/Unix 機器上的/usr/local/apache-tomcat-7.0.59中,然後設定指向安裝位置的CATALINA_HOME環境變數。

可以透過在 Windows 機器上執行以下命令來啟動 Tomcat,或者您可以簡單地雙擊 startup.bat

%CATALINA_HOME%\bin\startup.bat
 
or
 
C:\apache-tomcat-7.0.59\bin\startup.bat

可以透過在 UNIX(Solaris、Linux 等)機器上執行以下命令來啟動 Tomcat:

$CATALINA_HOME/bin/startup.sh
 
or
 
/usr/local/apache-tomcat-7.0.59/bin/startup.sh

成功啟動後,可以透過訪問https://:8080/來使用 Tomcat 附帶的預設 Web 應用程式。如果一切正常,則應顯示以下螢幕:

Tomcat

有關配置和執行 Tomcat 的更多資訊,請參閱此處包含的文件以及 Tomcat 網站:https://tomcat.apache.org

可以透過在 Windows 機器上執行以下命令來停止 Tomcat:

%CATALINA_HOME%\bin\shutdown

or

C:\apache-tomcat-7.0.59\bin\shutdown

可以透過在 UNIX(Solaris、Linux 等)機器上執行以下命令來停止 Tomcat:

$CATALINA_HOME/bin/shutdown.sh

or

/usr/local/apache-tomcat-7.0.59/bin/shutdown.sh

完成最後一步後,我們就可以開始第一個 Web Services 示例了,我們將在下一章中討論它。

Spring WS - 第一個應用程式

讓我們開始使用 Spring-WS 框架編寫一個基於 SOAP 的實際 Web 服務。在使用 Spring-WS 框架編寫第一個示例之前,我們必須確保 Spring-WS 環境已正確設定,如 Spring Web Services - 環境設定 章節中所述。我們假設讀者對 Eclipse IDE 有一定的基本使用知識。

因此,讓我們繼續編寫一個簡單的 Spring WS 應用程式,該應用程式將公開一個 Web 服務方法,用於在 HR 門戶中預訂假期。

契約優先方法

Spring-WS 使用契約優先方法,這意味著在編寫任何基於 JAVA 的實現程式碼之前,我們應該準備好我們的 XML 結構。我們正在定義一個 LeaveRequest 物件,它具有子物件 - Leave 和 Employee。

以下是所需的 XML 結構 -

Leave.xml

<Leave xmlns = "https://tutorialspoint.tw/hr/schemas">
   <StartDate>2016-07-03</StartDate>
   <EndDate>2016-07-07</EndDate>
</Leave>

Employee.xml

<Employee xmlns = "https://tutorialspoint.tw/hr/schemas">
   <Number>404</Number>
   <FirstName>Mahesh</FirstName>
   <LastName>Parashar</LastName>
</Employee>

LeaveRequest.xml

<LeaveRequest xmlns = "https://tutorialspoint.tw/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 = "https://tutorialspoint.tw/hr/schemas"
   elementFormDefault = "qualified"
   targetNamespace = "https://tutorialspoint.tw/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>

建立專案

現在讓我們開啟命令控制檯,轉到 C:\MVN 目錄並執行以下 mvn 命令。

C:\MVN>mvn archetype:generate -DarchetypeGroupId = org.springframework.ws
-DarchetypeArtifactId = spring-ws-archetype -DgroupId = com.tutorialspoint.hr
-DartifactId = leaveService

Maven 將開始處理並建立完整的 Java 應用程式專案結構。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] Using property: groupId = com.tutorialspoint.hr
[INFO] Using property: artifactId = leaveService
Define value for property 'version':  1.0-SNAPSHOT: :
[INFO] Using property: package = com.tutorialspoint.hr
Confirm properties configuration:
groupId: com.tutorialspoint.hr
artifactId: leaveService
version: 1.0-SNAPSHOT
package: com.tutorialspoint.hr
 Y: :
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 spring-ws-archetype:2.0.0-M1
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.tutorialspoint.hr
[INFO] Parameter: packageName, Value: com.tutorialspoint.hr
[INFO] Parameter: package, Value: com.tutorialspoint.hr
[INFO] Parameter: artifactId, Value: leaveService
[INFO] Parameter: basedir, Value: C:\mvn
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\mvn\leaveService
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.989 s
[INFO] Finished at: 2017-01-21T11:18:31+05:30
[INFO] Final Memory: 17M/178M
[INFO] ------------------------------------------------------------------------

現在轉到 C:/MVN 目錄。我們將看到一個名為 leaveService(如 artifactId 中指定)的 Java 應用程式專案。更新 pom.xml 並將 HumanResourceService.java 和 HumanResourceServiceImpl.java 新增到以下資料夾中 – C:\MVN\leaveService\src\main\java\com\tutorialspoint\hr\service 資料夾。完成後,將 LeaveEndpoint.java 新增到以下資料夾中 – C:\MVN\leaveService\src\main\java\com\tutorialspoint\hr\ws 資料夾。

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/maven-v4_0_0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.hr</groupId>
   <artifactId>leaveService</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>leaveService Spring-WS Application</name>
   <url>http://www.springframework.org/spring-ws</url>
   
   <build>
      <finalName>leaveService</finalName>
   </build>
   
   <dependencies>
      <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>
</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 + "] ");
   }
}

LeaveEndpoint.java

package com.tutorialspoint.hr.ws;

import java.text.SimpleDateFormat;
import java.util.Date;

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 com.tutorialspoint.hr.service.HumanResourceService;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;

@Endpoint
public class LeaveEndpoint {
   private static final String NAMESPACE_URI = "https://tutorialspoint.tw/hr/schemas";
   private XPath startDateExpression;
   private XPath endDateExpression;
   private XPath nameExpression;
   private HumanResourceService humanResourceService;

   @Autowired
   public LeaveEndpoint(HumanResourceService humanResourceService) throws JDOMException {
      this.humanResourceService = humanResourceService;

      Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);

      startDateExpression = XPath.newInstance("//hr:StartDate");
      startDateExpression.addNamespace(namespace);

      endDateExpression = XPath.newInstance("//hr:EndDate");
      endDateExpression.addNamespace(namespace);

      nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
      nameExpression.addNamespace(namespace);
   }

   @PayloadRoot(namespace = NAMESPACE_URI, localPart = "LeaveRequest")                  
   public void handleLeaveRequest(@RequestPayload Element leaveRequest) throws Exception {
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      Date startDate = dateFormat.parse(startDateExpression.valueOf(leaveRequest));
      Date endDate = dateFormat.parse(endDateExpression.valueOf(leaveRequest));
      String name = nameExpression.valueOf(leaveRequest);

      humanResourceService.bookLeave(startDate, endDate, name);
   }
}	

/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.tutorialspoint.hr"/>
   <bean id = "humanResourceService"
      class = "com.tutorialspoint.hr.service.HumanResourceServiceImpl" />
   <sws:annotation-driven/>

   <sws:dynamic-wsdl id = "leave"
      portTypeName = "HumanResource"
      locationUri = "/leaveService/"
      targetNamespace = "https://tutorialspoint.tw/hr/definitions">
      <sws:xsd location = "/WEB-INF/hr.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 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>

/WEB-INF/hr.xsd

<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
   xmlns:hr = "https://tutorialspoint.tw/hr/schemas"
   elementFormDefault = "qualified"
   targetNamespace = "https://tutorialspoint.tw/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>

構建專案

現在讓我們開啟命令控制檯,轉到 C:\MVN\leaveService 目錄並執行以下 mvn 命令。

C:\MVN\leaveService>mvn clean package

Maven 將開始構建專案。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building leaveService Spring-WS Application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ leaveService ---
[INFO] Deleting C:\mvn\leaveService\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ leaveServi
ce ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ leaveService --
-
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 3 source files to C:\mvn\leaveService\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ le
aveService ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\mvn\leaveService\src\test\resource
s
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ leaveSe
rvice ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ leaveService ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ leaveService ---
[INFO] Packaging webapp
[INFO] Assembling webapp [leaveService] in [C:\mvn\leaveService\target\leaveServ
ice]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\mvn\leaveService\src\main\webapp]
[INFO] Webapp assembled in [7159 msecs]
[INFO] Building war: C:\mvn\leaveService\target\leaveService.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.667 s
[INFO] Finished at: 2017-01-21T11:56:43+05:30
[INFO] Final Memory: 18M/173M
[INFO] ------------------------------------------------------------------------

在 Eclipse 中匯入專案

按照以下步驟在 Eclipse 中匯入專案。

  • 開啟 Eclipse。

  • 選擇檔案→匯入→選項。

  • 選擇 Maven Projects 選項。點選下一步按鈕。

  • 選擇專案位置,即使用 Maven 建立 leaveService 專案的位置。

  • 點選完成按鈕。

執行專案

完成原始檔和配置檔案的建立後,匯出應用程式。右鍵單擊應用程式,使用匯出→WAR 檔案選項並將 leaveService.war 檔案儲存在 Tomcat 的 webapps 資料夾中。

啟動 Tomcat 伺服器並確保我們能夠使用標準瀏覽器從 webapps 資料夾訪問其他網頁。嘗試訪問 URL – https://:8080/leaveService/leave.wsdl,如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。

First Application result

Spring WS - 靜態 WSDL

在上一章 Spring -WS - 第一個應用程式 中,我們使用 Spring WS 配置自動生成了 WSDL。在本例中,我們將展示如何使用 Spring WS 公開現有的 WSDL。

步驟 描述
1 如 Spring WS - 第一個應用程式章節中所述,建立一個名為 leaveService 的專案,放在 com.tutorialspoint 包下。
2 在 /WEB-INF/wsdl 子資料夾下建立一個 WSDL leave.wsdl。
3 更新 /WEB-INF 子資料夾下的 spring-ws-servlet.xml。我們在這裡使用 static-wsdl 標記而不是 dynamic-wsdl。
4 最後一步是建立所有原始檔和配置檔案的內容,並匯出應用程式,如下所述。

/WEB-INF/spring-ws-servlet.xml

<wsdl:definitions xmlns:wsdl = "http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:schema = "https://tutorialspoint.tw/hr/schemas"
   xmlns:tns = "https://tutorialspoint.tw/hr/definitions"
   targetNamespace = "https://tutorialspoint.tw/hr/definitions">
   
   <wsdl:types>
      <xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
         <xsd:import namespace = "https://tutorialspoint.tw/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 = "https://:8080/leaveService/"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

/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.tutorialspoint.hr"/>
   <sws:annotation-driven/>
   <sws:static-wsdl id = "leave" location = "/WEB-INF/wsdl/leave.wsdl"/>
</beans>

執行專案

完成原始檔和配置檔案的建立後,應匯出應用程式。右鍵單擊應用程式,使用匯出→WAR 檔案選項並將 leaveService.war 檔案儲存在 Tomcat 的 webapps 資料夾中。

現在,啟動 Tomcat 伺服器並確保我們能夠使用標準瀏覽器從 webapps 資料夾訪問其他網頁。嘗試訪問 URL – https://:8080/leaveService/leave.wsdl,如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。

Static WSDL result

Spring WS - 編寫伺服器

在本章中,我們將瞭解如何使用 Spring WS 建立 Web 應用程式伺服器。

步驟 描述
1 如 Spring WS - 第一個應用程式章節中所述,建立一個名為 countryService 的專案,放在 com.tutorialspoint 包下。
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://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>

建立專案

讓我們開啟命令控制檯,轉到 C:\MVN 目錄並執行以下 mvn 命令。

C:\MVN>mvn archetype:generate -DarchetypeGroupId = org.springframework.ws 
-DarchetypeArtifactId = spring-ws-archetype -DgroupId = com.tutorialspoint 
-DartifactId = countryService

Maven 將開始處理並建立完整的 Java 應用程式專案結構。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] Using property: groupId = com.tutorialspoint
[INFO] Using property: artifactId = countryService
Define value for property 'version':  1.0-SNAPSHOT: :
[INFO] Using property: package = com.tutorialspoint
Confirm properties configuration:
groupId: com.tutorialspoint
artifactId: countryService
version: 1.0-SNAPSHOT
package: com.tutorialspoint
 Y: :
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 spring-ws-archetype:2.0.0-M1
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.tutorialspoint
[INFO] Parameter: packageName, Value: com.tutorialspoint
[INFO] Parameter: package, Value: com.tutorialspoint
[INFO] Parameter: artifactId, Value: countryService
[INFO] Parameter: basedir, Value: C:\mvn
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\mvn\countryService
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.989 s
[INFO] Finished at: 2017-01-21T11:18:31+05:30
[INFO] Final Memory: 17M/178M
[INFO] ------------------------------------------------------------------------

現在轉到 C:/MVN 目錄。我們將看到一個名為 countryService(如 artifactId 中指定)的 Java 應用程式專案。更新 pom.xml。

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/maven-v4_0_0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.hr</groupId>
   <artifactId>countryService</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>countryService Spring-WS Application</name>
   <url>http://www.springframework.org/spring-ws</url>
   
   <build>
      <finalName>countryService</finalName>
   </build>
   
   <dependencies>
      <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>
</project>

建立域類

將 countries.xsd 複製到 C:\mvn\countryService\src\main\resources 資料夾中。讓我們開啟命令控制檯,轉到 C:\mvn\countryService\src\main\resources 目錄並執行以下 xjc 命令以使用 countries.xsd 生成域類。

C:\MVN\countryService\src\main\resources>xjc -p com.tutorialspoint countries.xsd

Maven 將開始處理並在 com.tutorialspoint 包中建立域類。

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

在 C:\mvn\countryService\src\main 資料夾中建立資料夾 java。將所有類複製到 C:\mvn\countryService\src\main\java 資料夾中。建立 CountryRepository 和 CountryEndPoint 分別表示國家/地區資料庫和國家/地區伺服器。

CountryRepository.java

package com.tutorialspoint;

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 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) {
      Assert.notNull(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.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.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;
   }
}

/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.tutorialspoint"/>
   <sws:annotation-driven/>

   <sws:dynamic-wsdl id="countries"
      portTypeName = "CountriesPort"
      locationUri = "/countryService/"
      targetNamespace = "https://tutorialspoint.tw/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>

構建專案

讓我們開啟命令控制檯。轉到 C:\MVN\countryService 目錄並執行以下 mvn 命令。

C:\MVN\countryService>mvn clean package

Maven 將開始構建專案。

INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building countryService Spring-WS Application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ countryService ---
[INFO] Deleting C:\mvn\countryService\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ countrySer
vice ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ countryService
---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. 
build is platform dependent!
[INFO] Compiling 4 source files to C:\mvn\countryService\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ co
untryService ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\mvn\countryService\src\test\resour
ces
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ country
Service ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryService ---

[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ countryService ---
[INFO] Packaging webapp
[INFO] Assembling webapp [countryService] in [C:\mvn\countryService\target\count
ryService]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\mvn\countryService\src\main\webapp]
[INFO] Webapp assembled in [5137 msecs]
[INFO] Building war: C:\mvn\countryService\target\countryService.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.484 s
[INFO] Finished at: 2017-01-28T09:07:59+05:30
[INFO] Final Memory: 19M/170M
[INFO] ------------------------------------------------------------------------

執行專案

建立原始檔和配置檔案後,將 countryService.war 檔案匯出到 Tomcat 的 webapps 資料夾中。

現在,啟動 Tomcat 伺服器並確保我們能夠使用標準瀏覽器從 webapps 資料夾訪問其他網頁。向 URL – https://:8080/countryService/ 發出 POST 請求,並使用任何 SOAP 客戶端發出以下請求。

<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>

您將看到以下結果。

<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 - 伺服器單元測試

在本章中,我們將瞭解如何對使用 Spring WS 建立的 Web 應用程式服務進行單元測試。

步驟 描述
1 更新在 Spring WS – 編寫伺服器章節中建立的專案 countryService。新增 src/test/java 資料夾。
2 在 – src/test/java/com/tutorialspoint/ws 資料夾下建立 CustomerEndPointTest.java,然後按如下所示更新 POM.xml。
3 在 src/main/resources 子資料夾下新增 spring-context.xml。
4 最後一步是建立所有原始檔和配置檔案的內容,並測試應用程式,如下所述。

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/maven-v4_0_0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>countryService</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>countryService Spring-WS Application</name>
   <url>http://www.springframework.org/spring-ws</url>
   <build>
      <finalName>countryService</finalName>
   </build>
   
   <dependencies>
      <dependency>
         <groupId>org.springframework.ws</groupId>
         <artifactId>spring-ws-core</artifactId>
         <version>2.4.0.RELEASE</version>
      </dependency>
      
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>2.5</version>
      </dependency>
      
      <dependency>
         <groupId>org.springframework.ws</groupId>
         <artifactId>spring-ws-test</artifactId>
         <version>2.4.0.RELEASE</version>
      </dependency>
      
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>3.1.2.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>
      
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.5</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</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 javax.xml.transform.Source;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

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.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;

import static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.payload;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = "/spring-context.xml" )
public class CustomerEndPointTest {
   @Autowired
   private ApplicationContext applicationContext;
   private MockWebServiceClient mockClient;

   @Before
   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));
   }	
}

構建專案

讓我們開啟命令控制檯,轉到 C:\MVN\countryService 目錄並執行以下 mvn 命令。

C:\MVN\countryService>mvn test

Maven 將開始構建和測試專案。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building countryService Spring-WS Application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ countrySer
vice ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ countryService
---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ co
untryService ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\countryService\src\test\resour
ces
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ country
Service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryService ---

[INFO] Surefire report directory: C:\MVN\countryService\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.ws.CustomerEndPointTest
Feb 27, 2017 11:49:30 AM org.springframework.test.context.TestContextManager ret
rieveTestExecutionListeners
INFO: @TestExecutionListeners is not present for class [class com.tutorialspoint
.ws.CustomerEndPointTest]: using defaults.
Feb 27, 2017 11:49:30 AM org.springframework.beans.factory.xml.XmlBeanDefinition
Reader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-context.xml]

Feb 27, 2017 11:49:30 AM org.springframework.context.support.GenericApplicationC
ontext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@b
2eddc0: startup date [Mon Feb 27 11:49:30 IST 2017]; root of context hierarchy
Feb 27, 2017 11:49:31 AM org.springframework.ws.soap.addressing.server.Annotatio
nActionEndpointMapping afterPropertiesSet
INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
Feb 27, 2017 11:49:31 AM org.springframework.ws.soap.saaj.SaajSoapMessageFactory
 afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.386 sec
Feb 27, 2017 11:49:31 AM org.springframework.context.support.GenericApplicationC
ontext doClose
INFO: Closing org.springframework.context.support.GenericApplicationContext@b2ed
dc0: startup date [Mon Feb 27 11:49:30 IST 2017]; root of context hierarchy

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.517 s
[INFO] Finished at: 2017-02-27T11:49:31+05:30
[INFO] Final Memory: 11M/109M
[INFO] ------------------------------------------------------------------------

Spring WS - 編寫客戶端

在本章中,我們將學習如何為在 Spring WS - 編寫伺服器 中建立的 Web 應用程式伺服器建立客戶端,使用 Spring WS。

步驟 描述
1 更新 com.tutorialspoint 包下的專案 countryService,如 Spring WS – 編寫伺服器章節中所述。
2 在 com.tutorialspoint.client 包下建立 CountryServiceClient.java,在 com.tutorialspoint 包下建立 MainApp.java,如以下步驟中所述。

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 = "https://:8080/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());
   }
}

啟動 Web 服務

啟動 Tomcat 伺服器並確保我們能夠使用標準瀏覽器從 webapps 資料夾訪問其他網頁。

測試 Web 服務客戶端

右鍵單擊 Eclipse 中應用程式下的 MainApp.java,使用 作為 Java 應用程式執行命令。如果應用程式一切正常,它將列印以下訊息。

Country : United States
Capital : Washington
Population : 46704314
Currency : USD

在這裡,我們為基於 SOAP 的 Web 服務建立了一個客戶端 - CountryServiceClient.java。MainApp 使用 CountryServiceClient 對 Web 服務進行命中,發出 POST 請求並獲取資料。

Spring WS - 客戶端單元測試

在本章中,我們將學習如何對在 Spring WS - 編寫客戶端 中建立的客戶端進行單元測試,該客戶端用於在章節 Spring WS - 編寫伺服器 中建立的 Web 應用程式伺服器,使用 Spring WS。

步驟 描述
1 更新 com.tutorialspoint 包下的專案 countryService,如 Spring WS - 編寫伺服器章節中所述。
2 在 com.tutorialspoint 包下的 SRC → Test → Java 資料夾中建立 CountryServiceClientTest.java,如以下步驟中所述。

CountryServiceClientTest.java

package com.tutorialspoint;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import com.tutorialspoint.client.CountryServiceClient;

public class CountryServiceClientTest {
   CountryServiceClient client;
   
   @Before
   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();
      Assert.assertEquals(expectedCountry.getCapital(), actualCountry.getCapital());
   }
}

啟動 Web 服務

啟動 Tomcat 伺服器並確保我們能夠使用標準瀏覽器從 webapps 資料夾訪問其他網頁。

單元測試 Web 服務客戶端

讓我們開啟命令控制檯,轉到 C:\MVN\countryService 目錄並執行以下 mvn 命令。

C:\MVN\countryService>mvn test

Maven 將開始構建和測試專案。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building countryService Spring-WS Application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ countrySer
vice ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ countryService
---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 10 source files to C:\MVN\countryService\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ co
untryService ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\countryService\src\test\resour
ces
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ country
Service ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 2 source files to C:\MVN\countryService\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryService ---

[INFO] Surefire report directory: C:\MVN\countryService\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.CountryServiceClientTest
Feb 27, 2017 8:45:26 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory
afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
Feb 27, 2017 8:45:26 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbC
ontextFromContextPath
INFO: Creating JAXBContext with context path [com.tutorialspoint]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.457 sec
Running com.tutorialspoint.ws.CustomerEndPointTest
Feb 27, 2017 8:45:27 PM org.springframework.test.context.TestContextManager retr
ieveTestExecutionListeners
INFO: @TestExecutionListeners is not present for class [class com.tutorialspoint
.ws.CustomerEndPointTest]: using defaults.
Feb 27, 2017 8:45:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionR
eader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-context.xml]

Feb 27, 2017 8:45:27 PM org.springframework.context.support.GenericApplicationCo
ntext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@5
17c642: startup date [Mon Feb 27 20:45:27 IST 2017]; root of context hierarchy
Feb 27, 2017 8:45:28 PM org.springframework.ws.soap.addressing.server.Annotation
ActionEndpointMapping afterPropertiesSet
INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
Feb 27, 2017 8:45:28 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory
afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.243 sec
Feb 27, 2017 8:45:28 PM org.springframework.context.support.GenericApplicationCo
ntext doClose
INFO: Closing org.springframework.context.support.GenericApplicationContext@517c
642: startup date [Mon Feb 27 20:45:27 IST 2017]; root of context hierarchy

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.686 s
[INFO] Finished at: 2017-02-27T20:45:28+05:30
[INFO] Final Memory: 17M/173M
[INFO] ------------------------------------------------------------------------
廣告