📜  Spring WS-第一个应用程序

📅  最后修改于: 2020-11-11 06:42:39             🧑  作者: Mango


让我们开始使用Spring-WS Framework编写实际的基于SOAP的Web服务。在开始使用Spring-WS框架编写第一个示例之前,我们必须确保按照Spring Web Services-Environment Setup一章中的说明正确设置Spring-WS环境。我们假设读者具有Eclipse IDE的一些基本工作知识。

因此,让我们继续编写一个简单的Spring WS Application,它将公开一个Web服务方法以在HR门户中预订假期。

合同优先法

Spring-WS使用“合同优先”方法,这意味着在编写任何基于JAVA的实现代码之前,我们应该准备好XML结构。我们正在定义一个LeaveRequest对象,该对象具有子对象– Leave和Employee。

以下是必需的XML结构-

离开.xml


   2016-07-03
   2016-07-07

Employee.xml


   404
   Mahesh
   Parashar

LeaveRequest.xml


   
      2016-07-03
      2016-07-07
   
   
   
      404
      Mahesh
      Parashar
   

hr.xsd


   
   
      
         
            
            
         
      
   
   
   
      
         
         
      
   
   
   
      
         
         
         
      
   

创建项目

现在让我们打开一个命令控制台,进入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目录。我们将看到一个创建的Java应用程序项目,名为LeaveService (在artifactId中指定)。更新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



   
   4.0.0
   com.tutorialspoint.hr
   leaveService
   war
   1.0-SNAPSHOT
   leaveService Spring-WS Application
   http://www.springframework.org/spring-ws
   
   
      leaveService
   
   
   
      
         org.springframework.ws
         spring-ws-core
         2.4.0.RELEASE
      
      
      
         jdom
         jdom
         1.0
      
      
      
         jaxen
         jaxen
         1.1
      
      
      
         wsdl4j
         wsdl4j
         1.6.2
      
   

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 = "http://tutorialspoint.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



   
   
   

   
      
   

/WEB-INF/web.xml



   TutorialsPoint HR Leave Service
   
      spring-ws
      
         org.springframework.ws.transport.http.MessageDispatcherServlet
      
      
         transformWsdlLocations
         true
      
   

   
      spring-ws
      /*
   

/WEB-INF/hr.xsd



   
      
         
            
            
         
      
   

   
      
         
         
      
   

   
      
         
         
         
      
   

建立项目

现在让我们打开命令控制台,进入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项目选项”。单击下一步按钮。

  • 选择项目位置,其中使用Maven在其中创建了叶子服务项目

  • 单击完成按钮。

运行项目

完成创建源文件和配置文件后,导出应用程序。右键单击该应用程序,使用“导出”→“ WAR文件”选项,然后将LeaveService.war文件保存在Tomcat的webapps文件夹中。

启动Tomcat服务器,并确保我们能够使用标准浏览器从webapps文件夹访问其他网页。尝试访问URL – http:// localhost:8080 / leaveService / leave.wsdl,如果Spring Web Application一切正常,我们应该看到以下屏幕。

首次申请结果