Java 使用Axis实现WebService实例
在上一篇WebService实例中,基于jdk1.6以上的javax.jws 发布webservice接口。这篇博文则主要用eclipse/myeclipse 使用axis插件进行发布和调用WebService。
1. 下载axis,并解压到tomcat/webapps目录下
官网地址:http://axis.apache.org/axis2/java/core/download.cgi
下载 axis2-xx-war.zip,并解压到tomcat/webapps
2. 在tomcat部署axis2
启动tomcat, 可以看到多了个axis2文件
在浏览器输入:http://localhost:8080/axis2/
看到axis界面,则成功发布
3. 在eclipse/myeclipse 安装axis插件
将下载下来的axis2-eclipse-codegen-plugin-x.x.x.zip和axis2-eclipse-service-plugin-x.x.x.zip 解压,解压之后的jar文件复制到eclipse/myeclipse 的dropins目录下,重启eclipse/myeclipse,右键File->New->Other 可以看到axis插件已经安装成功。
4. 发布WebService
将下载下来的axis2-x.x.x-bin 解压,将其中的lib架包添加置项目中。
新建class类,用于发布。
编译该类之后,用axis2发布该类。
右键New -> File -> Other -> Axis2 wizards -> Axis2 Services Archiver 。
选择该class类生成的路径,注意只到classes目录下, 然后next,勾上Skip WSDL,点击next,点击next,service填写发布的名称, class name填写路径,包名加上类名,然后选择发布的方法。继续next,选择tomcat/webapps目录下的axis/web-inf/service。
发布成功后,启动tomcat,在浏览器输入:http://localhost:8080/axis2/services/listServices 。可以看到要发布的webservice ,点击该项目,进入wsdl界面。
/***
* Title: AxisServiceHello
* Description: Axis2 发布
* Version:1.0.0
* @author panchengming*/
public class AxisServiceHello {/** 供客户端调用方法 * @param name 传入参数* @return String 返回结果* */public String getValue(String name){return "Axis 欢迎你! "+name;}
}
5. 调用WebService
新建一个class类,用于调用发布的webservice。
可以使用rpc或document两种方法调用,运行main方法,看到打印消息,调用成功。
注:调用需要将tomcat服务启动,在浏览器输入wsdl地址能够查看。
import java.io.IOException;import javax.xml.namespace.QName;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.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;/***
* Title: AxisClientHello
* Description: webService 客户端调用
* Version:1.0.0
* @author panchengming*/
public class AxisClientHello {private final static String url="http://192.168.1.105:8080/axis2/services/AxisServiceHello?wsdl"; //wsdl地址 private final static String data="PanChengMing"; //参数private final static String tns = "http://service.pcm.com"; //命名空间private final static String method="getValue"; //调用的方法//调用webservicepublic static void main(String[] args) throws IOException{getRPC(); //调用方法一getDocument(); //调用方法二}/** * 方法一: * 应用rpc的方式调用 这种方式就等于远程调用, * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。 * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService * 【注】: 如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数 第一个参数的类型是QName对象,表示要调用的方法名; 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。 第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。 如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名, 也就是 <wsdl:definitions>元素的targetNamespace属性值。 * */ @SuppressWarnings("rawtypes")public static void getRPC() throws AxisFault{ RPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();// 指定调用WebService的URLEndpointReference targetEPR = new EndpointReference(url);options.setTo(targetEPR);// 指定要调用的WSDL文件的命名空间及getValue方法QName qn = new QName(tns, method);// 指定getValue方法的参数值Object[] ob = new Object[] { data };// 指定getValue方法返回值的数据类型的Class对象Class[] classes = new Class[] { String.class };// 调用getValue方法并输出该方法的返回值System.out.println(serviceClient.invokeBlocking(qn, ob, classes)[0]);}/** * 方法二: 应用document方式调用 * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合 */ public static void getDocument() throws AxisFault{ OMElement result = null; try { Options options = new Options(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); // 命名空间OMNamespace omNs = fac.createOMNamespace(tns, ""); OMElement ot = fac.createOMElement(method, omNs); OMElement symbol = fac.createOMElement("name", omNs); symbol.addChild(fac.createOMText(symbol, data)); ot.addChild(symbol); result=sender.sendReceive(ot);System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } }}
结语:使用axis实现webservice 暂时告一段落了,这次的demo和上篇的webservice的demo 我整合成了一个项目,发布到我的github上了 ,https://github.com/xuwujing/webservice_project 。 有兴趣的可以看看。
Java 使用Axis实现WebService实例相关推荐
- Java通过axis调用WebService
转载地址:http://www.linuxidc.com/Linux/2015-06/118460.htm ------------------------------------------分割线- ...
- java:AXIS调用webService接口,返回String类型xml,并用dom4j简单解析xml
一.使用axis调用webService接口,返回String类型xml 1.导入axis依赖 2.直接贴代码 /*** 调用webservice接口的方法,并返回String类型的xml* @par ...
- java使用axis实现webservice接口调用
看了很多博客,发现都是cp出来说明也不清楚.不是缺包就是少代码,一贴就不负责任.自己最后找到了方法,写一个比较完整的方法给大家提供. 确保webservice接口可以在网页进行访问,调用webserv ...
- java使用axis调用webservice接口
1.导jar包 <!-- https://mvnrepository.com/artifact/org.apache.axis/axis --><dependency>< ...
- java .net webservice_Java客户端调用.NET的WebService实例
项目需要去调用.NET的WebSrevice,本身是Java,研究了半天,终于有些头绪,记下来. 1,新建.NET WebService.只在原方法上加上一个string类型的参数str [WebMe ...
- java调用第三方的webservice应用实例
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...
- java使用axis2调用webservice接口实例
说明:我目前的项目环境是struts2+spring+mybatis+oracle,以下代码是java使用axis2调用webservice接口实例. import javax.xml.namespa ...
- java webservice实例教程
原文:java webservice实例教程 源代码下载地址:http://www.zuidaima.com/share/1590350954564608.htm 最近在学习web services, ...
- .Net/C# 与 J2EE/Java Web Service 互操作完整实例
http://www.cnblogs.com/Microshaoft/archive/2005/08/18/217213.html .Net 与 J2EE/Java Web Service 互操作完整 ...
最新文章
- 基于Dijkstra算法的武汉地铁路径规划!(附下载)
- vsftpd的配置文件路径,是在哪里指定的?
- Warm Up before Exercise
- 【Unity】11.5 物理材质 (Physics Material)
- 注意力机制的两种形式
- ajax如何将数据写入文本框,ajax 从数据库读到文本框
- 数据库平时错误和使用经验的总结
- python selenium T3
- AI 是中性的技术,如何用它更好地为人类服务
- Android 异步获取网络图片并处理图片Out Of Memory 内存溢出问题
- DBCP数据库连接池的使用
- 项目常用工具类整理(一)--时间工具类DateUtil.java
- 数组迭代方法之reduce
- cmd命令打开文本文档_Windows常用cmd命令总结
- 报错:OPC读完成报错 索引超出数组界限
- linux电脑关机后自动重启,Linux系统关机/重启
- 跨境电商个人物品清单申报开发代码
- 一文通透优化算法:从随机梯度、随机梯度下降法到牛顿法、共轭梯度
- TP5 微信分享朋友圈接口显示自定义图片和标题
- [转]AndroidTolls国内镜像
热门文章
- OSPF3的多区域生成与链路状态通告
- ipython怎么念_如何读取IPython%prun(profiler)命令的输出? - python
- ie浏览器调用本地文件无反应_我的ie浏览器为什么打不开本地的网页文件啊?...
- 若依分离版在windows上部署(1)
- vue货币过滤器以及路由参数的使用
- 善用思维导图来整理发散的思维
- QCon演讲实录|基于 KAITIAN 的前端工程研发模式变革
- SD卡插入手机容量变小,U盘低级格式化HA-LLFTOOL
- 【RISC-V】SiFive Unmatched开发板开发手记
- JAVA有percentile函数吗_五分位算法