Spring整合Axis2

在单独使用axis2做webservice的话,操作步骤其实挺繁琐的,可以参照笔者的axis2文章(http://kaobian.iteye.com/blog/1120446),但是我们把axis2和spring结合后,开发变的很简单,就像写action一样轻松了。好了,下面开始我们的代码演示:
需要我们加入的jar,axis2和spring,笔者的jar包加的有些多(axis2下的所有jar,spring的核心包,还有如下目录:ant、antlr、asm、jakarta-commons、log4j)

开始我们的配置
spring的配置文件,放到了web-info下

<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.Application ContextHolder" />
<bean id="test" class="me.sample.MyAddServiceImpl"></bean>


web.xml 配置如下:


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

services.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="service" targetNamespace="http://kaobian.iteye.com/" scope="application">
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
</parameter>
<parameter name="SpringBeanName">test</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>
</serviceGroup>


到此我们的配置文件结束,之后就是我们所必须要添加的axis2文件:
在web-inf下必须要有:conf/axis2.xml,modules/.... ,servcices/工程目录名称/meta-inf/services.xml

这里的所需的axis2 文件 可以在附件中下载,现在后的代码加载到工程 启动出错的话 可能原因:缺少jar,spring的配置文件 解析出错,需要注意schema约束,jar中的配置文件出错.

我们来测试一下我们的webservice:代码如下

package me.sample;

import javax.xml.namespace.QName;

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

public class MyClient {

@SuppressWarnings("unchecked")
public static void main(String[] args1) throws Exception {
String str = "{}";
// 建立一个远程连接客户端
RPCServiceClient serviceClient = new RPCServiceClient();
// 设置参数
Options options = serviceClient.getOptions();
// 设置调用的方法
options.setAction("myAdd");
// 设置webservice调用地址 本机测试就用 本地的webservice地址
// 远程webservice服务地址
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/Axis2Spring/services/service");
options.setTo(targetEPR);
// 调用远程方法的参数
Object[] opAddEntryArgs = new Object[] { 3f,1f };
// 返回结果类型
Class[] classes = new Class[] { Float.class };
serviceClient.setTargetEPR(targetEPR);
//此处的是 实现类所在的包的倒序
QName opAddEntry = new QName("http://sample.me", "myAdd");
str = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0].toString();
serviceClient.cleanupTransport();
System.out.println(Float.parseFloat(str));
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值