改建器-scvhost exe

axisfault
2023年3月31日发(作者:小说格式转换器下载)

java调用webservice的各种方法总结

现在webservice加xml技术已经逐渐成熟,但要真正要用起来还需时日!!

由于毕业设计缘故,我看了很多关于webservice方面的知识,今天和大家一起来研究

研究webservice的各种使用方法。

一、利用jdkweb服务api实现,这里使用基于SOAPmessage的Web服务

1.首先建立一个WebservicesEndPoint:packageHello;

vice;

hod;

nt;

@WebService

publicclassHello{

@WebMethod

publicStringhello(Stringname){

return"Hello,"+name+"n";

}

publicstaticvoidmain(String[]args){

//createandpublishanendpoint

Hellohello=newHello();

Endpointendpoint=h("http://localhost:8080/hello",hello);

}

}

2.使用apt编译(例:apt-d[存放编译后的文件目录]),会生成

jaws目录

3.使用运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出

现下列显示

4.使用wsimport生成客户端

使用如下:wsimport-p.-keephttp://localhost:8080/hello?wsdl

这时,会在当前目录中生成如下文件:

5.客户端程序:

1classHelloClient{

2publicstaticvoidmain(Stringargs[]){

3HelloServiceservice=newHelloService();

4HellohelloProxy=loPort();

5Stringhello=("你好");

n(hello);

7}

8}

9

以上方法还稍显繁琐,还有更加简单的方法

二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的

利用xfire开发WebService,可以有三种方法:

1一种是从javabean中生成;

2一种是从wsdl文件中生成;

3还有一种是自己建立webservice

步骤如下:

用myeclipse建立webservice工程,目录结构如下:

首先建立webservice接口,

代码如下:

ple;

2//GeneratedbyMyEclipse

3

4publicinterfaceIHelloWorldService{

5

6publicStringexample(Stringmessage);

7

8}接着实现这个借口:ple;

2//GeneratedbyMyEclipse

3

4publicclassHelloWorldServiceImplimplementsIHelloWorldService{

5

6publicStringexample(Stringmessage){

7returnmessage;

8}

9

10}修改文件,加入以下代码:

1

2HelloWorldService

3

WorldService

5

6

orldServiceImpl

8

9

10literal

11application

12把整个项目部署到tomcat服务器中,打开浏览器,输入

http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:

然后再展开HelloWorldService后面的wsdl可以看到:

客户端实现如下:;

2

medURLException;

;

5

actory;

;

roxyFactory;

e;

ServiceFactory;

11

WorldService;

13

14publicclassHelloWorldClient{

15publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{

16//TODOAuto-generatedmethodstub

17Services=newObjectServiceFactory().create();

18XFireProxyFactoryxf=newXFireProxyFactory(tance().getXFire());

19Stringurl="http://localhost:8989/HelloWorld/services/HelloWorldService";

20

21try

22{

23

24IHelloWorldServicehs=(IHelloWorldService)(s,url);

25Stringst=e("zhangjin");

(st);

27}

28catch(Exceptione)

29{

tackTrace();

31}

32}

33

34}

35这里再说点题外话,有时候我们知道一个wsdl地址,比如想用java客户端引用.net

做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直

接在类中引用,步骤如下:

1publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{

2//TODOAuto-generatedmethodstub

3Services=newObjectServiceFactory().create();

4XFireProxyFactoryxf=newXFireProxyFactory(tance().getXFire());

5

6

7//远程调用.net开发的webservice

8Clientc=newClient(new

URL("/webservices/?wsdl"));

9Object[]o=("qqCheckOnline",newString[]{"531086641","591284436"});

10

11//调用.net本机开发的webservice

12Clientc1=newClient(newURL("http://localhost/zj/?wsdl"));

13Object[]o1=("HelloWorld",newString[]{});

14

15}

三、使用axis1.4调用webservice方法

前提条件:下载axis1.4包和tomcat服务器,并将axis文件夹复制到tomcat服务器

的webapp文件夹中

这里我就说一下最简单的方法:

首先建立一个任意的java类(例如:),复制到axis文件夹下,将其

扩展名改为jws,然后重新启动tomcat,在浏览器中输入

http://localhost:8989/axis/?wsdl,就会得到一个wsdl文件,其客户端调用方法

如下:

e;

eException;

eFactory;

4

medURLException;

;

Exception;

8

;

10

11publicclassTestHelloWorld{

12

13

14publicstaticvoidmain(String[]args)throwsMalformedURLException,ServiceException,

RemoteException{

15//TODOAuto-generatedmethodstub

16

17StringwsdlUrl="http://localhost:8989/axis/?wsdl";

18StringnameSpaceUri="http://localhost:8989/axis/";

19StringserviceName="HelloWorldService";

20StringportName="HelloWorld";

21

22ServiceFactoryserviceFactory=tance();

23ServiceafService=Service(newURL(wsdlUrl),new

QName(nameSpaceUri,serviceName));

24HelloWorldInterfaceproxy=(HelloWorldInterface)t(new

QName(nameSpaceUri,portName),);

n("returnvalueis"+e("john"));

26

27}

28

29}

30四、使用axis2开发webservice(这里首先感谢李宁老师)

使用axis2需要先下载

/axis2/

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

在axis2中部署webservice有两种方法,

第一种是pojo方式,这种方式比较简单,但是有一些限制,例如部署的类不能加上包

第二种方式是利用xml发布webservice,这种方法比较灵活,不需要限制类的声明

下面分别说明使用方法:

方式:在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布

成WebService。其中POJO中所有的public方法将被发布成WebService方法。先实现一个

pojo类:

1publicclassHelloWorld{

2publicStringgetName(Stringname)

3{

4return"你好"+name;

5}

6publicintadd(inta,intb)

7{

8returna+b;

9}

10}

11由于这两个方法都是public类型,所以都会发布成webservice。编译HelloWorld类后,

将文件放到%tomcat%webappsaxis2WEB-INFpojo目录中(如果没有pojo

目录,则建立该目录),然后打开浏览器进行测试:

输入一下url:

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

会列出所有webservice

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

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

Service;

;

3

ult;

ntReference;

s;

viceClient;

8

9

10publicclassGetWSByAxis2{

11privatestaticStringEndPointUrl;

12privatestaticStringQUrl="/axis2";

13privateQNameopAddEntry;

14publicStringWSUrl;

15publicRPCServiceClientsetOption()throwsAxisFault

16{

17RPCServiceClientserviceClient=newRPCServiceClient();

18Optionsoptions=ions();

19EndpointReferencetargetEPR=newEndpointReference(WSUrl);

(targetEPR);

21returnserviceClient;

22}

23

24publicQNamegetQname(StringOption){

25

26returnnewQName(QUrl,Option);

27}

28//返回String

29publicStringgetStr(StringOption)throwsAxisFault

30{

31RPCServiceClientserviceClient=ion();

32

33opAddEntry=me(Option);

34

35Stringstr=(String)Blocking(opAddEntry,

36newObject[]{},newClass[]{})[0];

37returnstr;

38}

39//返回一维String数组

40publicString[]getArray(StringOption)throwsAxisFault

41{

42RPCServiceClientserviceClient=ion();

43

44opAddEntry=me(Option);

45

46String[]strArray=(String[])Blocking(opAddEntry,

47newObject[]{},newClass[]{String[].class})[0];

48returnstrArray;

49}

50//从WebService中返回一个对象的实例

51publicObjectgetObject(StringOption,Objecto)throwsAxisFault

52{

53RPCServiceClientserviceClient=ion();

54QNameqname=me(Option);

55Objectobject=Blocking(qname,newObject[]{},new

Class[]{ss()})[0];

56returnobject;

57}

58

59/////////////////////////////////////////读者可以自己封装数据类型,如int,byte,float

等数据类型

60}

61客户端调用方法:

yAxis2ws=yAxis2();

="http://localhost:8989/axis2/services/HelloWorld";

HelloWorldhello=(HelloWorld)ect("getName",);

n(e("zhangjin"));

2.使用发布webservice,这种方式和直接放在pojo目录中的POJO类不同。

要想将MyService类发布成WebService,需要一个文件,这个文件需要放在

META-INF目录中,该文件的内容如下:

HelloWorldwebservice

orld

class="sageReceiver"/>

class="nlyMessageReceiver"/>

其中元素用于发布WebService,一个元素只能发布一个WebService类,

name属性表示WebService名,如下面的URL可以获得这个WebService的WSDL内容:

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

除此之外,还有直接可以在其中制定webservice操作方法:可以这样些文件

1

2

3HelloWorldservice

4

5

orld

7

8

9

10

11

12

13class="sageReceiver"/>

14

15

16如果要发布多个webservice,可以在文件两段加上

...发布

更多推荐

axisfault