axis2 1.5.3学习 ApacheEclipseMyeclipseXML
程序员文章站
2022-03-02 16:56:01
...
由于工作的需要要开始接触接口,刚开始感觉很迷茫,有点不明所以,还好,在网上找了不少例子,
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]);
}
}
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]);
}
}