Spring WS入门程序

在这一章节中,我们将使用Spring-WS框架编写一个基于SOAP的Web服务。 在开始使用Spring-WS框架编写第一个示例应用之前,必须确保Spring-WS环境的设置正确,正如Spring Web Services环境设置章节中所述。并假设读者对Eclipse IDE有一些基本的工作知识。

下面开始编写一个简单的Spring WS应用程序,这个应用程序将公开Web服务方法以在HR入口中预订假期。

契约优先方法

Spring-WS使用契约优先方法,因此在编写基于JAVA的实现代码之前,我们应该准备好XML结构。 下面定义一个包含子对象 - LeaveEmployeeLeaveRequest对象。

以下是所需的XML结构 -

文件:WEB-INF/Leave.xml 的内容如下所示 -

<Leave xmlns = "http://www.zyiz.net/hr/schemas">
   <StartDate>2018-07-03</StartDate>
   <EndDate>2018-07-30</EndDate>
</Leave>

文件:WEB-INF/Employee.xml 的内容如下所示 -

<Employee xmlns = "http://www.zyiz.net/hr/schemas">
   <Number>10910</Number>
   <FirstName>Max</FirstName>
   <LastName>Su</LastName>
</Employee>

文件:WEB-INF/LeaveRequest.xml 的内容如下所示 -

<LeaveRequest xmlns = "http://www.zyiz.net/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 的内容如下所示 -

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
   xmlns:hr = "http://www.zyiz.net/hr/schemas"
   elementFormDefault = "qualified"
   targetNamespace = "http://www.zyiz.net/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>

创建项目

现在打开Eclipse,从菜单中找到:文件 -> New -> Maven Project, 打开界面如下所示:

进入下一步,选择 maven-archetype-webapp,如下所示:

填写项目相关信息(可根据你的实际情况填写),如下所示 -

完整的项目目录结构如下图所示 -

以下是各个文件中的代码 -

文件: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>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.zyiz.hr.service;

import java.util.Date;

public interface HumanResourceService {
    void bookLeave(Date startDate, Date endDate, String name);
}

文件:HumanResourceServiceImpl.java -

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

   <sws:dynamic-wsdl id = "leave"
      portTypeName = "HumanResource"
      locationUri = "/leaveService/"
      targetNamespace = "http://www.zyiz.net/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>zyiz 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.zyiz.net/hr/schemas"
   elementFormDefault = "qualified"
   targetNamespace = "http://www.zyiz.net/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>

运行项目

当完成创建源文件和配置文件,使用Maven构建应用程序。 右键单击应用程序项目名称,在弹出的菜单中选择:Run As -> Maven Build… ,如下所示 -

构建成功后, 右键单击应用程序项目名称,在弹出的菜单中选择:Run As -> Run On Server… ,如下所示 -

Eclipse启动Tomcat服务器后, 尝试访问URL => http://localhost:8080/leaveService/leave.wsdl,如果Spring Web应用程序一切正常,应该看到以下输出结果 -


上一篇:Maven+Java入门程序

下一篇:Spring WS静态WSDL

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程