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

2018-11-23 20:16

namespace=\http://DefaultNamespace\ use=\encoded\

-

encodingStyle=\http://schemas.xmlsoap.org/soap/encoding/\

encodingStyle=\http://schemas.xmlsoap.org/soap/encoding/\

namespace=\http://127.0.0.1:8080/MyWebServices/services/SayHello\ use=\encoded\

-

-

location=\http://127.0.0.1:8080/MyWebServices/services/SayHello\

说明:wsdl中描述了该webservices的方法为:hello,输入参数为name,String类型,返回参数为String类型,调用方式为rpc,和我们之前在wsdd中配置的一样。

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

http://127.0.0.1:8080/MyWebServices/services/SayHello?method=hello&name=waiwai 你会看到:

xmlns:soapenv=\http://schemas.xmlsoap.org/soap/envelope/\ xmlns:xsd=\http://www.w3.org/2001/XMLSchema\

xmlns:xsi=\http://www.w3.org/2001/XMLSchema-instance\ - -

soapenv:encodingStyle=\http://schemas.xmlsoap.org/soap/encoding/\

好,waiwai, axis Ver1.4 欢迎你.

xsi:type=\soapenc:string\

xmlns:soapenc=\http://schemas.xmlsoap.org/soap/encoding/\

我们可以看到浏览器返回的是一String类型数据:你好,waiwai, axis Ver1.4 欢迎你.

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

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

示例代码:

import javax.xml.namespace.QName;

import org.apache.axis.client.Call; import org.apache.axis.client.Service;

public class CallSayHello { ;

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

//call.setUseSOAPAction(true);

//call.setSOAPActionURI(\

String result = (String)call.invoke(new Object[]{\}); //调用Webservice

System.out.println(\+result); }

//call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING)public static void main(String[] args) {

try {

String endpoint =

//webservices的wsdl的url地址

Service service = new Service(); //创建服务

Call call = (Call) service.createCall(); //创建调用

call.setTargetEndpointAddress(endpoint); //设置调用的url地址

//call.setOperationName(\

call.setOperationName(new QName(\)); //设置调用的方法(method)

\;

}

}

catch (Exception e) {

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

执行该程序:后台输出:

result is :你好,waiwai, axis Ver1.4 欢迎你. 这表示webservices调用成功。

8. 实际应用中webservices的例子

调用天气预报的webservices:

import java.io.IOException; import java.io.InputStream;

import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL;

import java.net.URLConnection; import java.util.ArrayList; import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException;

/**@src http://eric-619.javaeye.com/blog/693673*/ public class CallWeatherWebServices {

private static String SERVICES_HOST = \; private static String WEATHER_SERVICES_URL =

\; private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL + \;

private static String CITY_CODE_URL = WEATHER_SERVICES_URL + \;

private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL + \;

private WeatherUtil(){}

public static void main(String[] args) throws Exception{ int provinceCode = getProvinceCode(\上海\); //省份 int cityCode = getCityCode(provinceCode, \浦东\); //城市 List weatherList = getWeather(cityCode); for(String weather:weatherList){ System.out.println(weather); } }

public static int getProvinceCode(String provinceName){ Document document;

DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance(); documentBF.setNamespaceAware(true); int provinceCode = 0; try{

DocumentBuilder documentB = documentBF.newDocumentBuilder(); InputStream inputStream =

getSoapInputStream(PROVINCE_CODE_URL); //具体webService相关 document = documentB.parse(inputStream);

NodeList nodeList = document.getElementsByTagName(\); //具体webService相关

int len = nodeList.getLength(); for(int i = 0; i < len; i++){ Node n = nodeList.item(i);

String result = n.getFirstChild().getNodeValue(); String[] address = result.split(\); String pName = address[0]; String pCode = address[1];

if(pName.equalsIgnoreCase(provinceName)){ provinceCode = Integer.parseInt(pCode); } }

inputStream.close();

}catch(DOMException e){ e.printStackTrace();

}catch(ParserConfigurationException e){ e.printStackTrace(); }catch (SAXException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }

return provinceCode; }

public static int getCityCode(int provinceCode, String cityName){ Document doc;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); int cityCode = 0; try{

DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode); //具体webService相关 doc = db.parse(is);

NodeList nl = doc.getElementsByTagName(\); //具体webService相关

int len = nl.getLength(); for(int i = 0; i < len; i++){ Node n = nl.item(i);

String result = n.getFirstChild().getNodeValue(); String[] address = result.split(\); String cName = address[0]; String cCode = address[1];

if(cName.equalsIgnoreCase(cityName)){ cityCode = Integer.parseInt(cCode); } }

is.close();

}catch(DOMException e){ e.printStackTrace();

}catch(ParserConfigurationException e){ e.printStackTrace(); }catch (SAXException e){ e.printStackTrace(); }catch(IOException e) {


在自己的工程里发布webservice的步骤(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:厂房、生产设施和设备变更风险评估

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: