文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Axis2集成Spring (POJO)

Axis2集成Spring (POJO)

时间:2010-08-31  来源:btpka3

以前用过Axis2的,但是想再次用的时候居然还得花大半天时间,哭了都,老老实实做个笔记吧。     本示例是在 Axis2 通过 外部 Spring容器 获取指定的实现Bean,以提供服务。全部自己写WSDL和服务实现类,不使用 Axis2提供的 Java2WSDL 和 WSDL2Java 工具(生成的太丑,太臃肿)。    
文件: SampleAxis2SpringPojoWebService.zip
大小: 13KB
下载: 下载

压缩包中省略了 axis2-web中所有的JSP,和Axis2、Spring及其所以来的jar包,请自行追加。

README.txt

 

Axis2 工程一般的目录结构

SampleWebApp
  ┣ *
  ┣ META-INF/*
  ┗ WEB-INF/
       ┣ classes/*
       ┣ lib/*
       ┣ conf
       ┃ ┗ axis2.xml
       ┣ modules/
       ┃ ┣ modules.list
       ┃ ┗ *.mar
       ┗ services
            ┣ services.list
            ┣ *.aar
            ┗ sampleWebService (sampleWebService.aar)
                 ┣ *
                 ┗ META-INF
                      ┣ services.wsdl
                      ┗ service.xml

----------------------------------------- axis2.xml
axis2.xsd
(axis2-1.4.1-src.zip\axis2-1.4.1\modules\kernel\resources\)
----------------------------------------- service.xml
services.xsd
(axis2-1.4.1-src.zip\axis2-1.4.1\modules\kernel\resources\)
如果你使用Axis2在 0.91 版本以上,你可以不使用 ARR 压缩文件,而直接创建一个相应的目录结构即可。
----------------------------------------- services.wsdl
该文件的主文件名可以任意
注意:<wsdl:service/> 的 name 属性的值 必须为 services.wsdl 父目录(META-INF)的父目录名(sampleWebService)



示例工程 SampleAxis2SpringPojoWebService 运行步骤


拷贝以下Jar文件到 SampleAxis2SpringPojoWebService/WebContent/WEB-INF/lib 目录下:
    axis2-1.5.1-bin.zip/axis2-1.5.1/lib/*.jar
    spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/dist/spring.jar
    spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/dist/modules/*.jar
    spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/lib/*/*.jar
拷贝以下Jar文件到 SampleAxis2SpringPojoWebService/WebContent/axis2-web/ 目录下:
    axis2-1.5.1-bin.zip/axis2-1.5.1/webapp/axis2-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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SampleAxis2SpringPojoWebService</display-name>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
    <!--<init-param> -->
    <!--<param-name>axis2.xml.path</param-name> -->
    <!--<param-value>/WEB-INF/conf/axis2.xml</param-value> -->
    <!--<param-name>axis2.xml.url</param-name> -->
    <!--<param-value>http://localhot/myrepo/axis2.xml</param-value> -->
    <!--<param-name>axis2.repository.path</param-name> -->
    <!--<param-value>/WEB-INF</param-value> -->
    <!--<param-name>axis2.repository.url</param-name> -->
    <!--<param-value>http://localhot/myrepo</param-value> -->
    <!--</init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
    <servlet-name>AxisAdminServlet</servlet-name>
    <servlet-class>org.apache.axis2.transport.http.AxisAdminServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisAdminServlet</servlet-name>
    <url-pattern>/axis2-admin/*</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

</web-app>


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="applicationContext"
    class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />

  <bean id="myAddServiceBean" class="me.sample.MyAddServiceImpl">
  </bean>
</beans>


services.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
该XSD文件可以在axis2-1.4.1-src.zip文件的以下目录下找到:
    axis2-1.4.1\modules\kernel\resources\
javadoc:
     org.apache.axis2.description.AxisService
     index-all.html

Contract First Development with Axis2 (Part II)
http://wso2.org/library/2935

Tutorial: Contract-First Web Services with Apache Axis2
http://www.roseindia.net/software-tutorials/detail/17781
<!DOCTYPE service SYSTEM "services.xsd">
-->

<service name="myAddService">
  <description>this is a Sample by btpka3.com.</description>

  <parameter name="ServiceObjectSupplier">
    org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
  </parameter>
  
  <!--
  <parameter name="ServiceObjectSupplier">
    org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
  </parameter>
  -->
  
  <parameter name="SpringBeanName">myAddServiceBean</parameter>
  <parameter name="ServiceClass">me.sample.MyAddService</parameter>

  <parameter name="useOriginalwsdl">true</parameter>
  <parameter name="modifyUserWSDLPortAddress">true</parameter>
  
  <messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
      class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
      class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  </messageReceivers>
</service>


services.wsdl

 

<?xml version="1.0" encoding="UTF-8" ?>
<!-- name="myAddWebService" standalone="no"-->
<wsdl:definitions
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://me.test"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    targetNamespace="http://me.test">
    
    <wsdl:documentation>myAdddfdiiiioooooooddee</wsdl:documentation>
    <wsdl:types>
      <xsd:schema targetNamespace="http://me.test">
        <xsd:element name="myAdd">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="param0" type="xsd:float" />
              <xsd:element name="param1" type="xsd:float" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="myAddResponse">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="return" type="xsd:float"/>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:schema>
    </wsdl:types>
    <wsdl:message name="myAddRequest">
        <wsdl:part name="parameters" element="tns:myAdd"/>
    </wsdl:message>
    <wsdl:message name="myAddResponse">
        <wsdl:part name="parameters" element="tns:myAddResponse"/>
    </wsdl:message>
    <wsdl:portType name="myAddPortType">
        <wsdl:operation name="myAdd">
            <wsdl:input message="tns:myAddRequest" />
            <wsdl:output message="tns:myAddResponse" />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="myAddSOAP11Binding" type="tns:myAddPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="myAdd">
            <soap:operation soapAction="urn:myAdd"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="myAddService">
        <wsdl:port name="myAddPort" binding="tns:myAddSOAP11Binding">
            <soap:address location="http://dfdfdf.com"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>


MyAddService.java

package me.sample;

/**
 * 一个示例用Web Service的接口。该接口中的方法与WSDL上公布的方法原型一致。
 *
 * @author [email protected]
 *
 */
public interface MyAddService {

    /**
     * 用于计算两个数的和。
     *
     * @param num1
     * 数值1
     * @param num2
     * 数值2
     * @return <code>num1</code>与<code>num2</code>的和
     */
    float myAdd(float num1, float num2);
}


MyAddServiceImpl.java

package me.sample;

/**
 * 这是Web Service接口的实现类,可以额外拥有其他一些不公开的方法。
 *
 * @author [email protected]
 *
 */
public class MyAddServiceImpl implements MyAddService {

    /**
     * {@inheritDoc}
     */
    @Override
    public float myAdd(float num1, float num2) {
        System.out.println("INVOKED : num1="+num1+", num2="+num2);
        return num1 + num2;
    }
}


MyClient.java

package me.sample;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class MyClient {
    
    public static void main(String[] args1) throws AxisFault {

        // Step 1. 创建 Client

        RPCServiceClient serviceClient = new RPCServiceClient();

        // Step 2. 访问设定(路径等)

        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/SampleAxis2SpringPojoWebService/services/myAddService");
        options.setTo(targetEPR);

        // Step 3.1. 准备方法调用(设定方法名)

        QName opGetWeather = new QName("http://me.test", "myAdd");

        // Step 3.2. 准备方法调用(设定参数)

        float num1 = 3;
        float num2 = 2;
        Object[] opGetWeatherArgs = new Object[] { num1, num2 };

        // Step 3.3. 准备方法调用(设定返回值类型)

        @SuppressWarnings("unchecked")
        Class[] returnTypes = new Class[] { Float.class };

        
        // Step 4. 执行方法调用

        Object[] response = serviceClient.invokeBlocking(opGetWeather,
                opGetWeatherArgs, returnTypes);

        // Step 5. 取的返回值

        Float result = (Float) response[0];

        // Step 6. 检查结果并输出

        if (result == null) {
            System.out.println("result is null!");
        } else {
            System.out.println("result = " + num1 + " + " + num2 + " = "
                    + result);
        }
    }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载