快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

亚虎国际老虎机网址_酒文化网进入



三、应用axis1.4调用webservice措施

条件前提:下载axis1.4包和tomcat办事器,并将axis文件夹复制到tomcat办事器的webapp文件夹中

这里我就说一下最简单的措施:

首先建立一个随意率性的java类(例如:HelloWorld.java),复制到axis文件夹下,将其扩展名改为jws,然后从新启动tomcat,在浏览器中输入http://localhost:8989/axis/HelloWorld.jws?wsdl,就会获得一个wsdl文件,其客户端调用措施如下:

Java代码

import javax.xml.rpc.Service;

import javax.xml.rpc.ServiceException;

import javax.xml.rpc.ServiceFactory;

import java.net.MalformedURLException;

import java.net.URL;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

public class 亚虎国际老虎机网址TestHelloWorld {

public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {

// TODO Auto-generated method stub

String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws?wsdl";

String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws";

String serviceName = "HelloWorldService";

String portName = "HelloWorld";

ServiceFactory serviceFactory = ServiceFactory.newInstance();

Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));

HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);

System.out.println("return value is "+proxy.getName("john") ) ;

}

}

import javax.xml.rpc.Service;

import javax.xml.rpc.ServiceException;

import javax.xml.rpc.ServiceFactory;

import java.net.MalformedURLException;

import java.net.URL;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

public class TestHelloWorld {

public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {

// TODO Auto-generated method stub

String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws?wsdl";

String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws";

String serviceName = "HelloWorldService";

String portName = "HelloWorld";

ServiceFactory serviceFactory = ServiceFactory.newInstance();

Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));

HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);

System.out.println("return value is "+proxy.getName("john") ) ;

}

}

四、应用axis2开拓webservice(这里首先谢谢李宁师长教师)

应用axis2 必要先下载

axis2-1.4.1-bin.zip

axis2-1.4.1-war.zip

http://ws.apache.org/axis2/

同理,也必要将axis2复制到webapp目录中

在axis2中支配webservice有两种措施,

第一种是pojo要领,这种要领对照简单,然则有一些限定,例如支配的类不能加上包名

第二种要领是使用xml宣布webservice,这种措施对照机动,不必要限定类的声明

下面分手阐明应用措施:

1.pojo要领:在Axis2中不必要进行任何的设置设置设备摆设摆设,就可以直接将一个简单的POJO宣布成WebService。此中POJO中所有的public措施将被宣布成WebService措施。先实现一个pojo类:

Java代码

public class HelloWorld{

public String getName(String name)

{

return "你好 " + name;

}

public int add(int a,int b)

{

return a+b;

}

}

public class HelloWorld{

public String getName(String name)

{

return "你好 " + name;

}

public int add(int a,亚虎国际老虎机网址int b)

{

return a+b;

}

}

因为这两个措施都是public类型,以是都邑宣布成webservice。编译HelloWorld类后,将HelloWorld.class文件放到%tomcat%webappsaxis2WEB-INFpojo目录中(假如没有pojo目录,则建立该目录),然后打开浏览器进行测试:

输入一下url:

http://localhost:8080/axis2/services/listServices

会列出所有webservice

这是此中的两个webservice列表,接着,在客户端进行测试:

首先可以写一个封装类,削减编码,代码如下:

Java代码

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等数据类型

}

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";

Hell亚虎国际老虎机网址oWorld 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代码

HelloWorld webservice

service.HelloWorld

HelloWorld webservice

service.HelloWorld

此中元素用于宣布Web Service,一个元素只能宣布一个WebService类,name属性表示WebService名,如下面的URL可以得到这个WebService的WSDL内容:

http://localhost:8080/axis2/services/myService?wsdl

除此之外,还有直接可以在此中拟订webservice操作措施:可以这样些service.xml文件

Java代码

HelloWorld service

s亚虎国际老虎机网址ervice.HelloWorld

HelloWorld service

service.HelloWorld

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: