WebService
1、WebServiceWebService可以跨平台的使用第三方的数据,根据WebService规范,无论使用什么语言(不同的后台,比如java,.net等)都可以实现数据交互或者集成。比如我们在做java网站的时候,想实现一个天气,或者快递查询的功能(它们的后台并不一定是java的,可以是各种语言),这样有了WebService就可以轻松实现获取数据。 比如说:有了WebService我们可以轻松的实现查询天所,手机号,股票,新闻,火车票等数据 2、java中的WebService1、java中有三种WebService的实现:
从java5开始支持JAX-WS2.0,jdk1.7支持JAX-WS2.2版本 3、WebService三要素WebService三要素:SOAP,WSDL,UDDI 1、WSDLWSDL是(Web Service Description Language)用于描述WebService函数,参数和返回值 WSDL基于XML,以此便于阅读 如何查看WSDL: 根据service查找 port name和binding 然后查找方法,输入参数,转出值 2、SOAPSOAP(Simple Object Access Protocol)是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。 1、SOAP封装:它定义一个框架,描述消息中的内容是什么,是谁发送的,谁接收处理 SOAP 是一种网络通信协议 SOAP请求是在http中 POST /weather HTTP/1.1
Accept: text/xml,multipart/related
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://xxx/queryWeatherRequest"
User-Agent: JAX-WS RI 2.2.8 svn-revision#13980
Host: 127.0.0.1:4321
Connection: keep-alive
Content-Length: 232
<?xml version='1.0' encoding='UTF-8'?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:queryWeather xmlns:ns2="http://xxxxx/"> <arg0>北京</arg0> </ns2:queryWeather> </S:Body> </S:Envelope>
响应 HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8
<?xml version='1.0' encoding='UTF-8'?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:queryWeatherResponse xmlns:ns2="http://xxx/"> <return>天气晴朗</return> </ns2:queryWeatherResponse> </S:Body> </S:Envelope>
3、UDDIUDDI(Universal Description Discovery and Integration)企业可以通过它对Web Service进行注册和搜索。 4、使用1、http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 2、根据WeatherWebService.wsdl生成java代码 在命令行中 进行到工作空间后用 wsimport -s .文件目录 3、调用webservice代码 public static void main(String[] args) throws MalformedURLException {
// 创建服务视图
Service service = Service
.create(new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"),new QName("http://WebXml.com.cn/","WeatherWebService"));
// 获得PortType
WeatherWebServiceSoap portType = service
.getPort(WeatherWebServiceSoap.class);
// 调用服务端
ArrayOfString result = portType.getWeatherbyCityName("北京");
System.out.println(result.getString());
}
运行结果: 通过上面简单的几步就可以调用天气接口,实现天气查询的功能 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |