一个简单的AXIS远程调用Web Service示例
我们通常都将编写好的Web Service发布在Tomcat或者其他应用服务器上,然后通过浏览器调用该Web Service,返回规范的XML文件。但是如果我们不通过浏览器调用,而是通过客户端程序调用,该如何实现?
接下来,我们利用Eclipse作为开发工具,演示一个Axis调用WebService的简单示例。步骤如下:
第一步:新建Web Project (一定要是web project,不能是java project)。
第二步:导入AXIS类库。(官方下载:http://apache.etoak.com//axis/axis2/java/core/1.5.4/axis2-1.5.4-bin.zip)(即把下载包里../lib/目录下的jar文件拷贝到工程的classpath下。 ) ,使用maven的话,此步忽略。
第三步:新建一个简单的连接字符串的类HelloWorld.java:
- package com.mzh.webservice;
- public class HelloWorld {
- public String connectStr(String str1,String str2,int flag){
- String resultStr="no str";
- if(flag==1){
- resultStr=str1+"---"+str2;
- }else if(flag==2){
- resultStr=str2+"---"+str1;
- }
- System.out.println(resultStr);
- return resultStr;
- }
- }
第四步:右击HelloWorld.java---Web Services---Create Web service.依次点击下一步……finish。此时发现WebContent目录下生成一个wsdl目录,目录下有一个HelloWorld.wsdl
第五步:测试web service :右击HelloWorld.wsdl---Web Services---Test with Web Services Explorer---点击方法名connectStr----输入参数mzh,zyd,1---点go.此时会发现控制台输出mzh---zyd,说明测试成功。WebService没有问题。
第六步:编写客户端调用类,利用AXIS远程调用HelloWorld.(为了体现远程调用,把此类放到另外一个工程里去)代码如下:
- import java.rmi.RemoteException;
- import javax.xml.rpc.ServiceException;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class HelloWorldTest {
- public String invokeRemoteFuc(){
- String endpoint=
- "http://192.168.1.236:8080/Axis2/services/HelloWorld";
- String result ="no result!";
- Service service = new Service();
- Call call;
- try {
- call=(Call)service.createCall();
- call.setTargetEndpointAddress(endpoint);//远程调用路径
- call.setOperationName("connectStr");//调用的方法名
- //设置参数名:
- call.addParameter("str1", //参数名
- org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型:String
- javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'
- call.addParameter("str2", //参数名
- org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型:String
- javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'
- call.addParameter("flag", //参数名
- org.apache.axis.encoding.XMLType.XSD_INT,//参数类型:INT
- javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'
- //设置返回值类型:
- call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//返回值类型:String
- String str1="mzh";
- String str2="zyd";
- int flag=1;
- result = (String)call.invoke(new Object[]{str1,str2,flag});//远程调用
- } catch (ServiceException e) {
- e.printStackTrace();
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- return result;
- }
- //测试:
- public static void main(String[] args){
- HelloWorldTest test = new HelloWorldTest();
- String result = test.invokeRemoteFuc();
- System.out.println(result);
- }
- }
(注意:1、设置方法名和参数名的时候,必须与被调用的WebService保持一致;2、传递的参数需封装到了一个Object数组里)
第七步:测试:HelloWorldTest ---Run as---Java application,如果发现控制台正确输出 mzh---zyd.说明测试成功。
第八步:至此,大功告成!
以下为pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WS_AXIS_TEST</groupId>
<artifactId>WS_AXIS_TEST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.axis/axis-jaxrpc -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/wsdl4j.wso2/wsdl4j -->
<!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency> </dependencies>
</project>
一个简单的AXIS远程调用Web Service示例的更多相关文章
- C#开发和调用Web Service
http://blog.csdn.net/h0322/article/details/4776819 1.1.Web Service基本概念 Web Service也叫XML Web Service ...
- php5调用web service
工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经 ...
- ASP.NET调用Web Service
1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- php5调用web service (笔者测试成功)
转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...
- C#之VS2010ASP.NET页面调用Web Service和winform程序调用Web Service
一:用ASP.NET调用Web Service 打开VS2010,打开“文件-新建-网站”,选择“ASP.NET网站” 选好存储位置,语言后点击确定,进入默认页面.然后先添加Web引用,把WebSer ...
- C# 调用 Web Service
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...
- 使用wsimport和JAX-WS调用Web Service接口
本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口.此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库. JDK版本:1.8.0_141开发 ...
- 工作笔记 | Visual Studio 调用 Web Service
引言 最近笔者负责ERP财务系统跟中粮集团财务公司的财务系统做对接,鉴于ERP系统中应付结算单结算量比较大,而且管理相对集中,ERP系统与中粮财务公司的支付平台系统对接,实现银企直联,将网银录入的环节 ...
- 使用Android应用调用Web Service
Java本身提供了丰富的Web Service支持,比如Sun公司指定的JAX-WS 2规范,还有Apache开源组织所提供的Axis1.Axis2.CXF等,这些技术不仅可以用于非常方便地对外提 ...
随机推荐
- sqlserver数据库脱机时发生异常:由于无法在数据库 'SMS' 上放置锁,ALTER DATABASE 失败。请稍后再试。 ALTER DATABASE 语句失败。 (.Net SqlClient Data Provider)
sqlserver数据库脱机时发生异常,如下: =================================== 设置脱机 对于 数据库“SMS”失败. (Microsoft.SqlServe ...
- Web目录结构
/: Web应用的跟目录,该目录下所有文件在客户端都可以访问(JSP,HTML) /WEB-INF: 存放应用使用的各种资源.该目录及其子目录对客户端都是不可以访问的, 其中包括web.xml(部署表 ...
- 测试中认识 sqlite
1.SQLite,是一款轻型的数据库:简单, 轻松的API 单词速记中单词离线包也用到sqlite 百度了一下,基本的使用语句: .help .quit sqlite3 testDB.db 在当前目录 ...
- VS2017 Product Key
Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
- (一)mvc与mvvm设计模式
前沿:了解设计模式对我们而言,具有很大意义,对语言没有限制,它适用于任何语言,是一种变成思想.设计模式最初有四人帮提出,有兴趣的同学可以去了解下,今天给大家主要分析mvc与mvvm设计模式 一.mvc ...
- freemarket使用自定义标签 注解【项目实际使用】
页面达到效果 [主html页面代码] <!-- 合作单位 版块 -->[#include 'inc_project_succ_coo.html'/]['inc_project_succ_c ...
- postman安装Postman Interceptor 插件
做后端开发避免不了进行接口调试,但是一般的项目都是前后端分离的,如果把前端代码下到本地,较为费事,这个时候就需要一个可以进行接口调试的工具.Postman就是一个不错的选择. Postman是什么? ...
- FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 怎么办?
FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 怎么办? 有小伙伴提问 FastAdmin 在 CRUD 时出现 ...
- [转]HTTP 协议中的 Transfer-Encoding
本文作为我的博客「HTTP 相关」专题新的一篇,主要讨论 HTTP 协议中的 Transfer-Encoding.这个专题我会根据自己的理解,以尽量通俗的讲述,结合代码示例和实际场景来说明问题,欢迎大 ...
- asp+jquery+ajax,asp后台程序执行不正常
项目中前台页面通过jquery .ajax功能将关键数据传递到后台并写入数据库,调试中发现后台程序一直没有正常执行,后反复排查 发现asp程序中不能包含#include file语句