在Web應用中嵌入Axis2 Web Service

來源:互聯網
上載者:User
在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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.