一.什么是Webservice
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序,其实webservice就是一种远程调用技术。
WebService的创建
服务接口
public interface WeatherInterface {
public String queryCity(String cityName);
}
服务实现类:
@WebService //@webservice 表示其该类是一个服务类,需要发布其中的public方法
public class WeatherInterfaceImpl implements WeatherInterface{
@Override
public String queryCity(String cityName) {
// TODO Auto-generated method stub
System.out.println("from client..."+cityName);
String weather = "晴";
return weather;
}
}
发布服务:
public class WeatherServer {
public static void main(String[] args) {
/**
* Endpoint发布服务,有两个参数
* 1.实现类 2.服务地址
*/
Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
}
}
测试是否发布成功:
通过阅读使用说明书,确定客户端调用的接口、方法、参数和返回值存在,证明服务发布成功。
WSDL地址:服务地址+”?wsdl”
WSDL阅读方式:从下往上
客户端:
开发步骤:首先通过wsimport -s .http://127.0.0.1:12345/weather?wsdl生成客户端代码
wsimport -s ./ -p com.study.client http://127.0.0.1:12345/weather?wsdl
根据使用说明书,在客户端调用服务端代码
第一步:创建服务视图,视图是从service标签的name属性获取
第二步:获取服务实现类,实现类从portType的name属性获取
第三步:获取查询方法,从portType的operation标签获取
public static void main(String[] args) {
//创建服务视图
WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
//获取服务实现类
WeatherInterfaceImpl weatherInterfaceImpl = weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class);
//获取查询方法
String result = weatherInterfaceImpl.queryCity("北京");
System.out.println(result);
}
webService的核心技术
xml
XML是WebService平台中表示数据的格式
WSDL(web services description language) 是一个基于xml的描述语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都能理解的标准格式。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。一些最新的开发工具既能根据你的Web service生成WSDL文档,又能导入WSDL文档,生成调用相应WebService的代理类代码,说白了,就是指导我们在客户端怎么调用服务的文档描述.
文档主要包含如下部分:
<service> 服务视图,webservice的服务结点,它包括了服务端点
<binding> 为每个服务端点定义消息格式和协议细节
<portType> 服务端点,描述 web service可被执行的操作方法,以及相关的消息,通过binding指向portType
<message> 定义一个操作(方法)的数据参数(可有多个参数)
<types> 定义 web service 使用的全部数据类型
SOAP协议
soap是简单对象访问协议,使用http发送xml数据的协议,可以跨防火墙,跨平台。并且增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用Web Service。
即soap = http+xml;
协议格式:
必需有Envelope 元素,此元素将整个 XML 文档标识为一条 SOAP 消息
可选的 Header 元素,包含头部信息
必需有Body元素,包含所有的调用和响应信息
可选的 Fault 元素,提供有关在处理此消息所发生错误的信息
soap1.1 和soap1.2的区别
相同点:
请求发送方式相同:都是使用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 是一种目录服务,企业可以使用它对 Web services 进行注册和搜索。UDDI,英文为"Universal Description, Discovery and Integration",可译为“通用描述、发现与集成服务”
webservice的使用场景
软件的集成和复用,即一个服务可以使多个系统调用
适用场景
发布一个服务(对内/对外),不考虑客户端类型,不考虑性能,建议使用webservice
服务端已经确定使用webservice,客户端不能选择,必须使用webservice
不适用场景
考虑性能时不建议使用webservice
同构程序下不建议使用webservice,比如java 用RMI,不需要翻译成XML的数据
webservice的优缺点
发送方式采用http的post发送,http的默认端口是80,防火墙默认不拦截80,所以跨防火墙
采用XML格式封装数据,XML是跨平台的,所以webservice也可以跨平台。
Webservice支持面向对象
缺点:
采用XML格式封装数据,所以在传输过程中,要传输额外的标签,随着SOAP协议的不断完善,标签越来越大,导致webservice性能下降
webservice的四种客户端调用:
调用天气电话查询的webservice接口
第一种如上所示:
第二种:service编程调用
public class ServiceClient {
public static void main(String[] args) throws IOException {
//创建WSDL的URL,注意不是服务地址
URL url = new URL("http://webservice.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 - 服务名称
Serviceservice= Service.create(url, qname);
//获取服务实现类
MobileCodeWSSoapmobileCodeWSSoap= service.getPort(MobileCodeWSSoap.class);
//调用查询方法
Stringresult = mobileCodeWSSoap.getMobileCodeInfo("1866666666", "");
System.out.println(result);
}
}
第三种:HttpURLConnection调用方式
开发步骤:
第一步:创建服务地址
第二步:打开一个通向服务地址的连接
第三步:设置参数
设置POST,POST必须大写,如果不大写,会报异常
第四步:组织SOAP数据,发送请求
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();
//第三步:设置参数
//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("15226466316");
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 staticString getXML(String phoneNum){
StringsoapXML= "<?xml version=\"1.0\"encoding=\"utf-8\"?>"
+"<soap:Envelopexmlns: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>"
+"<getMobileCodeInfoxmlns=\"http://WebXml.com.cn/\">"
+"<mobileCode>"+phoneNum+"</mobileCode>"
+"<userID></userID>"
+"</getMobileCodeInfo>"
+"</soap:Body>"
+"</soap:Envelope>";
return soapXML;
}
第四种 ajax方式
ajax请求方式和普通的ajax一样,只是发送的数据时xml格式的数据.
webservice的深入开发
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:指定参数的名称