Spring Boot整合spring-ws開發web service
新增依賴
spring boot的工程,除了spring boot外還需要新增spring-ws和wsdl4j的依賴,當然後面生成程式碼還需要新增maven的jaxb2外掛。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>
</dependency>
編寫schema檔案
spring-ws的釋出,都是以一個schema
檔案(xsd)定義開始的,它描述了web service的引數以及返回的資料。
下面是官方示例給出的countries.xsd
,這裡我們也以它為例,當然名稱空間改掉了,因為等會jaxb2外掛生成程式碼是以它來確定包名的:
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:tns="http://www.dexcoder.com/ws"
targetNamespace="http://www.dexcoder.com/ws"
<xs:elementname="getCountryRequest">
<xs:complexType>
<xs:sequence>
<xs:elementname="name"type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:elementname="getCountryResponse">
<xs:complexType>
<xs:sequence>
<xs:elementname="country"type="tns:country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexTypename="country">
<xs:sequence>
<xs:elementname="name"type="xs:string"/>
<xs:elementname="population"type="xs:int"/>
<xs:elementname="capital"type="xs:string"/>
<xs:elementname="currency"type="tns:currency"/>
</xs:sequence>
</xs:complexType>
<xs:simpleTypename="currency">
<xs:restrictionbase="xs:string">
<xs:enumerationvalue="GBP"/>
<xs:enumerationvalue="EUR"/>
<xs:enumerationvalue="PLN"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
新增maven的jaxb2外掛來生成程式碼
jaxb2外掛可以根據描述的xsd檔案來幫我們生成相應的ws程式碼,具體配置如下:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<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//schema</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
配置好外掛然後install
一下,這樣web service需要的服務端程式碼就已經幫我們生成好了,根據上面的xsd,生成的程式碼在com.dexcoder.ws
包下。
編寫Endpoint
我們就不再像spring-ws官方那樣再建一個Repository了,這裡直接返回。需要注意PayloadRoot
註解當中的namespace
和localPart
需要和xsd中對應。
@Endpoint
publicclassCountryEndpoint{
privatestaticfinalString NAMESPACE_URI ="http://www.dexcoder.com/ws";
@PayloadRoot(namespace= NAMESPACE_URI, localPart ="getCountryRequest")
@ResponsePayload
publicGetCountryResponse getCountry(@RequestPayloadGetCountryRequest request){
GetCountryResponse response =newGetCountryResponse();
Country poland =newCountry();
poland.setName("Poland-"+ request.getName());
poland.setCapital("Warsaw");
poland.setCurrency(Currency.PLN);
poland.setPopulation(38186860);
response.setCountry(poland);
return response;
}
}
在spring boot中配置web service
在spring boot中配置spring-ws十分簡單,畢竟是同一家的東西,相容性應該沒的說。程式碼如下:
@EnableWs
@Configuration
publicclassWebServiceConfigextendsWsConfigurerAdapter{
@Bean
publicServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext){
MessageDispatcherServlet servlet =newMessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
returnnewServletRegistrationBean(servlet,"/ws/*");
}
@Bean(name ="countries")
publicDefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema){
DefaultWsdl11Definition wsdl11Definition =newDefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("CountriesPort");
wsdl11Definition.setSchema(countriesSchema);
return wsdl11Definition;
}
@Bean
publicXsdSchema countriesSchema(){
returnnewSimpleXsdSchema(newClassPathResource("schema/countries.xsd"));
}
}
到這裡spring-ws的所有配置和工作都已經完成了,上面的DefaultWsdl11Definition
id預設就是釋出的ws的訪問路徑。
啟動專案
最後一步當然是啟動專案了,這裡依然使用spring boot的啟動方式:
@SpringBootApplication
publicclassApplicationStartup{
publicstaticvoid main(String[] args){
SpringApplication.run(ApplicationStartup.class);
}
}
啟動後訪問 http://localhost:8080/ws/countries.wsdl 發現web service已經成功釋出了。
這裡要注意一下spring-ws釋出的web service是以後綴.wsdl
訪問的,跟傳統的?wsdl
不大一樣,也看過它的原始碼,發現是在判斷後綴時寫死的,所以沒辦法配置修改了。
還有就是spring-ws實際上把釋出wsdl
和真正的服務實現Endpoint
分開了,如果你的Endpoint
不正確,很可能會出現瀏覽器訪問.wsdl
地址看起來正常而客戶端呼叫卻出現Not
Found 404的錯誤。