Spring Web Services(下簡稱ws)本質上是基於SpringBoot的項目,所以若是有對SpringBoot不太瞭解的同窗,回頭再來看比較合適。java
ws分爲server端與client端兩個部分,本文旨在介紹框架搭建的流程與重點。web
1、ws.server端搭建spring
創建Server的關鍵是首先創建xsd文件。xsd文件是xml文件的定義與基礎,你但願別人如何訪問與獲取你的數據都須要在xsd文件中說明。apache
countries.xsdjson
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://learnhow.org/ws/schema" targetNamespace="http://learnhow.org/ws/schema" elementFormDefault="qualified"> <xs:element name="getCountryRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getCountryResponse"> <xs:complexType> <xs:sequence> <xs:element name="country" type="tns:country" /> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="country"> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="population" type="xs:int" /> <xs:element name="capital" type="xs:string" /> <xs:element name="currency" type="tns:currency" /> <xs:element name="language" type="tns:language" /> </xs:sequence> </xs:complexType> <xs:simpleType name="currency"> <xs:restriction base="xs:string"> <xs:enumeration value="GBP" /> <xs:enumeration value="EUR" /> <xs:enumeration value="PLN" /> </xs:restriction> </xs:simpleType> <xs:complexType name="language"> <xs:sequence> <xs:element name="name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema>
users.xsdapi
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://learnhow.org/ws/schema" targetNamespace="http://learnhow.org/ws/schema" elementFormDefault="qualified"> <xs:element name="getUserRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getUserResponse"> <xs:complexType> <xs:sequence> <xs:element name="user" type="tns:user" /> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="user"> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="gender" type="tns:gender" /> <xs:element name="age" type="xs:int" /> <xs:element name="address" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:simpleType name="gender"> <xs:restriction base="xs:string"> <xs:enumeration value="MALE" /> <xs:enumeration value="FEMALE" /> </xs:restriction> </xs:simpleType> </xs:schema>
這兩個文件默認請存放於 src/main/resources 目錄下,以下圖所示:瀏覽器
創建完成之後咱們能夠着手編寫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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.learnhow</groupId> <artifactId>ws.server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ws.server</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </plugin> </plugins> </build> </project>
重點是最後的一項maven plugin,它會讀取resources目錄下的xsd文件並在 src/main/java 目錄下創建.java文件。須要注意的是代碼的package路徑是經過xsd的targetNamespace事先指定的。網絡
代碼文件被maven建立完成之後表明第一段工做順利完成。下面咱們須要人工編寫Endpoint類,即創建對外訪問的服務接口。一般你提供了幾分xsd文件就應該建立幾個Endpoint類。Endpoint本質上是接收一個request,而後通過你的業務邏輯再返回一個response。與傳統意義上的瀏覽器不一樣,後者一般傳輸json字符串,而前者則是xml。app
CountryEndpoint
package org.learnhow.ws.server; import org.learnhow.ws.schema.GetCountryRequest; import org.learnhow.ws.schema.GetCountryResponse; 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; @Endpoint public class CountryEndpoint { private static final String NAMESPACE_URI = "http://learnhow.org/ws/schema"; @Autowired private CountryRepository countryRepository; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest") @ResponsePayload public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) { GetCountryResponse response = new GetCountryResponse(); response.setCountry(countryRepository.findCountry(request.getName())); return response; } }
UserEndpoint
package org.learnhow.ws.server; import org.learnhow.ws.schema.GetUserRequest; import org.learnhow.ws.schema.GetUserResponse; 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; @Endpoint public class UserEndpoint { private static final String NAMESPACE_URI = "http://learnhow.org/ws/schema"; @Autowired private UserRepository userRepository; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getUserRequest") @ResponsePayload public GetUserResponse getUser(@RequestPayload GetUserRequest request) { GetUserResponse response = new GetUserResponse(); response.setUser(userRepository.findUser(request.getName())); return response; } }
很顯然你的業務邏輯應該封裝在CountryRepository和UserRepository對象裏。接下來建立CountryRepository對象。
CountryRepository
package org.learnhow.ws.server; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.learnhow.ws.schema.Country; import org.learnhow.ws.schema.Currency; import org.learnhow.ws.schema.Language; import org.springframework.stereotype.Component; @Component public class CountryRepository { private static final Map<String, Country> countries = new HashMap<>(); @PostConstruct public void initData() { Country spain = new Country(); spain.setName("Spain"); spain.setCapital("Madrid"); spain.setCurrency(Currency.EUR); spain.setPopulation(46704314); Language spanish = new Language(); spanish.setName("spanish"); spain.setLanguage(spanish); Country poland = new Country(); poland.setName("Poland"); poland.setCapital("Warsaw"); poland.setCurrency(Currency.PLN); poland.setPopulation(38186860); Language polish = new Language(); polish.setName("polish"); poland.setLanguage(polish); Country uk = new Country(); uk.setName("United Kingdom"); uk.setCapital("London"); uk.setCurrency(Currency.GBP); uk.setPopulation(63705000); Language english = new Language(); english.setName("english"); uk.setLanguage(english); countries.put(spain.getName(), spain); countries.put(poland.getName(), poland); countries.put(uk.getName(), uk); } public Country findCountry(String name) { return countries.get(name); } }
UserRepository(略)
最後是編寫configuration,它是整個框架調用的核心。
package org.learnhow.ws.server; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.ws.config.annotation.EnableWs; import org.springframework.ws.config.annotation.WsConfigurerAdapter; import org.springframework.ws.transport.http.MessageDispatcherServlet; import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; import org.springframework.xml.xsd.SimpleXsdSchema; import org.springframework.xml.xsd.XsdSchema; @EnableWs @Configuration public class WebServiceConfig extends WsConfigurerAdapter { @Bean public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean(servlet, "/ws/*"); } @Bean(name = "countries") public DefaultWsdl11Definition defaultWsdl11DefinitionCountry() { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("CountriesPort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://learnhow.org/ws/schema"); wsdl11Definition.setSchema(countriesSchema()); return wsdl11Definition; } @Bean(name = "users") public DefaultWsdl11Definition defaultWsdl11DefinitionUser() { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("CountriesPort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://learnhow.org/ws/schema"); wsdl11Definition.setSchema(usersSchema()); return wsdl11Definition; } @Bean public XsdSchema countriesSchema() { return new SimpleXsdSchema(new ClassPathResource("countries.xsd")); } @Bean public XsdSchema usersSchema() { return new SimpleXsdSchema(new ClassPathResource("users.xsd")); } }
最後一步:編寫啓動項Application
package org.learnhow.ws; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
完成的目錄結構以下:
2、測試
啓動application,打開瀏覽器訪問:http://localhost:8080/ws/countries.wsdl
與http://localhost:8080/ws/users.wsdl 若是頁面分別展現了兩份xml文件表明服務器已經能夠正常運行了。也能夠用SoapUI Pro進一步測試數據的讀取和發送是否正常。
3、ws.client端搭建
若是說server端是經過xsd產生java與WSDL的過程那麼client端就偏偏相反。咱們仍是使用maven工具經過服務器暴露在外的wsdl文件創建java對象。首先編輯pom.xml文件配置依賴和wsdl訪問路徑。
<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>org.learnhow</groupId> <artifactId>ws.client</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.13.2</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaLanguage>WSDL</schemaLanguage> <generatePackage>ws.wsdl</generatePackage> <schemas> <schema> <url>http://localhost:8080/ws/countries.wsdl</url> </schema> <schema> <url>http://localhost:8080/ws/users.wsdl</url> </schema> </schemas> </configuration> </plugin> </plugins> </build> </project>
這裏要注意,若是此時你已經將server的進程中止,也就是wsdl沒法訪問,pom.xml文件會報錯。看上去大概會像這樣:
正常狀況下maven會自動幫你在target目錄下創建java對象,目錄結構以下:
若是你發現沒法正常創建java對象,請首先檢查如下兩點:
maven引入的依賴是否完整:國內的網絡環境不是很好,有時候經過maven搭建環境常常會在運行時報出各類莫名其妙的錯誤。其中絕大多數其實都是因爲依賴引入不完整形成的。此時你可能須要對Maven Dependencies目錄下的jar包逐一檢查。
maven結構錯誤:maven的版本不少,不一樣的版本間可能在元素的結構定義上有所差距。若是在<plugins>節點報錯能夠考慮在外層再包一層<pluginManagement>節點。具體緣由我也沒有深究,只能說「有時管用」,若是你對maven有深刻的瞭解也但願告訴我。
若是以上環境你進行的都很順利,那麼恭喜你80%的工做已經完成了。
下面是編寫client客戶端代碼:
package org.learnhow.ws.client; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; import org.springframework.ws.soap.client.core.SoapActionCallback; import ws.wsdl.GetCountryRequest; import ws.wsdl.GetCountryResponse; public class CountryClient extends WebServiceGatewaySupport { public static final String URI = "http://localhost:8080/ws"; public static final String SOAPACTION = "http://learnhow.org/ws/schema/getUserRequest"; public GetCountryResponse getCountry(String countryName) { GetCountryRequest request = new GetCountryRequest(); request.setName(countryName); GetCountryResponse response = (GetCountryResponse) getWebServiceTemplate().marshalSendAndReceive(URI, request, new SoapActionCallback(SOAPACTION)); return response; } }
而後依然是建立configuration供框架調用
package org.learnhow.ws.client; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller; @Configuration public class AppConfiguration { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("ws.wsdl"); return marshaller; } @Bean("country") public CountryClient counrtyClient(Jaxb2Marshaller marshaller) { CountryClient client = new CountryClient(); client.setDefaultUri(CountryClient.URI); client.setMarshaller(marshaller); client.setUnmarshaller(marshaller); return client; } @Bean("user") public UserClient userClient(Jaxb2Marshaller marshaller) { UserClient client = new UserClient(); client.setDefaultUri(UserClient.URI); client.setMarshaller(marshaller); client.setUnmarshaller(marshaller); return client; } }
最後是編寫Application啓動項
package org.learnhow.ws; import org.learnhow.ws.client.CountryClient; import org.learnhow.ws.client.UserClient; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import ws.wsdl.GetCountryResponse; import ws.wsdl.GetUserResponse; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean CommandLineRunner lookup(CountryClient client) { return args -> { String countryName = "Spain"; if (args.length > 0) { countryName = args[0]; } GetCountryResponse response = client.getCountry(countryName); System.out.println("response: " + response.getCountry().getName()); }; } }
運行application,若是你能看到控制檯有 "response: Spain" 打出表明數據已經可以正常獲取。
後記:
本文的代碼邏輯主要參考了Spring Web Services官網文檔。另外WebService除了經過Spring還有多種實現手段,感興趣的同窗能夠看看如何使用wsimport工具以及Tomcat發佈WebService的例子。這裏再也不贅述。