Spring 整合 Axis2 web service

Posted by Bruce Tsai

專案結構

project
|   pom.xml
|   
+---src
    \---main
        +---java
        |   \---com
        |       \---fet
        |           \---crm
        |               \---sample
        |                   \---web
        |                       +---common
        |                       |   +---controller
        |                       |   |   |   IHomeController.java
        |                       |   |   |   
        |                       |   |   \---impl
        |                       |   |           HomeController.java
        |                       |   |           
        |                       |   \---facade
        |                       |       |   IHomeFacade.java
        |                       |       |   
        |                       |       \---impl
        |                       |               HomeFacade.java
        |                       |               
        |                       +---service
        |                       |   |   IHelloWorldService.java
        |                       |   |   
        |                       |   \---impl
        |                       |           HelloWorldService.java
        |                       |           
        |                       \---supporter
        |                               GenericControllerSupporter.java
        |                                       
        +---resources
        |   \---spring
        |           application-context.xml
        |           
        \---webapp
            \---WEB-INF
                |   web.xml
                |   
                +---pages
                |   \---home
                |           index.jsp
                |           
                \---services
                    \---HelloWorldService
                        \---META-INF
                                services.xml

依賴元件

<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2</artifactId>
    <version>1.6.2</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-http</artifactId>
    <version>1.6.2</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-local</artifactId>
    <version>1.6.2</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.4.0</version>
</dependency>

<!-- 整合 Spring -->
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-spring</artifactId>
    <version>1.6.2</version>
</dependency>

定義 web service 介面

package com.foo.sample.web.service;

import java.util.List;

import com.foo.sample.common.model.Category;

public interface IHelloWorldService {

    String say();

    List<Category> getCategories();

}

實作 web service 內容

為了與 Spring 完整整合,web service 實作需繼承 SpringBeanAutowiringSupport 以便能透過 @Autowired 進行注入。

package com.foo.sample.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.foo.sample.common.dao.ICategoryRepository;
import com.foo.sample.common.model.Category;
import com.foo.sample.web.service.IHelloWorldService;

/**
 * Axis2 web service 整合 spring
 * 
 * @author nanashi07
 *
 */
public class HelloWorldService extends SpringBeanAutowiringSupport implements IHelloWorldService {

    @Autowired
    ApplicationContext context;

    @Autowired
    ICategoryRepository categoryRepository;

    @Override
    public String say() {
        return "hello";
    }

    @Override
    public List<Category> getCategories() {
        return categoryRepository.getCategories();
    }

}

配置 web service 設定檔

設定檔的位置需位於 WEB-INF/services/webservice/META-INF/service.xml

  • service.xml
<serviceGroup>
    <service name="HelloWorldService" targetNamespace="http://webservice.MavenAxis2WebService/">
        <description>JNLPGenerator service</description>
        <schema schemaNamespace="http://webservice.MavenAxis2WebService/" />

        <parameter name="ServiceClass" locked="false">com.foo.sample.web.service.impl.HelloWorldService
        </parameter>

        <operation name="say">
            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        </operation>
        <operation name="getCategories">
            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        </operation>

    </service>
</serviceGroup>

整合 Spring 設定檔

  • application-context.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"
    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">

    <!-- 整合 axis web service bean -->
    <bean
        class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />

    <!-- web service bean -->
    <bean class="com.foo.sample.web.service.impl.HelloWorldService" />

</beans>

Web 配置檔

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    ...

    <!-- axis2 web service -->
    <servlet>
        <servlet-name>Axis2Servlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Axis2Servlet</servlet-name>
        <url-pattern>/axis2/*</url-pattern>
    </servlet-mapping>

    <!-- spring 設定檔 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-base.xml</param-value>
    </context-param>

    ...

</web-app>

查看結果

results matching ""

    No results matching ""