开发手册 欢迎您!
软件开发者资料库

Spring WS - 第一个应用程序

Spring Web Services第一个应用程序 - 从基本到高级概念的简单简单步骤学习Java Spring Framework 4.1.6版,包括示例环境设置,控制反转(IoC),依赖注入,bean范围,bean生命周期,内部bean,自动装配,不同模块,面向方面编程(AOP),数据库访问(JDBC),事务管理,Web MVC框架,Web流,异常处理,EJB集成和发送电子邮件等。

让我们开始用Spring-WS Framework编写一个基于SOAP的实际Web服务.在我们开始使用Spring-WS框架编写第一个例子中,我们必须确保春节-WS环境的设置是否正确,如的Spring Web解释服务 - 环境设置章节.我们假设读者对Eclipse IDE有一些基本的工作知识.

因此,让我们继续编写一个简单的Spring WS应用程序,它将公开一个Web服务方法来预订一个假在人力资源门户中.

契约优先方法

Spring-WS使用契约优先方法,这意味着我们应该拥有在编写任何基于JAVA的实现代码之前准备好XML Structures .我们正在定义一个LeaveRequest对象,它有子对象 -  Leave和Employee.

以下是必需的XML构造 :

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

                                                                                                                                                      

Create the Project

Let us now open a command console, go the C:\MVN directory and execute the following mvn command.

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

Maven will start processing and will create the complete Java Application Project Structure.

[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building Maven Stub Project (No POM) 1[INFO] ------------------------------------------------------------------------[INFO][INFO] Using property: groupId = com.it1352.hr[INFO] Using property: artifactId = leaveServiceDefine value for property 'version':  1.0-SNAPSHOT: :[INFO] Using property: package = com.it1352.hrConfirm properties configuration:groupId: com.it1352.hrartifactId: leaveServiceversion: 1.0-SNAPSHOTpackage: com.it1352.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.it1352.hr[INFO] Parameter: packageName, Value: com.it1352.hr[INFO] Parameter: package, Value: com.it1352.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] ------------------------------------------------------------------------

Now go to C:/MVN directory. We will see a java application project created named leaveService (as specified in artifactId). Update the pom.xml and add HumanResourceService.java and HumanResourceServiceImpl.java in the following folder – C:\MVN\leaveService\src\main\java\com\it1352\hr\service folder. Once that is done, then add LeaveEndpoint.java in the following folder – C:\MVN\leaveService\src\main\java\com\it1352\hr\ws folder.

pom.xml

      4.0.0   com.it1352.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.it1352.hr.service;import java.util.Date;public interface HumanResourceService {   void bookLeave(Date startDate, Date endDate, String name);}

HumanResourceServiceImpl.java

package com.it1352.hr.service;import java.util.Date;import org.springframework.stereotype.Service;@Servicepublic 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.it1352.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.it1352.hr.service.HumanResourceService;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.Namespace;import org.jdom.xpath.XPath;@Endpointpublic class LeaveEndpoint {   private static final String NAMESPACE_URI = "http://it1352.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

                                                                                                                                             

Build the Project

现在让我们打开命令控制台,进入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) @ leaveService ---[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. build is platform dependent![INFO] Compiling 3 source files to C:\mvn\leaveService\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ leaveService ---[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\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ leaveService ---[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\leaveService][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在其中创建LeaveService项目.

  • 单击完成按钮.

Run the Project

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

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

First Application result