axis2 1.5.3学习

由于工作的需要要开始接触接口,刚开始感觉很迷茫,有点不明所以,还好,在网上找了不少例子,
axis2 1.5以上的版本都会自带了了一个axis2的war包,刚开始我什么都没有做,只不过把这个war包发布到了tomact下,然后启动tomact。然后就会看到了war中自带的接口,然后根据网上说的,只需要在axis2的文件夹下的webinf下建一个pojo的文件夹,里面只不过是放了一个类的.class,这个类里有有两个public的方法,然后把这个类的.clss文件拷贝到pojo的文件下,然后重启tomact,就会发现多了两个服务,然后就要写客户端了,因为不知道那些jar包是用到或者用不到的,就直接把axis2 bin下的lib文件夹下的所有的jar都加入到项目中,然后有安装了那两个eclipse的插件,从官网上下载1.4版本的就行,解压后,放到myeclipse中eclipse中的plugis下就行,不用重启,然后新建项目的过程一样,但是要选择other就会发现那两个插件,然后选择Axis2 code gennerator ,然后进入一个选择页面,选择第一个,而后输入http://localhost:8080/axis2/services/Testservicess?wsdl,地址,或者直接就是http://localhost:8080/axis2/services+类名+?wsdl就可以了。然后就会找到你的接口,选择custom,自己只需要改一下包名就可以了了,然后下一步,然后选择第一个,然后选择你要把这接口加在那个项目上,然后就完成了。配置完成,然后就要写个客户端了。新建一个类package axis2;


import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import axis2.TestservicessStub;
import axis2.TestservicessStub.GetGreeting;
import axis2.TestservicessStub.GetPriceResponse;


public class TestClent {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;

public TestClent(String endpoint) throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}

public TestClent() {

}

public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
QName opQName = new QName(targetNamespace, opName);
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
************************************************************************以上方法不要动。
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public String getResult(String name) throws RemoteException {
TestservicessStub stub = new TestservicessStub();
TestservicessStub.GetGreeting request = new TestservicessStub.GetGreeting();
request.setPass(name);
return stub.getGreeting(request).get_return();
}

public int Testgrice() throws RemoteException{
TestservicessStub stub = new TestservicessStub();
return stub.getPrice().get_return();

}
public static void main(String[] args) throws AxisFault,
ClassNotFoundException {

//
try {

System.out.println(new TestClent().getResult("zhangsan"));
System.out.println(new TestservicessStub().getPrice().get_return());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
还有一个就是第二种的客户端,是从别的地方转过来的。
package axis2;


import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class WsClient {

private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;

public WsClient(String endpoint) throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}

public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
QName opQName = new QName(targetNamespace, opName);
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
********************************************************************************************************************************************
以上不用改动。
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws AxisFault,
ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference = "http://localhost/axis2/services/Testservicess";
final String targetNamespace = "http://ws.apache.org/axis2";
WsClient client = new WsClient(endPointReference);

String opName = "getGreeting";
Object[] opArgs = new Object[] { "123" };
Class<?>[] opReturnType = new Class[] { String[].class };

Object[] response = client.invokeOp(targetNamespace, opName, opArgs,
opReturnType);
System.out.println(((String[]) response[0])[0]);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值