axis2-1.6.2+spring3.1.4 发布 webservice 客户端调用总结

axis2-1.6.2+spring3.1.4 发布 webservice 客户端调用总结

   

一、下载 axis2-1.6.2

    下载地址: http://axis.apache.org/axis2/java/core/download.cgi ,自己根据情况确定下载,本人下载

二、 spring3.1.4 下载

    这个根据自己项目需要下载对应的版本,这里就不说明。

三、创建整合工程并发布测试

    3.1 、创建工程名为 axis

    3.2 、导入需要的 axis 和 spring 的 Jar

       axis 的 Jar 包 ( 相对较精简的 Jar 包,可能其他情况还需要用别的 jar, 下面的 jar足以发布 webservice ,可能还可以精简 )

客户端测试必须的 jar ,可能还需要其他包

  httpcore-4.0.jar

  commons-httpclient-3.1.jar

axis2 与 spring 整合的 jar 包:  

    axis2-spring-1.6.2.jar

    axis-xmlbeans-1.6.2.har 不知道是否有用

spring 的 Jar 包

    3.3 、编写普通 Java 类   LessonAction.java   代码如下:

        public class LessonAction {

            public int add( int a, int b){

               return a+b;

            }

        }

    3.4 、编写 spring 的配置文件 applicationContext.xml    代码如下:

< 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:aop = "http://www.springframework.org/schema/aop"

    xmlns:tx = "http://www.springframework.org/schema/tx"

    xsi:schemaLocation = "http://www.springframework.org/schema/beans

                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                         http://www.springframework.org/schema/context

                         http://www.springframework.org/schema/context/spring-context-3.0.xsd

                         http://www.springframework.org/schema/tx

                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

                         http://www.springframework.org/schema/aop

                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >

   <!-- 如果没有配置对应的 bean ,运行则报错, Caused by:org.apache.axis2.deployment.DeploymentException : The following error occurred during schema generation: No bean named 'lessonAction' is defined   由于在services.xml 需要用到这里配置的 bean -->

    < bean id = "lessonAction" class = "com.lcb.axis.service.LessonAction" > </ bean >

</ beans >

   

    3.5 、在 WEB-INF 下创建 services 文件夹,,名字一定是 services ,其他不能识别,在 services 一定要再创建一个文件夹(名字顺便,例如: MyService ),在MyService 下创建一个文件夹,名字一定是 META-INF ,在 META-INF 下创建services.xml 文件,这个名字也是不变的。最后路径是 WEB-INF/services/MyService/META-INF/services.xml services.xml 代码如下:

<!-- 访问地址中 的访问那个 webservice 的名字 -->

< service name = "LessonAction" >  

    < description > Spring aware </ description >  

<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->

    < parameter name = "ServiceObjectSupplier" >  

org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier

    </ parameter >  

    <!-- 配置在 applicationContext 中配置的 bean,, 这里的值域 bean 中的 id 事一样的,否则就报错找不到 bean 错 -->

    < parameter name = "SpringBeanName" > lessonAction </ parameter >  

    < messageReceivers >  

       <!-- 配置没有返回值的方法 lessonAction 的第一个方法 -->

        < messageReceiver mep = "http://www.w3.org/2004/08/wsdl/in-out"  

           class = "org.apache.axis2.rpc.receivers.RPCMessageReceiver" />       <!-- 配置没有返回值的方法   lessonAction 的第一个方法 ,一直下去

        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"

    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />   

         -->   

  </ messageReceivers >  

</ service >

3.6 、在 web.xml 文件添加如下代码:

   <!-- 用于初始化 spring 容器的监听器 -->

    < listener >

< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >

    </ listener >

    <!-- Spring ApplicationContext 配置文件的路径 , 可使用通配符,多个路径用 , 号分隔 . 此参数用于 Spring-Context loader -->

    < context-param >

       < param-name > contextConfigLocation </ param-name >

       < param-value > classpath*:/applicationContext*.xml </ param-value >

    </ context-param >

<!--axis2   WebService 配置信息开始 -->

  

  < 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://localhost/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://localhost/myrepo</param-value>-->

        <!--</init-param>-->

        < load-on-startup > 1 </ load-on-startup >

    </ servlet >

   

    <!-- 可要可不要开始 -->

    < servlet >

        < servlet-name > AxisAdminServlet </ servlet-name >

        < servlet-class >

            org.apache.axis2.webapp.AxisAdminServlet </ servlet-class >

    </ servlet >

    <!-- 可要可不要结束 -->

    <!-- servlet>

        <servlet-name>SOAPMonitorService</servlet-name>

        <display-name>SOAPMonitorService</display-name>

        <servlet-class>org.apache.axis2.soapmonitor.servlet.SOAPMonitorService</servlet-class>

        <init-param>

            <param-name>SOAPMonitorPort</param-name>

            <param-value>5001</param-value>

        </init-param>

        <init-param>

            <param-name>SOAPMonitorHostName</param-name>

            <param-value>localhost</param-value>

        </init-param>

         <load-on-startup>1</load-on-startup>

    </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 >

    <!-- 可要可不要结束 -->

   

<!-- axis2   WebService 配置信息结束 -->

    3.7 、工程结构

四、部署 tomcat

   4.1 、在浏览器访问: http://localhost:8088/axis/services/LessonAction?wsdl ,,显示如图,则表明发布 webservice 成功

    4.2 、客户端调用:

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;  

public class TestMain {  

    public static void main(String args[]){  

       //   使用 RPC 方式调用 WebService          

          getRPCServiceClient ();

    }

      public static void getRPCServiceClient() {

         RPCServiceClient serviceClient;

       try {

           serviceClient = new RPCServiceClient();

           Options options = serviceClient.getOptions();  

           //   指定调用 WebService 的 URL  

           EndpointReference targetEPR = new EndpointReference(  

//   是浏览器中的访问地址

"http://localhost:8088/axis/services/LessonAction?wsdl" );  

           options.setTo(targetEPR);  

            //   指定 sum 方法的参数值  

           Object[] opAddEntryArgs = new Object[] {1,2};  

           //   指定 sum 方法返回值的数据类型的 Class 对象  

           Class[] classes = new Class[] { boolean class };  

           //   指定要调用的 sum 方法及 WSDL 文件的命名空间

           // 第一个参数浏览器中看到 targetNamespace 的值targetNamespace="http://service.axis.lcb.com"   第二个参数是方法名

           QName opAddEntry = new QName( "http://service.axis.lcb.com" , "sum" );  

           //   调用 sum 方法并输出该方法的返回值  

           System. out .println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);   // 输出 3

       } catch (AxisFault e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值