一. Web Service 是什么?
1. 基于web的服务,服务器端整出一些资源让客户端应用访问(获取数据)。
2. 一个跨语言,跨平台的规范(抽象)。
3. 多个跨平台,跨语言的应用问题间通信整合的方案(实际)。
二. 什么时候使用Web Service?
1. 同一个公司的新旧应用之间。
2. 不同公司的应用之间。
3. 一些提供数据的内容聚合应用:天气预报、股票行情等。
三. Web Service中的几个重要术语。
1. WSDL : web service defintion language (webService定义语言 )
xml
* 对应一种类型的文件.wsdl
* 定义了web service的服务端与客户端应用交互传递请求和响应数据的格式和方式
* 一个web service对应一个唯一的 wsdl 文档
2. SOAP : simple objectaccess protocal (简单对象访问协议)
* 是一种简单的,基于 http 和 xml 的协议,用于在web上交换结构化的数据
* soap消息:请求消息和响应消息
* http + xml 片段
3. SEI : WebService EndPoint Interface (web service的终端接口)
* 就是WebService服务端用来处理请求的接口
4. CXF : Celtix + XFire (一个用于开发 web service 服务器端和客户端的框架)
四. 使用JDK开发 Web Service (服务器端 + 客户端)
1. 开发服务器端(可以自己写也可以直接调用第三方WebService接口)
* Web Service编码
-- @WebService ( SEI 和 SEI 的实现类)
-- @WebMethod ( SEI 中所有的方法)
* 发布 Web Service
-- Endpoint(终端, 发布WebService)
服务器端代码:
SEI 接口:
package com.syp.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* WebService终端 SEI接口
*/
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
SEI 实现类:
package com.syp.server;
import javax.jws.WebService;
/**
* SEI实现类
*/
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
String message = "Hello," + name;
System.out.println("server is starting...");
return message;
}
}
2. 客户端编写
新建一个web工程,打开命令窗口,cd 到新建工程的src下,输入命令:wsimport -keep + webService的暴露端口 + ?wsdl 回车即生成客户端代码。
测试代码:
package com.syp.client.test;
import com.syp.server.HelloWSImpl;
import com.syp.server.HelloWSImplService;
/**
* webService调用测试
*/
public class TestClient {
public static void main(String[] args) {
HelloWSImplService helloWSImplService = new HelloWSImplService();
//此处返回SEI接口HelloWS的动态代理对象,即SEI的实现类
HelloWSImpl HelloWS = helloWSImplService.getHelloWSImplPort();
//System.out.println(HelloWS.toString());
String result = HelloWS.sayHello("webService");
System.out.println(result);
}
}