Web Service概括
1. Web Service中的几个重要术语
1.1. WSDL:web service definition language
直译 : WebService定义语言
1. 一个web service对应一个唯一的wsdl文档,对应一种类型的文件.wsdl 2. 定义了web service的服务器端与客户端应用交互传递请求和响应数据的格式和方式 |
1.2. SOAP:simple object access protocal
直译: 简单对象访问协议
|
1.3. SEI:WebService EndPoint Interface(终端)
直译: web service的终端接口,
1. 就是WebService服务器端用来处理请求的接口,在发表服务使用 – @WebService( SEI和SEI的实现类)
– @WebMethod(SEI中的所有方法)
|
1.4. CXF:Celtix + XFire
一个apache的用于开发webservice服务器端和客户端的框架 |
-----------------------------------------------------
2. 开发webservice
2.1. 概述
- 组成:
– 服务器端
– 客户端
2.2. 使用JDK开发WebService
1).开发服务器端
- Web Service编码:
– @WebService( SEI和SEI的实现类)
– @WebMethod(SEI中的所有方法)
- 发布Web Service:
– Endpoint(终端, 发布webservice)
@WebService public interface HelloWS { @WebMethod public String sayHello(String name); } |
@WebService public class HelloWSImpl implements HelloWS { @Override public String sayHello(String name) { System.out.println("server sayHello()"+name); return "Hello " +name; } } |
/* * 发布Web Service */ public class ServerTest {
public static void main(String[] args) {
String address = "http://127.0.0.1:8989/ws/hellows"; Endpoint.publish(address , new HelloWSImpl()); System.out.println("发布webservice成功!"); } } |
2). 开发客户端
- 使用eclipse提供的web service浏览器访问
– 查看对应的wsdl文档:…..?wsdl (一般浏览器)
– 请求webService并查看请求和响应消息(webservice浏览器)
http://127.0.0.1:8989/ws/hellows?wsdl |
- 创建客户端应用编码方式访问
– 借助jdk的wsimport.exe工具生成客户端代码:
wsimport -keep url //url为wsdl文件的路径
– 借助生成的代码编写请求代码
public class ClientTest {
public static void main(String[] args) { HelloWSImplService factory = new HelloWSImplService(); HelloWSImpl helloWS = factory.getHelloWSImplPort(); System.out.println(helloWS.getClass());
String result = helloWS.sayHello("Jack"); System.out.println("client "+result); } } |
-----------------------------------------------------------------------------------------------------------------------------
3.2. 调用免费的web service(天气预报) 和 调用国内手机号码归属地查询WEB服务
- Google”免费WebService”, 找到提供天气预报Webservice的网络地址
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
2.使用eclipse的web service浏览器访问
3.客户端编码方式访问
– 借助命令工具自动生成客户端代码
– 借助生成的代码编写请求代码
说明: 直接生成客户端代码会抛异常, 无法生成客户端代码, 解决办法:
将 <s:element ref="s:schema" /><s:any /> 替换成 <s:any minOccurs="2" maxOccurs="2"/> 备注: 这个是Java调用net的webservice都有的问题 |
1、把在线地址http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 的内容保存到本地文件
2、把里面 <s:element ref="s:schema" /><s:any /> 替换成 <s:any minOccurs="2" maxOccurs="2"/>
修改3处
3、执行 wsimport -keep E:\oxygen_workspace\weather_client\weather.wsdl
生成本地文件
4、把生成的代码拷贝到工程中
5、编写客户端
public class ClientTest {
public static void main(String[] args) { WeatherWS weatherWS = new WeatherWS(); WeatherWSSoap weatherWSSoap = weatherWS.getWeatherWSSoap(); ArrayOfString weather = weatherWSSoap.getWeather("昌平", null); List<String> list = weather.getString(); for(String s : list) { System.out.println(s+"------"); }
} } |
调用国内手机号码归属地查询WEB服务
1、地址http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
2、生成客户端
wsimport -keep http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
3、把生成的文件拷入工程,编写客户端
public class ClientTest {
public static void main(String[] args) { MobileCodeWS factory = new MobileCodeWS(); MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap(); String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("15010176627", null); System.out.println(mobileCodeInfo); } } |
--------------------------------------------------------------------------------------------
4.2. WebService请求深入分析
1). 分析WebService的WSDL文档结构
1.1). 实例截图
<definitions>
<type>
<message>
<portType>
<binding>
<service>
1.2). 文档结构
<definitions> <types> <schema> <element> </types> <message> <part> </message> <portType> <operation> <input> <output> </portType> <binding> <operation> <input> <output> </binding> <service> <port> <address> </service> </definitions> |
1.3). 文档结构图
1.4). 重要标签的说明
- types - 数据类型(标签)定义的容器,里面使用schema定义了一些标签结构供message引用
- message - 通信消息的数据结构的抽象类型化定义。引用types中定义的标签
- operation - 对服务中所支持的操作的抽象描述,一个operation描述了一个访问入口的请求消息与响应消息对。
- portType - 对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。
- binding - 特定端口类型的具体协议和数据格式规范的绑定。
- service- 相关服务访问点的集合
- port - 定义为协议/数据格式绑定与具体Web访问地址组合的单个服务访问点。