前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java axis_Java 使用Axis实现WebService实例

java axis_Java 使用Axis实现WebService实例

作者头像
全栈程序员站长
发布2022-09-13 20:57:26
6390
发布2022-09-13 20:57:26
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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

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

ae32e120bb44c208937befb33a8099aa.png
ae32e120bb44c208937befb33a8099aa.png
6dec781895652fee342254177d1e7873.png
6dec781895652fee342254177d1e7873.png

2. 在tomcat部署axis2

启动tomcat, 可以看到多了个axis2文件

在浏览器输入:http://localhost:8080/axis2/

看到axis界面,则成功发布

1d770a0fef928b5324bc1395b5452801.png
1d770a0fef928b5324bc1395b5452801.png
b0f55f22c9b0f0ce8c309118089f57d9.png
b0f55f22c9b0f0ce8c309118089f57d9.png

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插件已经安装成功。

a1f16f779965279646f63b6ffc515a82.png
a1f16f779965279646f63b6ffc515a82.png
240444e785cd5d3a80cec09353de4fe5.png
240444e785cd5d3a80cec09353de4fe5.png
af9d149820076fd313870591d2e57958.png
af9d149820076fd313870591d2e57958.png

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;

}

}

1e59b8acbe0d00e8ab2efc0d74cdc961.png
1e59b8acbe0d00e8ab2efc0d74cdc961.png
6f8467166aec9bd12a9a0ab8203346c3.png
6f8467166aec9bd12a9a0ab8203346c3.png
9bf083d1bad4ad4e28bff1e95712ec14.png
9bf083d1bad4ad4e28bff1e95712ec14.png
64bc310a93d84ee63b3f1c472e10916b.png
64bc310a93d84ee63b3f1c472e10916b.png

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();

}

}

}

204c7800ca68a2dae36ab99e707322a8.png
204c7800ca68a2dae36ab99e707322a8.png

结语:使用axis实现webservice 暂时告一段落了,这次的demo和上篇的webservice的demo 我整合成了一个项目,发布到我的github上了 ,https://github.com/xuwujing/webservice_project 。 有兴趣的可以看看。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162651.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档