博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 使用Axis实现WebService实例
阅读量:7199 次
发布时间:2019-06-29

本文共 5108 字,大约阅读时间需要 17 分钟。

在中,基于jdk1.6以上的javax.jws 发布webservice接口。这篇博文则主要用eclipse/myeclipse 使用axis插件进行发布和调用WebService。

1. 下载axis,并解压到tomcat/webapps目录下

官网地址:

下载 axis2-xx-war.zip,并解压到tomcat/webapps

这里写图片描述

这里写图片描述

2. 在tomcat部署axis2

启动tomcat, 可以看到多了个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,在浏览器输入: 。可以看到要发布的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"; //调用的方法 //调用webservice public 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文件的命名空间名, 也就是
元素的targetNamespace属性值。 * */ @SuppressWarnings("rawtypes") public static void getRPC() throws AxisFault{ RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定调用WebService的URL EndpointReference 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和上篇的的demo 我整合成了一个项目,发布到我的github上了 , 。 有兴趣的可以看看。

转载于:https://www.cnblogs.com/xuwujing/p/7536700.html

你可能感兴趣的文章
tomcat服务器重启后session可以继续使用
查看>>
SQL Server 全文索引介绍(转载)
查看>>
根据key删除Map集合中的key-value映射
查看>>
支持并发的httpclient(基于tcp连接池以及netty)
查看>>
Vuex的入门教程
查看>>
微服务架构实战
查看>>
语音信号处理之(三)矢量量化(Vector Quantization)
查看>>
hdu 4666 Hyperspace
查看>>
linux中shell变量$#,$@,$0,$1,$2的含义解释
查看>>
多态时最好将基类的析构函数设为virtual、 C++中两个类相互包含引用问题 (转载)...
查看>>
JS动态生成的元素,其对应的方法不响应(比如单击事件,鼠标移动事件等)...
查看>>
iOS - Swift PList 数据存储
查看>>
[转载]Windows 2003 R2 SP2 VOL 企业版(简体中文)
查看>>
java web 分页实现
查看>>
谈谈区块链的理解 -- 读《区块链:技术驱动金融》
查看>>
模板类声明和定义 (转)
查看>>
RSync 远程同步工具的使用
查看>>
C++访问mysql数据库
查看>>
字符测试与映射函数 ctype.h
查看>>
关于Spring的69个面试问答——终极列表
查看>>