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 代码如下:
publicclass LessonAction {
publicint 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>
<!-- 手动为axis2使用的bean配置事务开始 -->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="lessonActionTarget" class=" com.lcb.axis.service.LessonAction "></bean>
<bean id="ariesService" parent="transactionBase" >
<property name="target" ref=" lessonActionTarget " />
</bean>
<!--<bean id="lessonAction" class="com.lcb.axis.service.LessonAction"> </bean> -->
</beans>
<!--EndFragment-->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>
<!-- 如果 aries.common.webservice.AriesService在spring中配置了事务,必须加上下面这句,否则报错
aries.common.webservice.AriesService本人还无法实现注解方法配置事务-->
<parameter name="ServiceClass"> aries.common.webservice.AriesService(包名+接口名)</parameter>
</service>
<!--EndFragment-->
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、工程结构
3.8、axis管理session
1、把sessionId存在ServiceContext
MessageContext context = MessageContext.getCurrentMessageContext();
ServiceContext serviceContext = context.getServiceContext();
HttpServletRequest request = null;
HttpSession session = null;
if(context!=null){
request = (HttpServletRequest) context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
}
serviceContext.setProperty(session.getId(), session.getId());//把sessionId存在serviceContext中
2、从ServiceContext 获取sessionId
MessageContext context = MessageContext.getCurrentMessageContext();
ServiceContext serviceContext = context.getServiceContext();
serviceContext.setProperty(session.getId())//键就是上面存的
<!--EndFragment-->
四、部署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;
publicclass TestMain {
publicstaticvoid main(String args[]){
// 使用RPC方式调用WebService
getRPCServiceClient();
}
publicstaticvoid getRPCServiceClient() {
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
//当我们使用sessionID进行判断,这个对象在每个客户端都是唯一的,否则sessionId是不存在的,对象不同相当于两个客户端调用
<!--EndFragment-->Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
// 是浏览器中的访问地址
"http://localhost:8088/axis/services/LessonAction?wsdl");
options.setTo(targetEPR);
// 指定add方法的参数值
Object[] opAddEntryArgs = new Object[] {1,2};
// 指定add方法返回值的数据类型的Class对象
Class[] classes = new Class[] {boolean.class};
// 指定要调用的add方法及WSDL文件的命名空间
//第一个参数浏览器中看到targetNamespace的值targetNamespace="http://service.axis.lcb.com" 第二个参数是方法名
QName opAddEntry = new QName("http://service.axis.lcb.com", "add");
// 调用add方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]); //输出3
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
五、总结
如果有错或者有更好的方法,麻烦告知,谢谢!
相关推荐
Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService ...Axis用Spring的JavaBean发布WebService Axis异步调用WebService Axis 的Module模块 Axis使用SoapMonitar监视WebService的请求和响应信息
Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService ...Axis用Spring的JavaBean发布WebService Axis异步调用WebService Axis 的Module模块 Axis使用SoapMonitar监视WebService的请求和响应信息
Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService ...Axis用Spring的JavaBean发布WebService Axis异步调用WebService Axis的Module模块 Axis使用SoapMonitar监视WebService的请求和响应信息
Axis的简单准备 Axis的入门实例 Axis复杂对象类型的WebService ... Axis用Spring的JavaBean发布WebService Axis异步调用WebService Axis 的Module模块 Axis使用SoapMonitor监视WebService的请求和响应信息
WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService ................................... 26 WebService大讲堂之Axis2(8):异步调用WebService ..................................................
WebService大讲堂之Axis2系列教程 1. 用POJO实现0配置的WebService 2. 复合类型数据的传递 3. 使用services.xml文件发布WebService 4. 二进制文件传输 5. 会话(Session)管理 6. 跨服务会话(Session)管理 ...
1):用POJO实现0配置的WebService 2):复合类型数据的传递 ...7):将Spring的装配JavaBean发布成 8):异步调用WebService 9):编写Axis2模块(Module) 10):使用soapmonitor模块监视soap请求与响应消息
1. 用POJO实现0配置的WebService 2. 复合类型数据的传递 ... 将Spring的装配JavaBean发布成WebService 8. 异步调用WebService 9. 编写Axis2模块(Module) 10. 使用soapmonitor模块监视soap请求与响应消息
2. Axis2与Eclipse的整合 7 2.1. Axis2下载,并部署axis2到Tomcat 7 2.2. Eclipse下安装Axis2插件 8 2.2.1. 下载axis2的eclipse插件 8 3. 编写Web service程序 10 3.1. Axis2的简单WebService示例 10 3.1.1. 0配置...
文档1 pojo实现0配置 文档2 符合数据类型 文档3 使用services.xml发布webservice ...文档7 spring的bean发布为webservice 文档8 异步调用webservice 文档9 编写axis模块 文档10 使用moniter监视器
axis2入门学习资料,通过10个步骤详细例子(测试都能运行),简单阐述了ajax通过tomcat发布方法,session管理,异步调用和同步调用,spring整合等功能,说明,配置比较详细