在Web應用中嵌入Axis2 Web Service
有一個最簡單的方法就是把axis2.war中的內容作為web app的基礎, 來進行開發. 不過為了更清楚的瞭解如何在一個已有的web app中嵌入axis2, 那就只能手動了.
1. 把以下內容copy到已有的web app中
- axis2.war/axis2-web
- axis2.war/WEB-INF/*
2. 在web.xml中配置axis2 servlet
<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>
3. 建立自己的Service Bean
package example;
public class HelloWorldService {
public HelloWorldService() {
}
public String hello() {
return "Hello axis2 service!";
}
}
4. 配置Web Service.
由於axis2嵌入了web app, 所以web service就不用打包成aar,而是直接在/WEB-INF目錄下建立相應的檔案夾和services.xml
/WEB-INF
/services
/HelloWorld
/META-INF
services.xml
5. 配置services.xml的內容
<serviceGroup>
<service name="HelloWorldService">
<description>Hello World Service</description>
<parameter name="ServiceClass" locked="false">example.HelloWorldService</parameter>
<operation name="hello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
</serviceGroup>
6. 測試
如果一切OK的話, 在瀏覽器中輸入http://localhost:8080/example/services/HelloWorldService?wsdl就應該可以看到輸出的wsdl xml.
Done!
其實在項目中還整合了Spring, 不過這個應該相對簡單.
1. 需要把Spring的jar放到/WEB-INF/lib中
2. services.xml中service的ServiceObjectSupplier用org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
HelloWorldSpringService.java
package example;
public class HelloWorldSpringService {
public String hello() {
return "Hello spring axis2 service!";
}
}
In services.xml:
<serviceGroup>
<service name="HelloWorldService">
<description>Hello World Service</description>
<parameter name="ServiceClass" locked="false">example.HelloWorldService</parameter>
<operation name="hello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<service name="HelloWorldSpringService">
<description>Hello World Spring Service</description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<parameter name="SpringBeanName">HelloWorldSpringService</parameter>
<operation name="hello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
</serviceGroup>
In applicationContext.xml
<bean id="HelloWorldSpringService" class="example.HelloWorldSpringService"/>
參考:
- How to Embed an Axis2 based Web Service in your Webapp? | WSO2 Oxygen Tank
wso2.org/library/90
- Axis2 Integration with the Spring Framework
http://ws.apache.org/axis2/1_3/spring.html