创建WebService.docx

上传人:b****5 文档编号:12191189 上传时间:2023-04-17 格式:DOCX 页数:18 大小:528.96KB
下载 相关 举报
创建WebService.docx_第1页
第1页 / 共18页
创建WebService.docx_第2页
第2页 / 共18页
创建WebService.docx_第3页
第3页 / 共18页
创建WebService.docx_第4页
第4页 / 共18页
创建WebService.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

创建WebService.docx

《创建WebService.docx》由会员分享,可在线阅读,更多相关《创建WebService.docx(18页珍藏版)》请在冰豆网上搜索。

创建WebService.docx

创建WebService

myeclipse6.5上基于JAX-WS开发Webservice(中文示例)

关键字:

webservice

1.Introduction

ThisdocumentwilloutlinetheprocessofdevelopingaJAX-WSwebserviceanddeployingitusingMyEclipse6.5totheinternalMyEclipseTomcatserver.Thewebserviceusedinthistutorialwillbeaverysimplecalculatorservicethatprovidesadd,subtract,multiplyanddivideoperationstothecaller.

MyEclipsealsosupportsdevelopingwebservicesusingtheexistingXFireframeworkfrompreviousMyEclipsereleases.ForfolksneedingtodevelopanddeployWebSphereJAX-RPCorWebSphereJAX-WSwebservices,pleasetakealookatourMyEclipseBlueEditionoftheMyEclipseIDE.

AdditionalresourcescoveringwebservicecreationusingJAX-RPC,JAX-WSorXFireareincludedintheResourcessectionofthisdocument.

2.SystemRequirements

ThistutorialwascreatedwithMyEclipse6.5.IfyouareusinganotherversionofMyEclipse(possiblynewer),mostofthesescreensandinstructionsshouldstillbeverysimilar.

IfyouareusinganewerversionofMyEclipseandnoticeportionsofthistutoriallookingdifferentthanthescreensyouareseeing,pleaseletusknowandwewillmakesuretoresolveanyinconsistencies.

3.新建一个工程

开始我们新建一个WebServiceProject工程File->New->WebServiceProject(OptionalMavenSupport)

Note:

AJAX-WSwebservicecanalsobegeneratedinanyexistingJavaEE5webproject.

我们给这个工程取名为WebServiceProject.注意JAX-WS支持只在javaEE5或更高版本的工程中是可行的。

如果你需要使用低版本的工程类型(java1.4或者1.3),那么只能使用XFireWebService代替JAX-WS。

这里我们使用上面的JAX—WS。

4.创建服务类

服务类就是一个普通的java类,负责提供我们想要发布的执行方法。

这里我们写一个简单的计算器类,实现几个典型的计算器应用方法,如加减乘除。

首先我们先建一个包,WebServiceProject->src->new->package,取名com.myeclipseide.ws

让后我们在这个包下建一个类,Calculator.java.

根据上面提到的,这个计算器类实现计算器的加减乘除算法,简单实现:

Java代码

1.package com.myeclipseide.ws;   

2.  

3.public class Calculator {   

4.    public int add(int a, int b) {   

5.        return (a + b);   

6.    }   

7.  

8.    public int subtract(int a, int b) {   

9.        return (a - b);   

10.    }   

11.  

12.    public int multiply(int a, int b) {   

13.        return (a * b);   

14.    }   

15.  

16.    public int divide(int a, int b) {   

17.        return (a / b);   

18.    }   

19.}   

packagecom.myeclipseide.ws;

publicclassCalculator{

publicintadd(inta,intb){

return(a+b);

}

publicintsubtract(inta,intb){

return(a-b);

}

publicintmultiply(inta,intb){

return(a*b);

}

publicintdivide(inta,intb){

return(a/b);

}

}

可以看出,这个类中的方法是非常简单的,没有用到特殊的注释还有接口,父类之类的东西。

5.创建一个WebService

在上面的工具条中点击新建WebService

Note:

如果没有的话可以File->New->others->Myeclipse->WebService->webService

点击之后出现的屏幕,在Strategy中选择Bottom-upscenario,因为我们已经建立好了Calculator类而且想根据它建立JAX-WS服务。

下面是创建的最后一个屏幕,你需要选择提供webService方法的javaBean,在我们这个例子中就是我们已经建立好的Calculator类。

填好之后,Myeclipse会自动帮我们填满其他的项,SelectGenerateWSDLinprojectandhitFinish.

点击完成之后,MyWebService的一些mappings,方便将webService部署到服务器中。

到此webservice已经建立好了,我们开始部署它然后进行测试。

6.部署和测试webService。

这里我们不使用用Myeclipse自带的tomcat服务器,使用自己应经在电脑中部署好的tomcat5.5。

在server面板中右击,选择configure

部署自己的tomcat注意选择jdk要跟项目中的相同。

现在要向工程中导入JAX-WS的jar包

在项目名称上右击->properties->AddLibrary->MyeclipseLibraries->最后面的两个。

点击完成,导入成功。

Note:

Myeclipse自带的tomcat中有自带的这两个jar包,可以不用导入。

6.1部署

在部署好的tomcat服务器上右击选择AddDeployment

点击完成。

6.2测试

运行tomcat服务器,在工具栏中点击launchWebServiceExplorer

打开后,点击右上角的WSDL视图,可以看到下面的屏幕

在WSDLURL中填写路径:

http:

//localhost:

8888/WebServiceProject/CalculatorPort?

WSDL

解释下路径组成:

http:

//localhost:

8888/是服务器的路径,我的端口号是8888,可以根据自己的更改,一般都是8080。

/WebServiceProject=WeknowbydefaulttheWebContext-rootthatisusedtodeploy(部署)webprojectsmatchesthenameoftheprojects.(因为我们没有为这个工程自定义我们的WebContext-root,所以他就是这个工程的名字)

/CalculatorPort=AswesawfromthelastscreenshotinSection#5,whenourJAX-WSwebservicewasgenerated,itwasboundusingaservlet-mappingintheweb.xmlfiletothe/CalculatorPortpath.

Xml代码

1.  

2.    JAX-WS endpoint - CalculatorService  

3.    CalculatorService  

4.    CalculatorService  

5.      

6.        com.sun.xml.ws.transport.http.servlet.WSServlet  

7.      

8.    1  

9.    

10.    

11.    CalculatorService  

12.    /CalculatorPort  

13.    

JAX-WSendpoint-CalculatorService

CalculatorService

CalculatorService

com.sun.xml.ws.transport.http.servlet.WSServlet

1

CalculatorService

/CalculatorPort

?

WSDL=Thisisauniversalquerystringargumentthatcanbeaddedtotheendofanywebservicewhichwilltellthewebservicetoreturnit'sfullWSDLtothecaller.Inthiscase,theWSDLisreturnedtoourWebServicesExplorertoolwhichloadsitup,anddisplaysthewebservicesexposedoperationstous.

弄清楚之后,我们开始测试,比如我们选择add方法:

填写args,点击go,在status中就会显示结果。

结果是正确的。

7.创建WebserviceClient

现在我们已经部署好Webservice,而且应经测试过了,那我们新建一个Webserviceclient,来调用Webservice提供的方法。

7.1新建一个javaproject,给他取个名字。

比如我们叫它ClientofWebService

在工具条中点击newWebServiceClient

然后按照以下步骤操作:

ThelaststepofthewebserviceclientcreationistospecifyeitheraWSDLFileoraWSDLURLforthewizardtoretrievethewebserviceWSDLfrom.InourcaseweareusingtheURLandgeneratetheclientintothenewpackagecom.myeclipseide.ws.client:

http:

//localhost:

8888/WebServiceProject/CalculatorPort?

WSDL

点击Next知道完成。

可以看到在新建的javaprojectClientofWebService中,src文件夹下产生了许多的文件,根据名称我们大体可以了解其意思,可以打开看一下源代码,其实不难理解。

比如add文件,就是Calculator类中add方法的两个参数的get和set方法。

其他类似。

我们在文件夹下见一个类test.java写一个main函数测试

Java代码

1.public static void main(String[] args) {   

2.    /* Create the service instance */   

3.    CalculatorService service = new CalculatorService();   

4.    CalculatorDelegate delegate = service.getCalculatorPort();   

5.  

6.    /* Using the web service, perform the 4 calculations */   

7.    System.out.println("1. 3+7=" + delegate.add(3, 7));   

8.    System.out.println("2. 12-2=" + delegate.subtract(12, 2));   

9.    System.out.println("3. 9*9=" + delegate.multiply(9, 9));   

10.    System.out.println("4. 40/2=" + delegate.divide(40, 2));   

11.}   

publicstaticvoidmain(String[]args){

/*Createtheserviceinstance*/

CalculatorServiceservice=newCalculatorService();

CalculatorDelegatedelegate=service.getCalculatorPort();

/*Usingthewebservice,performthe4calculations*/

System.out.println("1.3+7="+delegate.add(3,7));

System.out.println("2.12-2="+delegate.subtract(12,2));

System.out.println("3.9*9="+delegate.multiply(9,9));

System.out.println("4.40/2="+delegate.divide(40,2));

}

运行得到如下结果:

1.3+7=10

2.12-2=10

3.9*9=81

4.40/2=20

测试完成。

8.Resources

Inthissectionwewanttoprovideyouwithadditionallinkstoresourcesthatsupplementthetopicscoveredinthistutorial.Whilethisisnotanexhaustivelist,wedomakeanefforttopointtothemorepopularlinksthatshouldprovideyouwithdiverse,high-qualityinformation.

ExampleWebServiceProject&WebServiceClientProject

NOTE:

TheexampleprojectsprovidedareconfiguredtorunagainstWebSphere6.1.YoumayneedtoadjusttheTargetServerand/ortheruntimeJRElibrariesusedtobuildtheprojectstomorecloselymatchyourparticularbuildanddeploymentenvironment.

有些地方说的不是很好,但是大体应该能够弄明白,限于能力问题,请大家体谅。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 军事政治

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1