📜  Spring WS-写作服务器

📅  最后修改于: 2020-11-11 06:44:38             🧑  作者: Mango


在本章中,我们将了解如何使用Spring WS创建Web应用程序服务器。

Step Description
1 Create a project with a name countryService under a package com.tutorialspoint as explained in the Spring WS – First Application chapter.
2 Create countries.xsd, domain classes, CountryRepository and CountryEndPoint as explained in the following steps.
3 Update spring-ws-servlet.xml under the /WEB-INF sub-folder.
4 The final step is to create content for all the source and configuration files and export the application as explained below.

country.xsd



   
      
         
            
         
      
   

   
      
         
            
         
      
   

   
      
         
         
         
         
      
   

   
      
         
         
         
      
   

创建项目

让我们打开命令控制台,进入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的Java应用程序项目(在artifactId中指定)。更新pom.xml。

pom.xml



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

创建域类

将country.xsd复制到C:\ mvn \ countryService \ src \ main \ resources文件夹中。让我们打开命令控制台,转到C:\ mvn \ countryService \ src \ main \ resources目录,然后执行以下xjc命令以使用country.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 countries = new ArrayList();

   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



   
   

   
      
   

/WEB-INF/web.xml



   TutorialsPoint Country Service

   
      spring-ws
      org.springframework.ws.transport.http.MessageDispatcherServlet
      
      
         transformWsdlLocations
         true
      
   
   
   
      spring-ws
      /*
   

建立项目

让我们打开命令控制台。转到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发出POST请求-http:// localhost:8080 / countryService /,然后使用任何SOAP客户端发出以下请求。


   
United States

您将看到以下结果。


   
United States 46704314 Washington USD