创建WebService文档格式.docx

上传人:b****2 文档编号:4579240 上传时间:2023-05-03 格式: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_第6页
第6页 / 共18页
创建WebService文档格式.docx_第7页
第7页 / 共18页
创建WebService文档格式.docx_第8页
第8页 / 共18页
创建WebService文档格式.docx_第9页
第9页 / 共18页
创建WebService文档格式.docx_第10页
第10页 / 共18页
创建WebService文档格式.docx_第11页
第11页 / 共18页
创建WebService文档格式.docx_第12页
第12页 / 共18页
创建WebService文档格式.docx_第13页
第13页 / 共18页
创建WebService文档格式.docx_第14页
第14页 / 共18页
创建WebService文档格式.docx_第15页
第15页 / 共18页
创建WebService文档格式.docx_第16页
第16页 / 共18页
创建WebService文档格式.docx_第17页
第17页 / 共18页
创建WebService文档格式.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

创建WebService文档格式.docx

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

创建WebService文档格式.docx

首先我们先建一个包,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, 

b) 

5. 

return 

(a 

b);

6. 

7. 

8. 

subtract(int 

9. 

10. 

11. 

12. 

multiply(int 

13. 

14. 

15. 

16. 

divide(int 

17. 

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

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

最后面的两个。

点击完成,导入成功。

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

6.1部署

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

点击完成。

6.2测试

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

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

在WSDLURL中填写路径:

http:

//localhost:

8888/WebServiceProject/CalculatorPort?

WSDL

解释下路径组成:

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

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

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

Xml代码

1.<

servlet>

<

description>

JAX-WS 

endpoint 

CalculatorService<

/description>

3. 

display-name>

/display-name>

servlet-name>

/servlet-name>

servlet-class>

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

/servlet-class>

load-on-startup>

1<

/load-on-startup>

/servlet>

servlet-mapping>

url-pattern>

/CalculatorPort<

/url-pattern>

/servlet-mapping>

<

JAX-WSendpoint-CalculatorService<

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

<

?

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:

点击Next知道完成。

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

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

其他类似。

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

1.public 

static 

void 

main(String[] 

args) 

/* 

Create 

the 

service 

instance 

*/ 

CalculatorService 

new 

CalculatorService();

CalculatorDelegate 

delegate 

service.getCalculatorPort();

Using 

web 

service, 

perform 

calculations 

System.out.println("

1. 

3+7="

delegate.add(3, 

7));

2. 

12-2="

delegate.subtract(12, 

2));

3. 

9*9="

delegate.multiply(9, 

9));

4. 

40/2="

delegate.divide(40, 

11.} 

publicstaticvoidmain(String[]args){

/*Createtheserviceinstance*/

CalculatorServiceservice=newCalculatorService();

CalculatorDelegatedelegate=service.getCalculatorPort();

/*Usingthewebservice,performthe4calculations*/

System.out.println("

1.3+7="

+delegate.add(3,7));

2.12-2="

+delegate.subtract(12,2));

3.9*9="

+delegate.multiply(9,9));

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-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2