所有涉及到的资源代码我会打个包上传到我的资源里,想要的可以下下来参考参考!
Axis2插件安装
第一步:下载axis2-1.6的插件压缩包,axis2-eclipse-codegen-plugin-1.6.2.zip 和 axis2-eclipse-service-plugin-1.6.2.zip(我用的是老版本,新版本差别不大);
第二步:解压下载的两个压缩包,并且将解压后的org.apache.axis2.eclipse.codegen.plugin_1.6.2.jar和org.apache.axis2.eclipse.service.plugin_1.6.2.jar放到myeclipse8.6的dropins目录下;
第三步:在dropins目录下添加axis2.link文件,内容是
path=myeclipse的安装目录\dropins(例如:我的文件内容为:path=E:\Genuitec\MyEclipse-8.6\dropins);
第四步:重启myeclipse,在新建工程时,选择new–>other,然后在弹出窗口中输入axis2,如果出现axis向导,则代表安装成功.
新建web service项目
第一步、新建Web Service Project。File->New->Web Service Project。
第二步、编写主要代码
第三步、使用插件 右键点击工程 =>new=>other=> Axis2 Service Archiver
将aar包放到axis2文件夹WEB-INF/services下,
将axis2部署在tomcat或weblogic中,例如:
D:\Software\apache-tomcat-7.0.52\webapps\axis2
D:\Software\apache-tomcat-7.0.52\webapps\axis2\WEB-INF\services\Axis2WebService
以上是axis2文件夹和Axis2WebService.aar文件所在路径。
浏览器中输入地址:
服务端代码发布成功。
使用Axis2插件生成客户端
第一步、创建一个java工程
第二部、使用axis2插件生成客户端代码
右键项目工程 new=>other=>Axis2 Code Generator
点击完成,生成代码如下
报错是因为忘记导依赖包
第三步、导axis2依赖包(这是精简过的 挑不出来就全拿过来把 来自axis2-1.7.4)
调用接口的方式
一、使用Axis2插件生成客户端代码方式调用webservice
import webservice.test.GetNameResponse;
public class Demo {
/**
*
* 使用Axis2插件生成客户端代码方式调用webservice
* user chenxs
*
*/
@Test
public void fun1(){
try {
//创建客户端实体类
Axis2WebServiceStub stub = new Axis2WebServiceStub();
//创建调用方法的实体,设置参数
GetName f = new GetName();
f.setUserId("110");
//客户端调用方法,并返回xml信息
GetNameResponse name = stub.getName(f);
//返回值
System.out.println(name.get_return());
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果
二、rpc方式调用webservice
/**
*
* rpc方式调用webservice
* user chenxs
*/
@Test
public void fun2(){
try {
String url = "http://localhost:8080/axis2/services/Axis2WebService?wsdl";
ServiceClient serviceClient = new ServiceClient();
Options option = new Options();
option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
option.setTransportInProtocol(Constants.TRANSPORT_HTTP);
option.setAction("http://test.webservice/getName"); // 值为targetNamespace+methodName
EndpointReference epfs = new EndpointReference(url);
option.setTo(epfs);
serviceClient.setOptions(option);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace namespace = fac.createOMNamespace("http://test.webservice", ""); //命名空间
OMElement element = fac.createOMElement("getName", namespace); //方法名
OMElement userId = fac.createOMElement("userId ", namespace); //参数名
userId.setText("110"); //设置参数
element.addChild(userId);
OMElement result = serviceClient.sendReceive(element);
System.out.println(result);
Iterator in = result.getChildrenWithLocalName("return");
while(in.hasNext()){
OMElement om = (OMElement)in.next();
System.out.println(om);
Iterator in2 = om.getChildElements();
while(in2.hasNext()){
// System.out.println(in2.next().toString());
System.out.println(((OMElement)in2.next()).getText());
}
}
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结果