axis2-1.6.2+spring3.1.4发布webservice客户端调用总结
2014-10-15 14:32:10
一、下载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>
<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:8080/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:8080/axis/services/LessonAction?wsdl");
options.setTo(targetEPR);
// 指定sum方法的参数值
Object[] opAddEntryArgs = new Object[] {1,2};
// 指定sum方法返回值的数据类型的Class对象
Class[] classes = new Class[] {int.class};
// 指定要调用的sum方法及WSDL文件的命名空间
//第一个参数浏览器中看到targetNamespace的值targetNamespace="http://service.axis.lcb.com" 第二个参数是方法名
QName opAddEntry = new QName("http://service.axis.lcb.com", "add");
// 调用sum方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]); //输出3
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- axis2_spring发布webservice总结文档.zip (158.4 KB)
- 下载次数: 0
- 查看图片附件
原文链接: http://lichaobao.iteye.com/blog/2142754