Spring项目跟Axis2结合

本文的前提是已经有一个Spring的项目,在此基础上如何跟Axis2进行结合,开发出WebService服务和调用WebService服务。

1.开放WebService服务

   1.引入必要的jar包

         将axis2-1.6.2-bin\axis2-1.6.2\lib所有包引入到你自己的工程中。(当然里面有些是不必要的,有兴趣的可以自己删减)。

   2.引入必要的文件,以及创建新的Service.xml

        1.将\axis2-1.6.2-war\axis2\WEB-INF中的conf目录,modules目录,copy到你工程的WEB-INF中。

        2.在WEB-INF中,新建services目录(必须),里面可以划分具体的子目录,子目录底下创建META-INF目录(必须),,目录下新建文件:services.xml(必须),

         创建完成后,目录结构如下:

       

        service.xml中的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<service name="testWebService">
    <description>testWebService</description>
    <parameter name="ServiceObjectSupplier">
        org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
    </parameter>
    <parameter name="SpringBeanName">TestWebService</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>
    <schema schemaNamespace="http://service.telchina.cn" />
</service>

   3.修改Web.xml,增加以下配置

<servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

   4.修改ApplicationContext.xml

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

PS:

  必须要加此配置,否者报错:

Caused by: java.lang.Exception: Axis2 Can‘t find Spring‘s ApplicationContext

官方描述:https://axis.apache.org/axis2/java/core/docs/spring.html

   5.创建服务类

package cn.telchina.standard.service;

import org.springframework.stereotype.Component;

@Component("TestWebService")
public class TestWebService {
    public String sayHello(String name) {
        return "hello"+name;
    }
}

 

2.调用WebService服务

调用有两种方式:

1.RPC方式

public  void testRPCClient() {
        try {
          // axis2 服务端
          String url = "http://localhost:8080/axis2Project/services/testWebService";  

          // 使用RPC方式调用WebService
          RPCServiceClient serviceClient = new RPCServiceClient();
          // 指定调用WebService的URL
          EndpointReference targetEPR = new EndpointReference(url);
          Options options = serviceClient.getOptions();
          //确定目标服务地址
          options.setTo(targetEPR);  

          /**
           * 指定要调用的getPrice方法及WSDL文件的命名空间
           * 如果 webservice 服务端由axis2编写
           * 命名空间 不一致导致的问题
           * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0
           */
          QName qname = new QName("http://service.telchina.cn", "sayHello");
          // 指定getPrice方法的参数值
          Object[] parameters = new Object[] { "name" };  

          // 指定getPrice方法返回值的数据类型的Class对象
          Class[] returnTypes = new Class[] { String.class };  

          // 调用方法一 传递参数,调用服务,获取服务返回结果集
          OMElement element = serviceClient.invokeBlocking(qname, parameters);
          //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
          //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果
          String result = element.getFirstElement().getText();
         System.out.println(result);  

//          // 调用方法二 getPrice方法并输出该方法的返回值
//          Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);
//          // String r = (String) response[0];
//          String r = (String) response[0];
//          System.out.println(r);  

        } catch (AxisFault e) {
          e.printStackTrace();
        }
      }

2.AXIOM方式

/**
       * 方法二: 应用document方式调用
       * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
       */
      public  void testDocument() {
        try {
          // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";
          String url = "http://localhost:8080/axis2Project/services/testWebService";  

          Options options = new Options();
          // 指定调用WebService的URL
          EndpointReference targetEPR = new EndpointReference(url);
          options.setTo(targetEPR);
          // options.setAction("urn:getPrice");  

          ServiceClient sender = new ServiceClient();
          sender.setOptions(options);  

          OMFactory fac = OMAbstractFactory.getOMFactory();
          // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的
          OMNamespace omNs = fac.createOMNamespace("http://service.telchina.cn", "");  

          OMElement method = fac.createOMElement("sayHello", omNs);
          OMElement symbol = fac.createOMElement("name", omNs);
          // symbol.setText("1");
          symbol.addChild(fac.createOMText(symbol, "Axis2 Echo String "));
          method.addChild(symbol);
          method.build();  

          OMElement result = sender.sendReceive(method);  

          System.out.println(result);  

        } catch (AxisFault axisFault) {
          axisFault.printStackTrace();
        }
      }

 

PS:

虽然这个例子中使用SPring的注解方式来声明了bean,但是Axis2本身不支持使用@WebService的注解直接声明服务,这个跟CXF是有本质的区别。

在浏览器中输入地址:http://localhost:8080/axis2Project/services/testWebService?wsdl

可以看到:

 

参考文章:

  1.http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html

  2.http://sunpfsj.blog.163.com/blog/static/1770500972013424113314769/

时间: 2024-07-31 11:17:49

Spring项目跟Axis2结合的相关文章

eclipse下构建maven spring项目

最近刚入职,发现公司都是使用eclipse,之前一直在学校一直使用netbeans集成开发环境,对eclipse不是太熟悉,自己也不太喜欢使用myeclipse收费的软件(虽然可以盗版激活),反应慢也是myeclipse被人诟病的原因,决定花一天时间来自己动手搭建eclipse+maven+spring. 准备工作: 1.下载eclipse(Eclipse Java EE IDE for Web Developers,Version: Juno Service Release 2). 2.下载m

spring项目整合mongodb进行开发

spring项目整合mongodb进行开发: MongoDB的性能指标: 100个并发,插入550万条记录的平均吞吐量:大约4100条/秒 MONGODB实际上是一个内存数据库,先将数据保存到内存,然后再写入磁盘中 1.官网下载mongodb. https://www.mongodb.org/downloads 2.redhat上安装好mongodb 3.

使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)

从网上看过许多打jar包的例子,大多是将配置文件打进jar包的.经过本人一番研究,终于搞清楚了怎样将jar包的配置文件外置. 废话不说,直接上spring的pom.xml的配置文件. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mav

maven搭建spring项目pom有关配置说明

<dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency> maven搭建spring项目p

关于spring项目的单例测试

这里是小知识啦,因为每次都要找以前的项目,这里记录一下,省得以后麻烦. 我们在做spring项目的时候,启动的时候spring容器肯定是要注入很多的类,这些在单例的时候比较麻烦,要启动整个项目,加载spring容器才能正确处理各个实例之间的依赖关系. 这里我们使用junit4来做单例猜测,相比于junit3的好处就是,junit4加入大量的注解功能,使得测试起来更加的方便快捷. 首先我们需要两个主要的jar包:junit4.spring-test,当然spring其他需要的包这里就不在赘述,按照

Maven构建的Spring项目需要哪些依赖?

Maven构建的Spring项目需要哪些依赖? <!-- Spring依赖 --> <!-- 1.Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.7.RELEASE</version> </dependency

spring项目启动错误——java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

最近在搭spring项目框架的时候,遇到一个很伤的问题,翻了很多帖,都报告说什么少spring-context包啊之类的,但实际上spring的那些依赖我根本没漏,下面是我的pom: <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version

spring项目后出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoade

导入别人的spring项目后出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoade错误, 解决: 1.若项目的主人是用maven创建spring项目, 解决办法: 项目 -> 属性 -> Deployment Assembly -> Add -> Java Build Path Entries -> 选择Maven Dependencies -> Finish -&

spring项目log4j使用入门

log4j是Java开发中经常使用的一个日志框架,功能强大,配置灵活,基本上可以满足项目开发中对日志功能的大部分需求.我前后经历了四五个项目,采用的日志框架都是log4j,这也反应了log4j受欢迎的程度.虽然前后接触过多次log4j,但进入的项目都是中后期,没有机会深入了解log4j.今天趁着周末,老婆出差,闲来无事,自己研究下这个日志框架在Spring框架中如何使用. 1.Spring项目中使用log4j使用 1)很显然,第一步是引入Jar包 pom文件中引入依赖的Jar包 <depende