Axis2與Spring整合併發布多個service(同樣使用services.xml)
上一篇Axis2使用services.xml進行開發server與client演示了axis2使用service.xml開發webservice服務端與客戶端,並未與Spring進行整合。
本篇演示與spring整合下服務端的開發併發布兩個service(客戶端如何呼叫,參考上篇)。其實也就是把bean交給Spring容器來管理。
測試上除了你基於SOAP使用Client呼叫service,Axis2預設情況下還支援Restful風格。當然後者僅支援簡單型別引數,二進位制檔案等不支援。
【1】環境配置
服務端繼續沿用上一個專案,不過要新增spring包/axis2與spring整合jar並修改配置。
① 新增jar;
② 新增applicationContext.xml並配置如下:
只配了自動掃描,說明類使用了註解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx ="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
<context:component-scan base-package="com.web"></context:component-scan>
</beans>
③ 為介面實現類新增@Service註解
④ 修改web.xml 如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
⑤ 修改services.xml
路徑如下:
配置如下:
<service name="AxisSpringService">
<description>AxisSpringService</description>
<!--
SpringBeanName作用類似於普通配置中的ServiceClass,都是用來建立服務類物件,只不過普通配置使用反射來建立 。
加入Spring之後,物件的建立交給了Spring的IOC容器,SpringBeanName指定要釋出成WebService的Java類,SpringBeanName引數是JavaBean的名稱。
SpringBeanName固定的不能改 ,因為springWebService是spring中註冊的實現類得id。
如果不使用spring,可以使用ServiceClass屬性,ServiceClass引數要指定要釋出成WebService的Java類,並指定全類名的方式。
-->
<parameter name="SpringBeanName">
myServiceImpl
</parameter>
<!-- 通過ServiceObjectSupplier引數指定SpringServletContextObjectSupplier類來獲得Spring的ApplicationContext物件 -->
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<!--
在這裡最值得注意的是<messageReceivers>元素,該元素用於設定處理WebService方法的處理器。
例如,getAge方法有一個返回值,因此,需要使用可處理輸入輸出的RPCMessageReceiver類,
而update方法沒有返回值,因此,需要使用只能處理輸入的RPCInOnlyMessageReceiver類。
-->
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation>
</service>
【2】部署到Tomcat,執行
獲取的wsdl地址:
http://localhost:8080/Axis2/services/AxisSpringService?wsdl
即,schema+IP+port+contextPath+services+serviceName+?wsdl
targetNamespace如下:
http://impl.service.Axis2.web.com
如果要為option設定action,則示例如下:
Options options = new Options();
// 指定呼叫WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setAction("http://impl.service.Axis2.web.com/sayHello");
即,targetNamespace+method
瀏覽器出現如下圖說明正常:
使用上一篇中的客戶端進行測試(url變了)
客戶端輸出結果如下:
使用Restful風格進行測試:
瀏覽器輸入地址:
http://localhost:8080/Axis2/services/
AxisSpringService2/sayHello?name=tom
結果如下圖:
服務端輸出結果如下:
【3】配置併發布兩個service
上面演示的一個service下axis2與spring整合。如果兩個service呢?下面進行演示。
① 拷貝MyServiceImpl並重命名為MyServiceImpl2
一個介面,兩個實現類,釋出成兩個不同的service。
修改其方法如下:
package com.web.Axis2.service.impl;
import org.springframework.stereotype.Service;
import com.web.Axis2.service.MyService;
@Service
public class MyServiceImpl2 implements MyService{
@Override
public String sayHello(String name) {
//service2 用於區分是第二個service的方法被呼叫
System.out.println("this is service2 "+name);
return "hello "+name;
}
}
② 修改services.xml
<serviceGroup>
<service name="AxisSpringService">
<description>AxisSpringService</description>
<parameter name="SpringBeanName">
myServiceImpl
</parameter>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation>
</service>
<service name="AxisSpringService2">
<description>AxisSpringService2</description>
<parameter name="SpringBeanName">
myServiceImpl2
</parameter>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
</serviceGroup>
如上面配置所示,使用了serviceGroup元素。一個xml中只能有一個serviceGroup元素。每個serviceGroup元素下可以有多個service元素,每一個service元素表示一個WebService。
③ 使用不同的url進行測試
兩個service對應的url分別如下:
http://localhost:8080/Axis2/services/AxisSpringService?wsdl
http://localhost:8080/Axis2/services/AxisSpringService2?wsdl
其實就是service名字改變了。
檢視第一個wsdl檔案:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://impl.service.Axis2.web.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="http://impl.service.Axis2.web.com">
<wsdl:documentation>AxisSpringService</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://impl.service.Axis2.web.com">
<xs:element name="sayHello">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sayHelloResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="ns:sayHello"/>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="ns:sayHelloResponse"/>
</wsdl:message>
<wsdl:portType name="AxisSpringServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input message="ns:sayHelloRequest" wsaw:Action="urn:sayHello"/>
<wsdl:output message="ns:sayHelloResponse" wsaw:Action="urn:sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AxisSpringServiceSoap11Binding" type="ns:AxisSpringServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="urn:sayHello" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="AxisSpringServiceSoap12Binding" type="ns:AxisSpringServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sayHello">
<soap12:operation soapAction="urn:sayHello" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="AxisSpringServiceHttpBinding" type="ns:AxisSpringServicePortType">
<http:binding verb="POST"/>
<wsdl:operation name="sayHello">
<http:operation location="sayHello"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AxisSpringService">
<wsdl:port name="AxisSpringServiceHttpSoap11Endpoint" binding="ns:AxisSpringServiceSoap11Binding">
<soap:address location="http://localhost:8080/Axis2/services/AxisSpringService.AxisSpringServiceHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="AxisSpringServiceHttpSoap12Endpoint" binding="ns:AxisSpringServiceSoap12Binding">
<soap12:address location="http://localhost:8080/Axis2/services/AxisSpringService.AxisSpringServiceHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="AxisSpringServiceHttpEndpoint" binding="ns:AxisSpringServiceHttpBinding">
<http:address location="http://localhost:8080/Axis2/services/AxisSpringService.AxisSpringServiceHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
這裡需要額外說明,Axis2默認同時支援SOAP1.1、SOAP1.2和HTTP協議。即廠家的SOAP和REST呼叫方式。如下圖所示:
進行測試,服務端輸出結果如下:
表示兩個service被正常呼叫!!!