///////////////////////////////////////// 读者可以自己封装数据类型,如int,byte,float等数据类型
}
package MZ.GetWebService;
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 GetWSByAxis2 {
private static String EndPointUrl;
private static String QUrl="http://ws.apache.org/axis2";
private QName opAddEntry;
public String WSUrl;
public RPCServiceClient setOption() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(WSUrl);
options.setTo(targetEPR);
return serviceClient;
}
public QName getQname(String Option){
return new QName (QUrl,Option);
}
//返回String
public String getStr(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String str = (String) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String.class })[0];
return str;
}
// 返回一维String数组
public String[] getArray(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String[].class })[0];
return strArray;
}
//从WebService中返回一个对象的实例
public Object getObject(String Option,Object o) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
QName qname=this.getQname(Option);
Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];
return object;
}
///////////////////////////////////////// 读者可以自己封装数据类型,如int,byte,float等数据类型
}
客户端调用方法:
Java代码
MZ.GetWebService.GetWSByAxis2 ws=new MZ.GetWebService.GetWSByAxis2();
ws.WSUrl="http://localhost:8989/axis2/services/HelloWorld";
HelloWorld hello= (HelloWorld)ws.getObject("getName", HelloWorld.class);
System.out.println(hello.getName("zhangjin"));
MZ.GetWebService.GetWSByAxis2 ws=new MZ.GetWebService.GetWSByAxis2();
ws.WSUrl="http://localhost:8989/axis2/services/HelloWorld";
HelloWorld hello= (HelloWorld)ws.getObject("getName", HelloWorld.class);
System.out.println(hello.getName("zhangjin"));
2.使用service.xml发布webservice,这种方式和直接放在pojo目录中的POJO类不同。要想将MyService类发布成Web Service,需要一个services.xml文件,这个文件需要放在META-INF目录中,该文件的内容如下:
Xml代码
<service name="HelloWorld">
<description>
HelloWorld webservice
</description>
<parameter name="ServiceClass">
service.HelloWorld