在這一章節(jié)中,我們將使用Spring-WS框架編寫一個(gè)基于SOAP的Web服務(wù)。 在開始使用Spring-WS框架編寫第一個(gè)示例應(yīng)用之前,必須確保Spring-WS環(huán)境的設(shè)置正確,正如Spring Web Services環(huán)境設(shè)置章節(jié)中所述。并假設(shè)讀者對(duì)Eclipse IDE有一些基本的工作知識(shí)。
下面開始編寫一個(gè)簡(jiǎn)單的Spring WS應(yīng)用程序,這個(gè)應(yīng)用程序?qū)⒐_Web服務(wù)方法以在HR入口中預(yù)訂假期。
Spring-WS使用契約優(yōu)先方法,因此在編寫基于JAVA的實(shí)現(xiàn)代碼之前,我們應(yīng)該準(zhǔn)備好XML結(jié)構(gòu)。 下面定義一個(gè)包含子對(duì)象 - Leave
和Employee
的LeaveRequest
對(duì)象。
以下是所需的XML結(jié)構(gòu) -
文件:WEB-INF/Leave.xml 的內(nèi)容如下所示 -
<Leave xmlns = "http://www.yiibai.com/hr/schemas">
<StartDate>2018-07-03</StartDate>
<EndDate>2018-07-30</EndDate>
</Leave>
文件:WEB-INF/Employee.xml 的內(nèi)容如下所示 -
<Employee xmlns = "http://www.yiibai.com/hr/schemas">
<Number>10910</Number>
<FirstName>Max</FirstName>
<LastName>Su</LastName>
</Employee>
文件:WEB-INF/LeaveRequest.xml 的內(nèi)容如下所示 -
<LeaveRequest xmlns = "http://www.yiibai.com/hr/schemas">
<Leave>
<StartDate>2018-07-03</StartDate>
<EndDate>2018-07-30</EndDate>
</Leave>
<Employee>
<Number>10010</Number>
<FirstName>Max</FirstName>
<LastName>Su</LastName>
</Employee>
</LeaveRequest>
文件:WEB-INF/hr.xsd 的內(nèi)容如下所示 -
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
xmlns:hr = "http://www.yiibai.com/hr/schemas"
elementFormDefault = "qualified"
targetNamespace = "http://www.yiibai.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>
現(xiàn)在打開Eclipse,從菜單中找到:文件 -> New -> Maven Project, 打開界面如下所示:
進(jìn)入下一步,選擇 maven-archetype-webapp,如下所示:
填寫項(xiàng)目相關(guān)信息(可根據(jù)你的實(shí)際情況填寫),如下所示 -
完整的項(xiàng)目目錄結(jié)構(gòu)如下圖所示 -
以下是各個(gè)文件中的代碼 -
文件: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>leaveService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>leaveService 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>leaveService</finalName>
<defaultGoal>compile</defaultGoal>
</build>
</project>
文件:HumanResourceService.java -
package com.yiibai.hr.service;
import java.util.Date;
public interface HumanResourceService {
void bookLeave(Date startDate, Date endDate, String name);
}
文件:HumanResourceServiceImpl.java -
package com.yiibai.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.yiibai.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.yiibai.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 = "http://www.yiibai.com/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.yiibai.hr"/>
<bean id = "humanResourceService"
class = "com.yiibai.hr.service.HumanResourceServiceImpl" />
<sws:annotation-driven/>
<sws:dynamic-wsdl id = "leave"
portTypeName = "HumanResource"
locationUri = "/leaveService/"
targetNamespace = "http://www.yiibai.com/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>Yiibai 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 -
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
xmlns:hr = "http://www.yiibai.com/hr/schemas"
elementFormDefault = "qualified"
targetNamespace = "http://www.yiibai.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>
當(dāng)完成創(chuàng)建源文件和配置文件,使用Maven構(gòu)建應(yīng)用程序。 右鍵單擊應(yīng)用程序項(xiàng)目名稱,在彈出的菜單中選擇:Run As -> Maven Build… ,如下所示 -
構(gòu)建成功后, 右鍵單擊應(yīng)用程序項(xiàng)目名稱,在彈出的菜單中選擇:Run As -> Run On Server… ,如下所示 -
Eclipse啟動(dòng)Tomcat服務(wù)器后, 嘗試訪問(wèn)URL => http://localhost:8080/leaveService/leave.wsdl
,如果Spring Web應(yīng)用程序一切正常,應(yīng)該看到以下輸出結(jié)果 -