Webservice就是一种远程调用技术,它的作用就是从远程系统中获取业务数据。
客户端通过网络通信协议访问服务端,网络协议包括TCP和UDP两大通信协议:
TCP是一种面向连接的协议,提供可靠的数据传输,一般服务质量要求比较高的情况,使用这个协议。
TCP支持的应用协议主要有:Telnet、FTP、SMTP、HTTP等;
UDP用户数据报协议,是一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务。
UDP支持的应用层协议主要有:NFS(网络文件系统)、SNMP(简单网络管理协议)、DNS(主域名称系统)、TFTP(通用文件传输协议)等。
客户服务器模式早期主要应用于c/s应用,web兴起后主要应用于b/s应用,b/s比c/s的好处就在于b/s是基于浏览器客户端访问服务端。
课程安排:
Web service 即web服务,它是一种跨编程语言和跨操作系统平台的远程调用技术即跨平台远程调用技术。
详解如下:
JAVA 中共有三种WebService 规范,分别是JAX-WS(JAX-RPC)、JAXM&SAAJ、JAX-RS。
下面来分别简要的介绍一下这三个规范。
1、JAX-WS(小公司使用)
JAX-WS 的全称为 Java API for XML-Based Webservices ,早期的基于SOAP 的JAVA 的Web 服务规范JAX-RPC(Java API For XML-Remote Procedure Call),
目前已经被JAX-WS规范取代。从java5开始支持JAX-WS2.0版本,Jdk1.6.0_13以后的版本支持2.1版本,jdk1.7支持2.2版本。
2、JAXM&SAAJ(不常用)
JAXM(JAVA API For XML Message)主要定义了包含了发送和接收消息所需的API,SAAJ(SOAP With Attachment API For Java,JSR 67)是与JAXM 搭配使用的API,
为构建SOAP 包和解析SOAP 包提供了重要的支持,支持附件传输等,JAXM&SAAJ 与JAX-WS 都是基于SOAP 的Web 服务,相比之下JAXM&SAAJ 暴露了SOAP更多的底层细节,编码比较麻烦,
而JAX-WS 更加抽象,隐藏了更多的细节,更加面向对象,实现起来你基本上不需要关心SOAP 的任何细节。
3、JAX-RS(大公司使用)
JAX-RS 是JAVA 针对REST(Representation State Transfer)风格制定的一套Web 服务规范,由于推出的较晚,该规范(JSR 311,目前JAX-RS 的版本为1.0)并未随JDK1.6 一起发行。
服务端:发布一个天气查询服务,接收客户端城市名称,返回天气数据给客户端。 客户端:发送城市名称给服务端,接收服务端的返回天气数据,打印出来。
JDK:1.8 Eclipse:Oxygen
开发步骤: 第一步:创建SEI(Service Endpoint Interface)接口,本质上就是Java接口
package com.itheima.webservice.jaxws.ws;
/*
* SEI接口
*/
public interface WeatherInterface {
public String queryWeather(String cityName);
}
第二步:创建SEI实现类,在实现类上加入注解@WebService
package com.itheima.webservice.jaxws.ws;
import javax.jws.WebService;
@WebService // @WebService表示该类是一个服务类,需要发布其中的public的方法
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String queryWeather(String cityName) {
System.out.println("from client..." + cityName);
String weather = "晴";
return weather;
}
}
第三步:发布服务,Endpoint发布服务,publish方法,两个参数:1.服务地址;2.服务实现类
package com.itheima.webservice.jaxws.ws;
import javax.xml.ws.Endpoint;
public class WeatherServer {
public static void main(String[] args) {
// Endpoint 发布服务
// 参数解释:
// 1.address - 服务地址
// 2.implementor - 服务实现类
Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
}
}
第四步:测试Webservice服务是否发布成功,通过阅读使用说明书,确定客户端调用的接口、方法、参数和返回值存在,证明服务发布成功。
如下图所示: 1、
2、
3、
开发步骤:
示例代码如下:
package com.itheima.webservice.jaxws.ws.client;
import com.itheima.webservice.jaxws.ws.WeatherInterfaceImpl;
import com.itheima.webservice.jaxws.ws.WeatherInterfaceImplService;
/*
* 天气查询客户端
*/
public class WeatherClient {
public static void main(String[] args) {
// 创建服务视图
WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
// 获取服务实现类
WeatherInterfaceImpl weatherInterfaceImpl = weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class);
// 调用查询方法,打印
String weather = weatherInterfaceImpl.queryWeather("北京");
System.out.println(weather);
}
}
客户端效果如下图所示:
服务端效果如下图所示:
详解如下:
<service> 服务视图:webservice的服务结点,它包括了服务端点port(一般会有多个服务端点)。
<binding> 为每个服务端点port定义消息格式和协议细节。
<portType> 服务端点类型:描述 web service可被执行的操作方法,以及相关的消息,通过binding指向portType。
<message> 定义一个操作(方法)的数据参数(可有多个参数)。
<types> 定义 web service 使用的全部数据类型。
从下往上读:先找到服务视图,通过binging找到protType,找到了protType就找到了我们要调用的webservice方法。
WSDL的文档结构图以及WSDL的阅读方式如下图所示;
必需有 envelope 元素
,此元素将整个 XML 文档标识为一条 SOAP 消息。必需有 body 元素
,包含所有的调用和响应信息。在MyEcplise中如何进行配置呢?步骤如下: Windows --> Show View --> Other…
在搜索框中输入“tcp”
在小三角处点击,选择 Properties
再选择 Add…,配置解释如下图所示:
配置如下图所示:
在浏览器中输入代理服务地址,能正常访问,代表代理服务器设置成功,如下图所示:
修改客户端代码,修改的代码文件是WeatherInterfaceImplService.java,把该代码里面所有的端口号12345改为54321,保存,打开服务端,查看代理服务器是否有作用,如下图所示,代理服务器起作用了。
有中文乱码,我们设置一下编码方式:
请求:
......
POST /weather HTTP/1.1
Accept: text/xml, multipart/related
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://ws.jaxws.webservice.itheima.com/WeatherInterfaceImpl/queryWeatherRequest"
User-Agent: JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e
Host: 127.0.0.1:54321
Connection: keep-alive
Content-Length: 224
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:queryWeather xmlns:ns2="http://ws.jaxws.webservice.itheima.com/">
<arg0>北京</arg0></ns2:queryWeather>
</S:Body>
</S:Envelope>
响应:
......
HTTP/1.1 200 OK
Date: Thu, 20 Sep 2018 07:44:22 GMT
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:queryWeatherResponse xmlns:ns2="http://ws.jaxws.webservice.itheima.com/">
<return>晴</return></ns2:queryWeatherResponse>
</S:Body>
</S:Envelope>
请求:
......
POST /weather HTTP/1.1
Accept: application/soap+xml, multipart/related
Content-Type: application/soap+xml; charset=utf-8;action="http://ws.jaxws.webservice.itheima.com/WeatherInterfaceImpl/queryWeatherRequest"
User-Agent: JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e
Host: 127.0.0.1:54321
Connection: keep-alive
Content-Length: 222
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body><ns2:queryWeather xmlns:ns2="http://ws.jaxws.webservice.itheima.com/">
<arg0>北京</arg0></ns2:queryWeather>
</S:Body>
</S:Envelope>
响应:
......
HTTP/1.1 200 OK
Date: Thu, 20 Sep 2018 09:05:06 GMT
Transfer-encoding: chunked
Content-type: application/soap+xml; charset=utf-8
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body><ns2:queryWeatherResponse xmlns:ns2="http://ws.jaxws.webservice.itheima.com/">
<return>晴</return></ns2:queryWeatherResponse>
</S:Body>
</S:Envelope>
什么是webservice?
什么是远程调用技术?答:系统和系统之间的调用,从远程系统当中获取业务数据。
Webservice是web服务,他是用http传输SOAP协议数据的一种远程调用技术。
Webservice入门程序
服务端
第一步:创建SEI接口
第二步:创建SEI实现类,要在类上加入注解:@WebService,作用是标识这个类是服务类,要发布里面的public方法。
第三步:发布服务,Endpoint的publish方法,有2两个参数:1.服务地址 2.实现类实例
第四步:测试服务是否发布成功,通过阅读使用说明书,确定服务接口、方法、参数和返回值存在,说明服务发布成功。
WSDL地址:服务地址+”?wsdl”,http://127.0.0.1:54321/weather?wsdl
WSDL阅读方式,从下往上,service --> binding --> portType --> 其中有接口、方法、参数和返回值
客户端
第一步:使用wsimport命令生成客户端代码
第二步:根据使用说明书,使用客户端调用服务端
创建服务视图,视图是从service的name属性获取
获取服务实现类,从portType的name属性获取
调用查询方法,从portType下的operation标签的name属性获取
优缺点:
优点:发送方式采用http的post,http默认端口是80,所以跨防火墙。
数据封装使用XML格式,XML是跨平台,所以webservice可以跨平台。
Webservice支持面向对象开发。
缺点:使用XML封装数据,需要额外传输其他标签,性能较差。
Webservice应用场景
软件集成和复用
适用场景:
发布服务(对内/对外),不考虑性能,不考虑客户端类型,建议使用webservice
服务端已确定使用webservice,客户端只能使用webservice
不适用场景:
考虑性能时,不建议使用webservice
同构程序下,不建议使用webservice,比如:客户端服务端都是java开发,建议使用Java RMI,Java的RMI同样可以实现远程调用,而且性能比webservice好很多。
WSDL
定义:WSDL即Web服务描述语言,他是webservice服务端的使用说明书,它说明服务端接口、方法、参数和返回值,它是随服务发布成功,自动生成的,无需编写。
文档结构:
service
binding
portType
message
types
阅读方式:从下往上
SOAP
定义:SOAP即简单对象访问协议,它是使用http发送的XML格式的数据,跨平台、跨防火墙,它不是webservice的专有协议。
SOAP = http + xml
协议的格式:
必须项:envelope和body
非必须项:header和fault
SOAP1.1和1.2区别:
相同点:
都使用http的POST发送请求
协议的格式都相同:都有envelope标签和body标签
不同点:
Content-type数据类选不同:
SOAP1.1:text/xml; charset=utf-8;
SOAP1.2: application/soap+xml; charset=utf-8
命名空间不同:
SOAP1.1:http://schemas.xmlsoap.org/soap/envelope/
SOAP1.2:http://www.w3.org/2003/05/soap-envelope
UDDI:就是一个目录服务,提供搜索和注册功能,因为不常用,所以了解下就可以了。
公网服务地址:http://www.webxml.com.cn/zh_cn/index.aspx 进入该网站后,--> 点击 WEB服务 --> 点击 所需要的WSDL链接
wsimport命令介绍
wimport就是jdk提供的的一个工具,它的作用是:根据WSDL地址生成客户端代码。
wimport位置:D:\learn\Java\JDK\jdk1.8.0_161\bin
wsimport常用的参数:
-d,生成.class文件的,默认的参数。
-s,生成.java文件的。
-p,指定包名的,如果不加该参数,默认包名就是wsdl文档中的命名空间的倒序。
wsimport仅支持SOAP1.1客户端的生成。
调用公网手机号归属地查询服务 第一步:在对应的src目录下,使用wsimport命令生成客户端代码 wsimport -p com.itheima.mobile -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
第二步:阅读使用说明书,使用生成的客户端代码调用服务端,示例代码如下:
package com.itheima.mobile.client;
import com.itheima.mobile.MobileCodeWS;
import com.itheima.mobile.MobileCodeWSSoap;
/*
* 公网手机号查询客户端
*/
public class MobileClient {
public static void main(String[] args) {
// 创建服务视图
MobileCodeWS mobileCodeWS = new MobileCodeWS();
// 获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class);
// 调用查询方法
String reuslt = mobileCodeWSSoap.getMobileCodeInfo("1365131", null);
System.out.println(reuslt);
}
}
效果截图如下所示:
调用公网天气服务端查询 方法同上 调用公网手机号归属地查询服务 第一步:在对应的src目录下,使用wsimport命令生成客户端代码 wsimport -p com.itheima.weather -s . http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 但是此时会出现一个错误,如下图所示:
解决方法:将WeatherWS.xml
另存下来,然后删除掉标签:<s:element ref="s:schema" />
,然后保存,再次使用wsimport命令生成客户端代码,此时不会再报错了
wsimport -p com.itheima.weather -s . file:///C:\Users\Bruce\Desktop/WeatherWS.xml
第二步:阅读使用说明书,使用生成的客户端代码调用服务端,示例代码如下:
package com.itheima.weather.client;
import java.util.List;
import com.itheima.weather.ArrayOfString;
import com.itheima.weather.WeatherWS;
import com.itheima.weather.WeatherWSSoap;
public class WeatherClient {
public static void main(String[] args) {
// 创建服务视图
WeatherWS weatherWS = new WeatherWS();
// 获取服务实现类
WeatherWSSoap weatherWSSoap = weatherWS.getPort(WeatherWSSoap.class);
// 调用查询方法
ArrayOfString arrayOfString = weatherWSSoap.getWeather("北京", "");
List<String> list = arrayOfString.getString();
for (String str : list) {
System.out.println(str);
}
}
}
效果截图如下所示:
第一种生成客户端调用方式特点: 该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,不方便维护,所以仅用于测试。
即:service编程调用方式
package com.itheima.mobile.client;
import java.io.IOException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.itheima.mobile.MobileCodeWSSoap;
/*
* service编程实现服务端调用
*/
public class ServiceClient {
public static void main(String[] args) throws IOException {
// 创建WSDL的URL,注意:不是服务地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
// 创建服务名称,参数解释:1、namespaceURI 命名空间地址 2、localPart 服务视图名称
QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
// 创建服务视图,参数解释:1、wsdlDocumentLocation WSDL地址 2、serviceName 服务名称
Service service = Service.create(url, qname);
// 获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
// 调用查询方法
String reuslt = mobileCodeWSSoap.getMobileCodeInfo("13651311090", "");
System.out.println(reuslt);
}
}
第二种生成客户端调用方式特点: 该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式。
即:模拟客户端方式 开发步骤: 第一步:创建服务地址 第二步:打开一个通向服务地址的连接 第三步:设置参数 设置POST,POST必须大写,如果不大写,报如下异常:
如果不设置输入输出,会报如下异常:
第四步:组织SOAP数据,发送请求 第五步:接收服务端响应,打印 示例代码如下:
package com.itheima.mobile.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* HttpURLConnection调用方式实现服务端调用
*/
public class HttpClient {
public static void main(String[] args) throws IOException {
// 第一步:创建服务地址,注意:不是WSDL地址
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
// 第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 第三步:设置参数
// 设置POST,POST必须大写,如果不大写,报如下异常:
// 3.1、设置发送方式:POST必须大写
connection.setRequestMethod("POST");
// 3.2、设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
// 3.3、设置输入输出:因为默认新创建的connection没有读写权限
connection.setDoInput(true);
connection.setDoOutput(true);
// 如果不设置输入输出,会报如下异常:
// 第四步:组织SOAP数据,发送请求
String soapXML = getXML("13651311090");
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
// 第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) { // 表示服务端响应成功
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is); // 由于字节流容易出现乱码,所以把字节流转换为字符流
BufferedReader br = new BufferedReader(isr); // 为了高效,装饰一把,装饰设计模式
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
System.out.println(sb.toString());
// 从里往外关流
is.close();
isr.close();
br.close();
}
os.close();
}
public static String getXML(String phoneNum) {
String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body>"
+ "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
+ "<mobileCode>" + phoneNum + "</mobileCode>"
+ "<userID></userID>"
+ "</getMobileCodeInfo>"
+ "</soap:Body>"
+ "</soap:Envelope>";
return soapXML;
}
}
示例代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
function queryMobile() {
// 创建XMLHttpRequest对象
var xhr = new XMLHttpRequest();
// 打开连接
xhr.open("post", "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx", true);
// 设置数据类型
xhr.setRequestHeader("content-type", "text/xml;charset=utf-8");
// 设置回调函数
xhr.onreadystatechange=function() {
// 判断是否发送成功和判断服务端是否响应成功
if (4 == xhr.readyState && 200 == xhr.status) {
alert(xhr.responseText);
}
}
// 组织SOAP协议数据
var soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body>"
+ "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
+ "<mobileCode>" + document.getElementById("phoneNum").value + "</mobileCode>"
+ "<userID></userID>"
+ "</getMobileCodeInfo>"
+ "</soap:Body>"
+ "</soap:Envelope>";
alert(soapXML);
// 发送数据
xhr.send(soapXML);
}
</script>
</head>
<body>
手机号查询:<input type="text" id="phoneNum"/>
<input type="button" value="查询" onclick="javascript:queryMobile();"/>
</body>
</html>
详解如下:
WebService的注解都位于javax.jws包下:
@WebService-定义服务,在public class 上边
targetNamespace:指定命名空间
name:portType的名称
portName:port的名称
serviceName:服务名称
endpointInterface:SEI接口地址,如果一个服务类实现了多个接口,只需要发布一个接口的方法,可通过此注解指定要发布服务的接口。
@WebMethod-定义方法,在公开方法上边
operationName:方法名
exclude:设置为true表示此方法不是webservice方法,不发布它;反之则表示webservice方法,默认是false
@WebResult-定义返回值,在方法返回值前边
name:返回结果值的名称
@WebParam-定义参数,在方法参数前边
name:指定参数的名称
作用:
通过注解,可以更加形像的描述Web服务。对自动生成的wsdl文档进行修改,为使用者提供一个更加清晰的wsdl文档。
当修改了WebService注解之后,会影响客户端生成的代码。调用的方法名和参数名也发生了变化。
示例代码如下:
package com.itheima.webservice.jaxws.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService(
targetNamespace="http://service.cn.itcast",
name="WeatherWSSoap",
portName="WeatherWSSoapPort",
serviceName="WeatherWS"
) // @WebService表示该类是一个服务类,需要发布其中的public的方法
// @BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public class WeatherInterfaceImpl implements WeatherInterface {
@WebMethod(
operationName="getWeather",
exclude=false
)
@Override
public @WebResult(name="result")String queryWeather(@WebParam(name="cityName")String cityName) {
System.out.println("from client..." + cityName);
String weather = "晴";
return weather;
}
}