在自己的工程里发布webservice的步骤.docx

上传人:b****5 文档编号:8387691 上传时间:2023-01-31 格式:DOCX 页数:15 大小:37.88KB
下载 相关 举报
在自己的工程里发布webservice的步骤.docx_第1页
第1页 / 共15页
在自己的工程里发布webservice的步骤.docx_第2页
第2页 / 共15页
在自己的工程里发布webservice的步骤.docx_第3页
第3页 / 共15页
在自己的工程里发布webservice的步骤.docx_第4页
第4页 / 共15页
在自己的工程里发布webservice的步骤.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

在自己的工程里发布webservice的步骤.docx

《在自己的工程里发布webservice的步骤.docx》由会员分享,可在线阅读,更多相关《在自己的工程里发布webservice的步骤.docx(15页珍藏版)》请在冰豆网上搜索。

在自己的工程里发布webservice的步骤.docx

在自己的工程里发布webservice的步骤

在自己的工程里发布webservice的步骤

1.下载axis-bin-1_4,解压

2.新建web项目MyWebServices,把解压后文件夹axis-1_4\webapps\axis\WEB-INF\lib下的8个jar包拷贝到该项目的WebRoot\WEB-INF\lib下。

3.修改该项目的web.xml文件加入axis监听器还有servlet的相关配置:

示例代码:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWeb

Application2.3//EN""

Apache-Axis

org.apache.axis.transport.http.AxisHTTPSessionListener

AxisServlet

org.apache.axis.transport.http.AxisServlet

AdminServlet

org.apache.axis.transport.http.AdminServlet

100

SOAPMonitorService

org.apache.axis.monitor.SOAPMonitorService

SOAPMonitorPort

5001

100

AxisServlet

/servlet/AxisServlet

AxisServlet

*.jws

AxisServlet

/services/*

SOAPMonitorService

/SOAPMonitor

 

--Defaultto5minutesessiontimeouts-->

5

--currentlytheW3ChaventsettledonamediatypeforWSDL;

http:

//www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft

fornowwegowiththebasic'it'sXML'response-->

wsdl

text/xml

xsd

text/xml

index.jsp

index.html

index.jws

4.在自己的项目中创建一个最基本的业务类:

(这里编写了一简单业务:

输入参数:

String,返回数据:

String。

示例代码:

publicclassSayHello{

publicStringhello(Stringname){

return"你好,"+name+",axisVer1.4欢迎你.";

}

}

5.在web.xml下编写server-config.wsdd文件发布该业务为webservices

示例代码:

xmlns:

java="http:

//xml.apache.org/axis/wsdd/providers/java"

xmlns:

handler="http:

//xml.apache.org/axis/wsdd/providers/handler"xmlns="http:

//xml.apache.org/axis/wsdd/">

org.apache.axis.handlers.JWSHandler">

org.apache.axis.handlers.JWSHandler">

org.apache.axis.handlers.http.URLMapper"/>

org.apache.axis.transport.local.LocalResponder"/>

org.apache.axis.handlers.SimpleAuthenticationHandler"/>

org.apache.axis.handlers.http.HTTPAuthHandler"/>

--webservices的配置-->

--这里配置了一个WebService,如果有多个WebService,就按这种格式在下面增加即可-->

RPC">

说明:

第一行指明了该webservices的服务名和服务类型(rpc);

第二行是配置了services的scope,主要有Request,Session,Application三种

第三行是配置了webservices所对应的class

第四行是配置了该webservices所允许调用的方法,“*”表示所有的pubic方法都可供调用

6.把项目发布到tomcat上,下面我们调用一下。

打开浏览器,在地址栏输入http:

//127.0.0.1:

8080/MyWebServices/services

你会看到如下界面:

这就表示webservices发布成功了,单击wsdl查看一下对于该webservices的描述:

http:

//127.0.0.1:

8080/MyWebServices/services/SayHello?

wsdl:

 

xmlversion="1.0"encoding="UTF-8"?

>

-

definitionstargetNamespace="http:

//127.0.0.1:

8080/MyWebServices/services/SayHello"xmlns:

apachesoap="http:

//xml.apache.org/xml-soap"xmlns:

impl="http:

//127.0.0.1:

8080/MyWebServices/services/SayHello"xmlns:

intf="http:

//127.0.0.1:

8080/MyWebServices/services/SayHello"xmlns:

soapenc="http:

//schemas.xmlsoap.org/soap/encoding/"xmlns:

wsdl="http:

//schemas.xmlsoap.org/wsdl/"xmlns:

wsdlsoap="http:

//schemas.xmlsoap.org/wsdl/soap/"xmlns:

xsd="http:

//www.w3.org/2001/XMLSchema">

-

--

WSDLcreatedbyApacheAxisversion:

1.4

BuiltonApr22,2006(06:

55:

48PDT)

 -->

-

messagename="helloRequest">

 

partname="name"type="soapenc:

string"/>

 

message>

-

messagename="helloResponse">

 

partname="helloReturn"type="soapenc:

string"/>

 

message>

-

portTypename="SayHello">

-

operationname="hello"parameterOrder="name">

 

inputmessage="impl:

helloRequest"name="helloRequest"/>

 

outputmessage="impl:

helloResponse"name="helloResponse"/>

 

operation>

 

portType>

-

bindingname="SayHelloSoapBinding"type="impl:

SayHello">

 

bindingstyle="rpc"transport="http:

//schemas.xmlsoap.org/soap/http"/>

-

operationname="hello">

 

operationsoapAction=""/>

-

inputname="helloRequest">

 

bodyencodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/"namespace="http:

//DefaultNamespace"use="encoded"/>

 

input>

-

outputname="helloResponse">

 

bodyencodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/"namespace="http:

//127.0.0.1:

8080/MyWebServices/services/SayHello"use="encoded"/>

 

output>

 

operation>

 

binding>

-

servicename="SayHelloService">

-

portbinding="impl:

SayHelloSoapBinding"name="SayHello">

 

addresslocation="http:

//127.0.0.1:

8080/MyWebServices/services/SayHello"/>

 

port>

 

service>

 

definitions>

说明:

wsdl中描述了该webservices的方法为:

hello,输入参数为name,String类型,返回参数为String类型,调用方式为rpc,和我们之前在wsdd中配置的一样。

看完说明我们可以直接在浏览器中调用该webservices:

在浏览器中输入:

http:

//127.0.0.1:

8080/MyWebServices/services/SayHello?

method=hello&name=waiwai

你会看到:

xmlversion="1.0"encoding="UTF-8"?

>

-

Envelopexmlns:

soapenv="http:

//schemas.xmlsoap.org/soap/envelope/"xmlns:

xsd="http:

//www.w3.org/2001/XMLSchema"xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance">

-

Body>

-

encodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/">

 

type="soapenc:

string"xmlns:

soapenc="http:

//schemas.xmlsoap.org/soap/encoding/">你好,waiwai,axisVer1.4欢迎你.

 

 

Body>

 

Envelope

我们可以看到浏览器返回的是一String类型数据:

你好,waiwai,axisVer1.4欢迎你.

7.接下来我们使用外部项目调用该webservices:

创建新的项目CallWebServices,同样引入axis的8个jar包,创建调用webservices的类

示例代码:

importjavax.xml.namespace.QName;

importorg.apache.axis.client.Call;

importorg.apache.axis.client.Service;

publicclassCallSayHello{

publicstaticvoidmain(String[]args){

try{

Stringendpoint="http:

//127.0.0.1:

8080/MyWebServices/services/SayHello?

wsdl";

//webservices的wsdl的url地址

Serviceservice=newService();

//创建服务

Callcall=(Call)service.createCall();

//创建调用

call.setTargetEndpointAddress(endpoint);

//设置调用的url地址

//call.setOperationName("hello");

call.setOperationName(newQName("hello"));

//设置调用的方法(method)

//call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

//设置获取响应参数的格式

//call.setUseSOAPAction(true);

//call.setSOAPActionURI("

Stringresult=(String)call.invoke(newObject[]{"waiwai"});

//调用Webservice

System.out.println("resultis:

"+result);

}

catch(Exceptione){

System.err.println(e.toString());

}

}

 

}

执行该程序:

后台输出:

resultis:

你好,waiwai,axisVer1.4欢迎你.

这表示webservices调用成功。

8.实际应用中webservices的例子

调用天气预报的webservices:

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.UnsupportedEncodingException;

import.MalformedURLException;

import.URL;

import.URLConnection;

importjava.util.ArrayList;

importjava.util.List;

importjavax.xml.parsers.DocumentBuilder;

importjavax.xml.parsers.DocumentBuilderFactory;

importjavax.xml.parsers.ParserConfigurationException;

importorg.w3c.dom.DOMException;

importorg.w3c.dom.Document;

importorg.w3c.dom.Node;

importorg.w3c.dom.NodeList;

importorg.xml.sax.SAXException;

/**@srchttp:

//eric-

publicclassCallWeatherWebServices{

privatestaticStringSERVICES_HOST="";

privatestaticStringWEATHER_SERVICES_URL="

privatestaticStringPROVINCE_CODE_URL=WEATHER_SERVICES_URL

+"getRegionProvince";

privatestaticStringCITY_CODE_URL=WEATHER_SERVICES_URL

+"getSupportCityString?

theRegionCode=";

privatestaticStringWEATHER_QUERY_URL=WEATHER_SERVICES_URL

+"getWeather?

theUserID=&theCityCode=";

privateWeatherUtil(){

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

当前位置:首页 > 表格模板 > 合同协议

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

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