`
zhpengfei0915
  • 浏览: 65493 次
  • 性别: Icon_minigender_1
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

Web Service概述

 
阅读更多
Web Service的定义
W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计。Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用,来执行远程系统的请求服务。

这里我们从一个程序员的视角来观察web service。在传统的程序编码中,存在这各种的函数方法调用。通常,我们知道一个程序模块M中的方法A,向其发出调用请求,并传入A方法需要的参数P,方法A执行完毕后,返回处理结果R。这种函数或方法调用通常发生在同一台机器上的同一程序语言环境下。现在的我们需要一种能够在不同计算机间的不同语言编写的应用程序系统中,通过网络通讯实现函数和方法调用的能力,而Web service正是应这种需求而诞生的。

最普遍的一种说法就是,Web Service = SOAP + HTTP + WSDL。其中,SOAP Simple Object Access Protocol)协议是web service的主体,它通过HTTP或者SMTP等应用层协议进行通讯,自身使用XML文件来描述程序的函数方法和参数信息,从而完成不同主机的异构系统间的计算服务处理。这里的WSDL(Web Services Description Language)web 服务描述语言也是一个XML文档,它通过HTTP向公众发布,公告客户端程序关于某个具体的 Web service服务的URL信息、方法的命名,参数,返回值等。
下面,我们先来熟悉一下SOAP协议,看看它是如何描述程序中的函数方法、参数及结果对象的。

SOAP协议简介

什么是SOAP
SOAP 指简单对象访问协议,它是一种基于XML的消息通讯格式,用于网络上,不同平台,不同语言的应用程序间的通讯。可自定义,易于扩展。一条 SOAP 消息就是一个普通的 XML 文档,包含下列元素:
• Envelope 元素,标识XML 文档一条 SOAP 消息
• Header 元素,包含头部信息的XML标签
• Body 元素,包含所有的调用和响应的主体信息的标签
• Fault 元素,错误信息标签。

以上的元素都在 SOAP的命名空间http://www.w3.org/2001/12/soap-envelope中声明;
SOAP的语法规则
• SOAP 消息必须用 XML 来编码
• SOAP 消息必须使用 SOAP Envelope 命名空间
• SOAP 消息必须使用 SOAP Encoding 命名空间
• SOAP 消息不能包含 DTD 引用
• SOAP 消息不能包含 XML 处理指令

SOAP 消息的基本结构
Java代码
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Header>
  6. ...
  7. ...
  8. </soap:Header>
  9. <soap:Body>
  10. ...
  11. ...
  12. <soap:Fault>
  13. ...
  14. ...
  15. </soap:Fault>
  16. </soap:Body>
  17. </soap:Envelope>
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Header>
  6. ...
  7. ...
  8. </soap:Header>
  9. <soap:Body>
  10. ...
  11. ...
  12. <soap:Fault>
  13. ...
  14. ...
  15. </soap:Fault>
  16. </soap:Body>
  17. </soap:Envelope>


SOAP Envelope 元素
Envelope 元素是 SOAP 消息的根元素。它指明 XML 文档是一个SOAP 消息。它的属性 xmlns:soap的值必须是http://www.w3.org/2001/12/soap-envelope。
 encodingStyle 属性,语法:soap:encodingStyle="URI"
encodingStyle 属性用于定义文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。
Java代码 复制代码
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. ...
  6. Messageinformationgoeshere
  7. ...
  8. </soap:Envelope>
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. ...
  6. Messageinformationgoeshere
  7. ...
  8. </soap:Envelope>


SOAP Header 元素

  • actor 属性,语法soap:actor="URI"

通过沿着消息路径经过不同的端点,SOAP 消息可从某个发送者传播到某个接收者。并非 SOAP 消息的所有部分均打算传送到 SOAP 消息的最终端点,不过,另一个方面,也许打算传送给消息路径上的一个或多个端点。SOAP 的 actor 属性可被用于将 Header 元素寻址到一个特定的端点。

  • mustUnderstand 属性 ,语法soap:mustUnderstand="0|1"

SOAP 的 mustUnderstand 属性可用于标识标题项对于要对其进行处理的接收者来说是强制的还是可选的。假如您向 Header 元素的某个子元素添加了 "mustUnderstand="1",则要求处理此头部的接收者必须认可此元素。
Java代码
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Header>
  6. <m:Trans
  7. xmlns:m="http://www.jsoso.net/transaction/"
  8. soap:mustUnderstand="1"
  9. soap:actor="http://www.w3schools.com/appml/“>234</m:Trans>
  10. </soap:Header>
  11. ...
  12. ...
  13. </soap:Envelope>
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Header>
  6. <m:Trans
  7. xmlns:m="http://www.jsoso.net/transaction/"
  8. soap:mustUnderstand="1"
  9. soap:actor="http://www.w3schools.com/appml/“>234</m:Trans>
  10. </soap:Header>
  11. ...
  12. ...
  13. </soap:Envelope>


SOAP Body 元素
必需的 SOAP Body 元素可包含打算传送到消息最终端点的实际 SOAP 消息。Body元素中既可以包含SOAP定义的命名空间中的元素,如Fault,也可以是用户的应用程序自定义的元素。以下是一个用户定义的请求:
Java代码
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Body>
  6. <m:GetPricexmlns:m="http://www.jsoso.net/prices">
  7. <m:Item>Apples</m:Item>
  8. </m:GetPrice>
  9. </soap:Body>
  10. </soap:Envelope>
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Body>
  6. <m:GetPricexmlns:m="http://www.jsoso.net/prices">
  7. <m:Item>Apples</m:Item>
  8. </m:GetPrice>
  9. </soap:Body>
  10. </soap:Envelope>

上面的例子请求苹果的价格。请注意,上面的 m:GetPrice 和 Item 元素是应用程序专用的元素。它们并不是 SOAP 标准的一部分。而对应的 SOAP 响应应该类似这样:
Java代码
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Body>
  6. <m:GetPriceResponsexmlns:m="http://www.jsoso.net/prices">
  7. <m:Price>1.90</m:Price>
  8. </m:GetPriceResponse>
  9. </soap:Body>
  10. </soap:Envelope>
  1. <?xmlversion="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5. <soap:Body>
  6. <m:GetPriceResponsexmlns:m="http://www.jsoso.net/prices">
  7. <m:Price>1.90</m:Price>
  8. </m:GetPriceResponse>
  9. </soap:Body>
  10. </soap:Envelope>


SOAP Fault 元素
Fault 元素表示 SOAP的错误消息。它必须是 Body 元素的子元素,且在一条 SOAP 消息中,Fault 元素只能出现一次。Fault 元素拥有下列子元素:

常用的SOAP Fault Codes


HTTP协议中的SOAP 实例
下面的例子中,一个 GetStockPrice 请求被发送到了服务器。此请求有一个 StockName 参数,而在响应中则会返回一个 Price 参数。此功能的命名空间被定义在此地址中: "http://www.jsoso.net/stock"
  • SOAP 请求:(注意HTTP的Head属性)

Java代码
  1. POST/InStockHTTP/1.1
  2. Host:www.jsoso.net
  3. Content-Type:application/soap+xml;charset=utf-8
  4. Content-Length:XXX
  5. <?xmlversion="1.0"?>
  6. <soap:Envelope
  7. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  8. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  9. <soap:Bodyxmlns:m="http://www.jsoso.net/stock">
  10. <m:GetStockPrice>
  11. <m:StockName>IBM</m:StockName>
  12. </m:GetStockPrice>
  13. </soap:Body>
  14. </soap:Envelope>
  1. POST/InStockHTTP/1.1
  2. Host:www.jsoso.net
  3. Content-Type:application/soap+xml;charset=utf-8
  4. Content-Length:XXX
  5. <?xmlversion="1.0"?>
  6. <soap:Envelope
  7. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  8. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  9. <soap:Bodyxmlns:m="http://www.jsoso.net/stock">
  10. <m:GetStockPrice>
  11. <m:StockName>IBM</m:StockName>
  12. </m:GetStockPrice>
  13. </soap:Body>
  14. </soap:Envelope>


  • SOAP 响应:(注意HTTP的Head属性)

Java代码
  1. HTTP/1.1200OK
  2. Content-Type:application/soap+xml;charset=utf-8
  3. Content-Length:XXX
  4. <?xmlversion="1.0"?>
  5. <soap:Envelope
  6. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  7. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  8. <soap:Bodyxmlns:m="http://www.jsoso.net/stock">
  9. <m:GetStockPriceResponse>
  10. <m:Price>34.5</m:Price>
  11. </m:GetStockPriceResponse>
  12. </soap:Body>
  13. </soap:Envelope>
  1. HTTP/1.1200OK
  2. Content-Type:application/soap+xml;charset=utf-8
  3. Content-Length:XXX
  4. <?xmlversion="1.0"?>
  5. <soap:Envelope
  6. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  7. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  8. <soap:Bodyxmlns:m="http://www.jsoso.net/stock">
  9. <m:GetStockPriceResponse>
  10. <m:Price>34.5</m:Price>
  11. </m:GetStockPriceResponse>
  12. </soap:Body>
  13. </soap:Envelope>


HTTP协议中的SOAP RPC工作流程


WSDL简介
介绍过了SOAP,让我们关注Web Service中另外一个重要的组成WSDL。
WSDL的主要文档元素

WSDL文档可以分为两部分。顶部分由抽象定义组成,而底部分则由具体描述组成。抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随机器或语言而变的元素。这就定义了一系列服务,截然不同的应用都可以实现。具体部分,如数据的序列化则归入底部分,因为它包含具体的定义。在上述的文档元素中,<types>、<message>、<portType>属于抽象定义层,<binding>、<service>属于具体定义层。所有的抽象可以是单独存在于别的文件中,也可以从主文档中导入。

WSDL文档的结构实例解析
下面我们将通过一个实际的WSDL文档例子来详细说明各标签的作用及关系。
Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <definitions
  3. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  4. xmlns:tns="http://www.jsoso.com/wstest"
  5. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  6. xmlns="http://schemas.xmlsoap.org/wsdl/"
  7. targetNamespace="http://www.jsoso.com/wstest"
  8. name="Example">
  9. <types>
  10. <xsd:schema>
  11. <xsd:import
  12. namespace="http://www.jsoso.com/wstest"
  13. schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
  14. </xsd:schema>
  15. </types>
  16. <messagename="toSayHello">
  17. <partname="userName"type="xsd:string"></part>
  18. </message>
  19. <messagename="toSayHelloResponse">
  20. <partname="returnWord"type="xsd:string"></part>
  21. </message>
  22. <messagename="sayHello">
  23. <partname="person"type="tns:person"></part>
  24. <partname="arg1"type="xsd:string"></part>
  25. </message>
  26. <messagename="sayHelloResponse">
  27. <partname="personList"type="tns:personArray"></part>
  28. </message>
  29. <messagename="HelloException">
  30. <partname="fault"element="tns:HelloException"></part>
  31. </message>
  32. <portTypename="Example">
  33. <operationname="toSayHello"parameterOrder="userName">
  34. <inputmessage="tns:toSayHello"></input>
  35. <outputmessage="tns:toSayHelloResponse"></output>
  36. </operation>
  37. <operationname="sayHello"parameterOrder="personarg1">
  38. <inputmessage="tns:sayHello"></input>
  39. <outputmessage="tns:sayHelloResponse"></output>
  40. <faultmessage="tns:HelloException"name="HelloException"></fault>
  41. </operation>
  42. </portType>
  43. <bindingname="ExamplePortBinding"type="tns:Example">
  44. <soap:binding
  45. transport="http://schemas.xmlsoap.org/soap/http"
  46. style="rpc"></soap:binding>
  47. <operationname="toSayHello">
  48. <soap:operationsoapAction="sayHello"></soap:operation>
  49. <input>
  50. <soap:bodyuse="literal"
  51. namespace="http://www.jsoso.com/wstest"></soap:body>
  52. </input>
  53. <output>
  54. <soap:bodyuse="literal"
  55. namespace="http://www.jsoso.com/wstest"></soap:body>
  56. </output>
  57. </operation>
  58. <operationname="sayHello">
  59. <soap:operationsoapAction="sayHello"></soap:operation>
  60. <input>
  61. <soap:bodyuse="literal"
  62. namespace="http://www.jsoso.com/wstest"></soap:body>
  63. </input>
  64. <output>
  65. <soap:bodyuse="literal"
  66. namespace="http://www.jsoso.com/wstest"></soap:body>
  67. </output>
  68. <faultname="HelloException">
  69. <soap:faultname="HelloException"use="literal"></soap:fault>
  70. </fault>
  71. </operation>
  72. </binding>
  73. <servicename="Example">
  74. <portname="ExamplePort"binding="tns:ExamplePortBinding">
  75. <soap:addresslocation="http://localhost:8080/hello"></soap:address>
  76. </port>
  77. </service>
  78. </definitions>
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <definitions
  3. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  4. xmlns:tns="http://www.jsoso.com/wstest"
  5. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  6. xmlns="http://schemas.xmlsoap.org/wsdl/"
  7. targetNamespace="http://www.jsoso.com/wstest"
  8. name="Example">
  9. <types>
  10. <xsd:schema>
  11. <xsd:import
  12. namespace="http://www.jsoso.com/wstest"
  13. schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
  14. </xsd:schema>
  15. </types>
  16. <messagename="toSayHello">
  17. <partname="userName"type="xsd:string"></part>
  18. </message>
  19. <messagename="toSayHelloResponse">
  20. <partname="returnWord"type="xsd:string"></part>
  21. </message>
  22. <messagename="sayHello">
  23. <partname="person"type="tns:person"></part>
  24. <partname="arg1"type="xsd:string"></part>
  25. </message>
  26. <messagename="sayHelloResponse">
  27. <partname="personList"type="tns:personArray"></part>
  28. </message>
  29. <messagename="HelloException">
  30. <partname="fault"element="tns:HelloException"></part>
  31. </message>
  32. <portTypename="Example">
  33. <operationname="toSayHello"parameterOrder="userName">
  34. <inputmessage="tns:toSayHello"></input>
  35. <outputmessage="tns:toSayHelloResponse"></output>
  36. </operation>
  37. <operationname="sayHello"parameterOrder="personarg1">
  38. <inputmessage="tns:sayHello"></input>
  39. <outputmessage="tns:sayHelloResponse"></output>
  40. <faultmessage="tns:HelloException"name="HelloException"></fault>
  41. </operation>
  42. </portType>
  43. <bindingname="ExamplePortBinding"type="tns:Example">
  44. <soap:binding
  45. transport="http://schemas.xmlsoap.org/soap/http"
  46. style="rpc"></soap:binding>
  47. <operationname="toSayHello">
  48. <soap:operationsoapAction="sayHello"></soap:operation>
  49. <input>
  50. <soap:bodyuse="literal"
  51. namespace="http://www.jsoso.com/wstest"></soap:body>
  52. </input>
  53. <output>
  54. <soap:bodyuse="literal"
  55. namespace="http://www.jsoso.com/wstest"></soap:body>
  56. </output>
  57. </operation>
  58. <operationname="sayHello">
  59. <soap:operationsoapAction="sayHello"></soap:operation>
  60. <input>
  61. <soap:bodyuse="literal"
  62. namespace="http://www.jsoso.com/wstest"></soap:body>
  63. </input>
  64. <output>
  65. <soap:bodyuse="literal"
  66. namespace="http://www.jsoso.com/wstest"></soap:body>
  67. </output>
  68. <faultname="HelloException">
  69. <soap:faultname="HelloException"use="literal"></soap:fault>
  70. </fault>
  71. </operation>
  72. </binding>
  73. <servicename="Example">
  74. <portname="ExamplePort"binding="tns:ExamplePortBinding">
  75. <soap:addresslocation="http://localhost:8080/hello"></soap:address>
  76. </port>
  77. </service>
  78. </definitions>

由于上面的事例XML较长,我们将其逐段分解讲解

WSDL文档的根元素:<definitions>
Java代码
  1. <definitions
  2. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  3. xmlns:tns="http://www.jsoso.com/wstest"
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  5. xmlns="http://schemas.xmlsoap.org/wsdl/"
  6. targetNamespace="http://www.jsoso.com/wstest"
  7. name="Example">
  8. ……
  9. ……
  10. </definitions>
  1. <definitions
  2. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  3. xmlns:tns="http://www.jsoso.com/wstest"
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  5. xmlns="http://schemas.xmlsoap.org/wsdl/"
  6. targetNamespace="http://www.jsoso.com/wstest"
  7. name="Example">
  8. ……
  9. ……
  10. </definitions>

<definitions>定义了文档中用到的各个xml元素的namespace缩写,也界定了本文档自己的targetNamespace="http://www.jsoso.com/wstest",这意味着其它的XML要引用当前XML中的元素时,要声明这个namespace。注意xmlns:tns="http://www.jsoso.com/wstest"这个声明,它标示了使用tns这个前缀指向自身的命名空间。

引用
WSDL文档数据类型定义元素:<types>

Java代码
  1. <types>
  2. <xsd:schema>
  3. <xsd:import
  4. namespace="http://www.jsoso.com/wstest"
  5. schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
  6. </xsd:schema>
  7. </types>
  1. <types>
  2. <xsd:schema>
  3. <xsd:import
  4. namespace="http://www.jsoso.com/wstest"
  5. schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
  6. </xsd:schema>
  7. </types>

<types>标签定义了当前的WSDL文档用到的数据类型。要说明的是,为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。这些数据类型用来定义web service方法的参数和返回指。对于通用的原生数据类型如:integer , boolean , char , float等,在W3C的标准文档http://www.w3.org/2001/XMLSchema中已经做了定义。这里我们要引入的schema定义schemaLocation="http://localhost:8080/hello?xsd=1"是我们自定义的Java对象类型。

WSDL文档消息体定义元素:< message >
Java代码
  1. <messagename="toSayHello">
  2. <partname="userName"type="xsd:string"></part>
  3. </message>
  4. <messagename="toSayHelloResponse">
  5. <partname="returnWord"type="xsd:string"></part>
  6. </message>
  7. <messagename="sayHello">
  8. <partname="person"type="tns:person"></part>
  9. <partname="arg1"type="xsd:string"></part>
  10. </message>
  11. <messagename="sayHelloResponse">
  12. <partname="personList"type="tns:personArray"></part>
  13. </message>
  14. <messagename="HelloException">
  15. <partname="fault"element="tns:HelloException"></part>
  16. </message>
  1. <messagename="toSayHello">
  2. <partname="userName"type="xsd:string"></part>
  3. </message>
  4. <messagename="toSayHelloResponse">
  5. <partname="returnWord"type="xsd:string"></part>
  6. </message>
  7. <messagename="sayHello">
  8. <partname="person"type="tns:person"></part>
  9. <partname="arg1"type="xsd:string"></part>
  10. </message>
  11. <messagename="sayHelloResponse">
  12. <partname="personList"type="tns:personArray"></part>
  13. </message>
  14. <messagename="HelloException">
  15. <partname="fault"element="tns:HelloException"></part>
  16. </message>

<message>元素定义了web service函数的参数。<message>元素中的每个<part>子元素都和某个参数相符。输入参数在<message>元素中定义,与输出参数相隔离,输出参数有自己的<message>元素。兼作输入、输出的参数在输入输出的<message>元素中有它们相应的<part>元素。输出<message>元素以"Response"结尾,对Java而言方法得返回值就对应一个输出的<message>。每个<part>元素都有名字和类型属性,就像函数的参数有参数名和参数类型。

在上面的文档中有两个输入参数、两个输出参数和一个错误参数(对应Java中的Exception)。

 输入参数<message>的name属性分别命名为toSayHello,sayHello。
toSayHello对应输入参数userName,参数类型为xsd:string,在Java语言中就是String;
sayHello对应两个输入参数person和arg1,类型为tns:person和xsd:string。这里tns:person类型就是引用了< types >标签中的类型定义。

 输出参数<message>的name属性分别命名为toSayHelloResponse和sayHelloResponse。
这个名称和输入参数的<message>标签name属性对应,在其后面加上Response尾缀。
toSayHelloResponse对应的返回值是returnWord,参数类型为xsd:string;
sayHelloResponse对应的返回值是personList,参数类型为tns:personArray(自定义类型);

 错误参数<message>的name属性为HelloException。
它的<part>子标签element而不是type来定义类型。

以上的message标签的name属性通常使用web service函数方法名作为参照,错误参数标签则使用异常类名为参照。标签中的参数名称,即part子元素的name属性是可自定义的(下一章节详细说明)。message标签的参数类型将引用types标签的定义。

WSDL文档函数体定义元素:< portType >
Java代码
  1. <portTypename="Example">
  2. <operationname="toSayHello"parameterOrder="userName">
  3. <inputmessage="tns:toSayHello"></input>
  4. <outputmessage="tns:toSayHelloResponse"></output>
  5. </operation>
  6. <operationname="sayHello"parameterOrder="personarg1">
  7. <inputmessage="tns:sayHello"></input>
  8. <outputmessage="tns:sayHelloResponse"></output>
  9. <faultmessage="tns:HelloException"name="HelloException"></fault>
  10. </operation>
  11. </portType>
  1. <portTypename="Example">
  2. <operationname="toSayHello"parameterOrder="userName">
  3. <inputmessage="tns:toSayHello"></input>
  4. <outputmessage="tns:toSayHelloResponse"></output>
  5. </operation>
  6. <operationname="sayHello"parameterOrder="personarg1">
  7. <inputmessage="tns:sayHello"></input>
  8. <outputmessage="tns:sayHelloResponse"></output>
  9. <faultmessage="tns:HelloException"name="HelloException"></fault>
  10. </operation>
  11. </portType>

<portType> 元素是最重要的 WSDL 元素。它可描述一个 web service、可被执行的操作,以及相关的消息。portType的name属性对应Java中的一个服务类的类名。<portType> 元素使用其子元素< operation>描述一个web service的服务方法。

在<operation>元素中,name属性表示服务方法名,parameterOrder属性表示方法的参数顺序,使用空格符分割多个参数,如:“parameterOrder="person arg1”。<operation>元素的子标签<input>表示输入参数说明,它引用<message>标签中的输入参数。<output>表示输出参数说明,它引用<message>标签中的输出参数。<fault>标签在Java方法中的特别用来表示异常(其它语言有对应的错误处理机制),它引用<message>标签中的错误参数。

WSDL绑定实现定义元素:< binding >
Java代码
  1. <bindingname="ExamplePortBinding"type="tns:Example">
  2. <soap:binding
  3. transport="http://schemas.xmlsoap.org/soap/http"
  4. style="rpc"></soap:binding>
  5. <operationname="toSayHello">
  6. <soap:operationsoapAction="sayHello"></soap:operation>
  7. <input>
  8. <soap:bodyuse="literal"
  9. namespace="http://www.jsoso.com/wstest"></soap:body>
  10. </input>
  11. <output>
  12. <soap:bodyuse="literal"
  13. namespace="http://www.jsoso.com/wstest"></soap:body>
  14. </output>
  15. </operation>
  16. <operationname="sayHello">
  17. <soap:operationsoapAction="sayHello"></soap:operation>
  18. <input>
  19. <soap:bodyuse="literal"
  20. namespace="http://www.jsoso.com/wstest"></soap:body>
  21. </input>
  22. <output>
  23. <soap:bodyuse="literal"
  24. namespace="http://www.jsoso.com/wstest"></soap:body>
  25. </output>
  26. <faultname="HelloException">
  27. <soap:faultname="HelloException"use="literal"></soap:fault>
  28. </fault>
  29. </operation>
  30. </binding>
  1. <bindingname="ExamplePortBinding"type="tns:Example">
  2. <soap:binding
  3. transport="http://schemas.xmlsoap.org/soap/http"
  4. style="rpc"></soap:binding>
  5. <operationname="toSayHello">
  6. <soap:operationsoapAction="sayHello"></soap:operation>
  7. <input>
  8. <soap:bodyuse="literal"
  9. namespace="http://www.jsoso.com/wstest"></soap:body>
  10. </input>
  11. <output>
  12. <soap:bodyuse="literal"
  13. namespace="http://www.jsoso.com/wstest"></soap:body>
  14. </output>
  15. </operation>
  16. <operationname="sayHello">
  17. <soap:operationsoapAction="sayHello"></soap:operation>
  18. <input>
  19. <soap:bodyuse="literal"
  20. namespace="http://www.jsoso.com/wstest"></soap:body>
  21. </input>
  22. <output>
  23. <soap:bodyuse="literal"
  24. namespace="http://www.jsoso.com/wstest"></soap:body>
  25. </output>
  26. <faultname="HelloException">
  27. <soap:faultname="HelloException"use="literal"></soap:fault>
  28. </fault>
  29. </operation>
  30. </binding>

<binding>标签是完整描述协议、序列化和编码的地方,<types>,<message>和<portType>标签处理抽象的数据内容,而<binding>标签是处理数据传输的物理实现。
<binding>标签把前三部分的抽象定义具体化。

首先<binding>标签使用<soap:binding>的transport和style属性定义了Web Service的通讯协议HTTP和SOAP的请求风格RPC。其次<operation>子标签将portType中定义的operation同SOAP的请求绑定,定义了操作名称soapAction,输出输入参数和异常的编码方式及命名空间。

WSDL服务地址绑定元素:< service >
Java代码
  1. <servicename="Example">
  2. <portname="ExamplePort"binding="tns:ExamplePortBinding">
  3. <soap:addresslocation="http://localhost:8080/hello"></soap:address>
  4. </port>
  5. </service>
  1. <servicename="Example">
  2. <portname="ExamplePort"binding="tns:ExamplePortBinding">
  3. <soap:addresslocation="http://localhost:8080/hello"></soap:address>
  4. </port>
  5. </service>

service是一套<port>元素。在一一对应形式下,每个<port>元素都和一个location关联。如果同一个<binding>有多个<port>元素与之关联,可以使用额外的URL地址作为替换。

一个WSDL文档中可以有多个<service>元素,而且多个<service>元素十分有用,其中之一就是可以根据目标URL来组织端口。在一个WSDL文档中,<service>的name属性用来区分不同的service。在同一个service中,不同端口,使用端口的"name"属性区分。

这一章节,我们简单的描述了WSDL对SOAP协议的支持,以及在Web Service中的作用。在接下来的章节中,我们将学习如何使用Java6.0的Annotation标签来定义和生成对应的WSDL。

JavaSE6.0下的Web Service
从JavaSE6.0开始,Java引入了对Web Service的原生支持。我们只需要简单的使用Java的Annotation标签即可将标准的Java方法发布成Web Service。(PS:Java Annotation资料请参考 JDK5.0 Annotation学习笔记(一)

但不是所有的Java类都可以发布成Web Service。Java类若要成为一个实现了Web Service的bean,它需要遵循下边这些原则:
  •  这个类必须是public类
  •  这些类不能是final的或者abstract
  •  这个类必须有一个公共的默认构造函数
  •  这个类绝对不能有finalize()方法

下面我们将通过一个具体的Java Web Service代码例子,配合上述的WSDL文件,讲述如何编写JavaSE6.0的原生Web Service应用。

完整的Java Web Service类代码

Java代码
  1. packageorg.jsoso.jws.server;
  2. importjava.util.ArrayList;
  3. importjavax.jws.WebMethod;
  4. importjavax.jws.WebParam;
  5. importjavax.jws.WebResult;
  6. importjavax.jws.WebService;
  7. importjavax.jws.WebParam.Mode;
  8. importjavax.jws.soap.SOAPBinding;
  9. /
  10. *提供WebService服务的类
  11. */
  12. @WebService(name="Example",targetNamespace="http://www.jsoso.com/wstest",serviceName="Example")
  13. @SOAPBinding(style=SOAPBinding.Style.RPC)
  14. publicclassExample{
  15. privateArrayList<Person>persons=newArrayList<Person>();;
  16. /**
  17. *
  18. *返回一个字符串
  19. *@paramuserName
  20. *@return
  21. */
  22. @WebMethod(operationName="toSayHello",action="sayHello",exclude=false)
  23. @WebResult(name="returnWord")//自定义该方法返回值在WSDL中相关的描述
  24. publicStringsayHello(@WebParam(name="userName")StringuserName){
  25. return"Hello:"+userName;
  26. }
  27. /**
  28. *webservices方法的返回值与参数的类型不能为接口
  29. *@paramperson
  30. *@return
  31. *@throwsHelloException
  32. */
  33. @WebMethod(operationName="sayHello",action="sayHello")
  34. @WebResult(partName="personList")
  35. publicPerson[]sayHello(@WebParam(partName="person",mode=Mode.IN)Personperson,
  36. StringuserName)throwsHelloException{
  37. if(person==null||person.getName()==null){
  38. thrownewHelloException("说hello出错,对像为空。。");
  39. }
  40. System.out.println(person.getName()+"对"+userName+"说:Hello,我今年"+person.getAge()+"岁");
  41. persons.add(person);
  42. returnpersons.toArray(newPerson[0]);
  43. }
  44. }
  1. packageorg.jsoso.jws.server;
  2. importjava.util.ArrayList;
  3. importjavax.jws.WebMethod;
  4. importjavax.jws.WebParam;
  5. importjavax.jws.WebResult;
  6. importjavax.jws.WebService;
  7. importjavax.jws.WebParam.Mode;
  8. importjavax.jws.soap.SOAPBinding;
  9. /
  10. *提供WebService服务的类
  11. */
  12. @WebService(name="Example",targetNamespace="http://www.jsoso.com/wstest",serviceName="Example")
  13. @SOAPBinding(style=SOAPBinding.Style.RPC)
  14. publicclassExample{
  15. privateArrayList<Person>persons=newArrayList<Person>();;
  16. /**
  17. *
  18. *返回一个字符串
  19. *@paramuserName
  20. *@return
  21. */
  22. @WebMethod(operationName="toSayHello",action="sayHello",exclude=false)
  23. @WebResult(name="returnWord")//自定义该方法返回值在WSDL中相关的描述
  24. publicStringsayHello(@WebParam(name="userName")StringuserName){
  25. return"Hello:"+userName;
  26. }
  27. /**
  28. *webservices方法的返回值与参数的类型不能为接口
  29. *@paramperson
  30. *@return
  31. *@throwsHelloException
  32. */
  33. @WebMethod(operationName="sayHello",action="sayHello")
  34. @WebResult(partName="personList")
  35. publicPerson[]sayHello(@WebParam(partName="person",mode=Mode.IN)Personperson,
  36. StringuserName)throwsHelloException{
  37. if(person==null||person.getName()==null){
  38. thrownewHelloException("说hello出错,对像为空。。");
  39. }
  40. System.out.println(person.getName()+"对"+userName+"说:Hello,我今年"+person.getAge()+"岁");
  41. persons.add(person);
  42. returnpersons.toArray(newPerson[0]);
  43. }
  44. }


Annotation 1@WebService(name="Example", targetNamespace="http://www.jsoso.com/wstest", serviceName="Example")
@WebService标签主要将类暴露为WebService,其中targetNamespace属性定义了自己的命名空间,serviceName则定义了< definitions >标签和<service>标签的name属性。

Annotation 2:@SOAPBinding(style=SOAPBinding.Style.RPC)
@SOAPBinding标签定义了WSDL文档中SOAP的消息协议,其中style属性对应SOAP的文档类型,可选的有RPC和DOCUMENT

Annotation 3:@WebMethod(operationName="toSayHello",action="sayHello",exclude=false)
@WebMethod定义Web Service运作的方法,
属性action 对应操作的活动 ,如<soap:operation soapAction="sayHello" />
属性operationName匹配的wsdl:operation 的名称,如<operation name="toSayHello" parameterOrder="userName">
属性exclude 用于阻止将某一继承方法公开为web服务,默认为false

Annotation 4:@WebResult(name="returnWord")
@ WebResult定义方法返回值得名称,如<part name="returnWord" type="xsd:string" />

Annotation 5:@WebParam(partName="person", mode=Mode.IN
@WebParam定义方法的参数名称,如<part name="person" type="tns:person" />,其中mode属性表示参数的流向,可选值有IN / OUT / INOUT

这里要着重说明的是,上述Web Service类的sayHello方法中,带有HelloException这个异常声明,造成该服务类不能直接发布成Web Service。需要使用wsgen工具为其生存异常Bean。关于wsgen工具的使用,请参考wsgen与wsimport命令说明

发布一个的Java Web Service
在完成了上述的Web Service Annotation注释后,我们使用wsgen工具为其进行服务资源文件的构造(这里主要是生成一个名为org.jsoso.jws.server.jaxws.HelloExceptionBean的异常bean类),最后使用以下的类发布Web 服务:
Java代码
  1. packageorg.jsoso.jws.server;
  2. importjava.util.LinkedList;
  3. importjava.util.List;
  4. importjavax.xml.ws.Binding;
  5. importjavax.xml.ws.Endpoint;
  6. importjavax.xml.ws.handler.Handler;
  7. /**
  8. *@authorzsy启动webservices服务
  9. */
  10. publicclassStartServer{
  11. /**
  12. *@paramargs
  13. */
  14. publicstaticvoidmain(String[]args){
  15. /*
  16. *生成Example服务实例
  17. */
  18. ExampleserverBean=newExample();
  19. /*
  20. *发布WebService到http://localhost:8080/hello地址
  21. */
  22. Endpointendpoint=
  23. Endpoint.publish("http://localhost:8080/hello",serverBean);
  24. Bindingbinding=endpoint.getBinding();
  25. /*
  26. *设置一个SOAP协议处理栈
  27. *这里就简单得打印SOAP的消息文本
  28. */
  29. List<Handler>handlerChain=newLinkedList<Handler>();
  30. handlerChain.add(newTraceHandler());
  31. binding.setHandlerChain(handlerChain);
  32. System.out.println("服务已启动http://localhost:8080/hello");
  33. }
  34. }
  1. packageorg.jsoso.jws.server;
  2. importjava.util.LinkedList;
  3. importjava.util.List;
  4. importjavax.xml.ws.Binding;
  5. importjavax.xml.ws.Endpoint;
  6. importjavax.xml.ws.handler.Handler;
  7. /**
  8. *@authorzsy启动webservices服务
  9. */
  10. publicclassStartServer{
  11. /**
  12. *@paramargs
  13. */
  14. publicstaticvoidmain(String[]args){
  15. /*
  16. *生成Example服务实例
  17. */
  18. ExampleserverBean=newExample();
  19. /*
  20. *发布WebService到http://localhost:8080/hello地址
  21. */
  22. Endpointendpoint=
  23. Endpoint.publish("http://localhost:8080/hello",serverBean);
  24. Bindingbinding=endpoint.getBinding();
  25. /*
  26. *设置一个SOAP协议处理栈
  27. *这里就简单得打印SOAP的消息文本
  28. */
  29. List<Handler>handlerChain=newLinkedList<Handler>();
  30. handlerChain.add(newTraceHandler());
  31. binding.setHandlerChain(handlerChain);
  32. System.out.println("服务已启动http://localhost:8080/hello");
  33. }
  34. }

在控制台运行这个类,就可以使用URL :http://localhost:8080/hello?wsdl 浏览到上文所描述的WSDL的全文了。这说明您的第一个Web Service应用发布成功!

构建Web Service客户端
使用JavaSE6.0构建Web Service的客户端是一件相当简单的事。这里我们要使用到JDK中的另一个命令行工具wsimport。在控制台下输入以下命令:
引用
wsimport -d ./bin -s ./src -p org.jsoso.jws.client.ref http://localhost:8080/hello?wsdl

即可在包org.jsoso.jws.client.ref中生成客户端的存根及框架文件。其中我们要使用的类只有两个:服务类Example_Service和本地接口Example。编写如下客户端,即可调用Web Service服务:
Java代码
  1. packageorg.jsoso.jws.client;
  2. importorg.jsoso.jws.client.ref.*;
  3. publicclassRunClient{
  4. /**
  5. *@paramargs
  6. */
  7. publicstaticvoidmain(String[]args){
  8. //初始化服务框架类
  9. Example_Serviceservice=newExample_Service();
  10. //或者本地服务借口的实例
  11. Exampleserver=(Example)service.getExamplePort();
  12. try{
  13. //调用webservice的toSayHello方法
  14. System.out.println("输入toSayHello的返回值——"+server.toSayHello("阿土"));
  15. Personperson=newPerson();
  16. person.setName("阿土");
  17. person.setAge(25);
  18. //调用webservice的sayHello方法
  19. server.sayHello(person,"机器人");
  20. person=newPerson();
  21. person.setName("aten");
  22. person.setAge(30);
  23. //调用webservice的sayHello方法
  24. PersonArraylist=server.sayHello(person,"机器人");
  25. //输出返回值
  26. System.out.println("/n以下输入sayHello的返回值——");
  27. for(Personp:list.getItem()){
  28. System.out.println(p.getName()+":"+p.getAge());
  29. }
  30. }catch(HelloException_Exceptione){
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  1. packageorg.jsoso.jws.client;
  2. importorg.jsoso.jws.client.ref.*;
  3. publicclassRunClient{
  4. /**
  5. *@paramargs
  6. */
  7. publicstaticvoidmain(String[]args){
  8. //初始化服务框架类
  9. Example_Serviceservice=newExample_Service();
  10. //或者本地服务借口的实例
  11. Exampleserver=(Example)service.getExamplePort();
  12. try{
  13. //调用webservice的toSayHello方法
  14. System.out.println("输入toSayHello的返回值——"+server.toSayHello("阿土"));
  15. Personperson=newPerson();
  16. person.setName("阿土");
  17. person.setAge(25);
  18. //调用webservice的sayHello方法
  19. server.sayHello(person,"机器人");
  20. person=newPerson();
  21. person.setName("aten");
  22. person.setAge(30);
  23. //调用webservice的sayHello方法
  24. PersonArraylist=server.sayHello(person,"机器人");
  25. //输出返回值
  26. System.out.println("/n以下输入sayHello的返回值——");
  27. for(Personp:list.getItem()){
  28. System.out.println(p.getName()+":"+p.getAge());
  29. }
  30. }catch(HelloException_Exceptione){
  31. e.printStackTrace();
  32. }
  33. }
  34. }

届此,本次Web Service的学习暂告一个段落。Java Web Service是一个相当庞大的知识体系,其中涉及的相关技术较多,这里无法一一道来,我们将会在今后的开发和使用中,同大家做进一步深入的探讨和学习。

附录:wsgen与wsimport命令说明

wsgen
wsgen是在JDK的bin目录下的一个exe文件(Windows版),该命令的主要功能是用来生成合适的JAX-WS。它读取Web Service的终端类文件,同时生成所有用于发布Web Service所依赖的源代码文件和经过编译过的二进制类文件。这里要特别说明的是,通常在Web Service Bean中用到的异常类会另外生成一个描述Bean,如果Web Service Bean中的方法有申明抛出异常,这一步是必需的,否则服务器无法绑定该对像。此外,wsgen还能辅助生成WSDL和相关的xsd文件。wsgen从资源文件生成一个完整的操作列表并验证web service是否合法,可以完整发布。
命令参数说明:
  •  -cp 定义classpath
  •  -r 生成 bean的wsdl文件的存放目录
  •  -s 生成发布Web Service的源代码文件的存放目录(如果方法有抛出异常,则会生成该异常的描述类源文件)
  •  -d 生成发布Web Service的编译过的二进制类文件的存放目录(该异常的描述类的class文件)

命令范例:wsgen -cp ./bin -r ./wsdl -s ./src -d ./bin -wsdl org.jsoso.jws.server.Example

wsimport
wsimport也是在JDK的bin目录下的一个exe文件(Windows版),主要功能是根据服务端发布的wsdl文件生成客户端存根及框架,负责与Web Service 服务器通信,并在将其封装成实例,客户端可以直接使用,就像使用本地实例一样。对Java而言,wsimport帮助程序员生存调用web service所需要的客户端类文件.java和.class。要提醒指出的是,wsimport可以用于非Java的服务器端,如:服务器端也许是C#编写的web service,通过wsimport则生成Java的客户端实现。
命令参数说明:
  •  -d 生成客户端执行类的class文件的存放目录
  •  -s 生成客户端执行类的源文件的存放目录
  •  -p 定义生成类的包名

命令范例:wsimport -d ./bin -s ./src -p org.jsoso.jws.client.ref http://localhost:8080/hello?wsdl
分享到:
评论

相关推荐

    Web Service概述及应用

    Web Service、CXF框架是解决跨平台和跨语言的通信技术

    Web Service学习

    Web Service概述,SOAP协议简介 WSDL简介 JavaSE6.0下的Web Service

    C#邮件收发系统代码下载

    Web Service是一种基于...9.1 Web Service概述  随着互联网的不断发展,计算机之间的共享数据变得越来越重要。企业与个人为了获得更高的交互性和便利性也提供给分布式应用程序一个产生的背景。 9.1.1 Web服务基本概念

    创建和访问Web Service

    创建和访问Web Service 1 1. 概述 2 2. 创建Java Web Service 2 3. 访问Java Web Service 9 4. 访问.NET Web Service 20

    基于Delphi的Web Service 客户端调用

    概述了Web Service的特点和核心要素, 依据Delphi和Java对Web Service 的支持,给出了基于Java ,Delphi开发Web Service的流程, 并用一个Delphi调用Java开发Web Service应用程序来说明跨平台Web Service应用程序的步骤...

    Web Service实现天气预报 在线客服实现 循环播放广告 广告位轮换管理页面设计 浏览循环播放广告页面设计

    30.1.1 Web Service天气预报功能概述 657 30.1.2 介绍Web Service 657 30.1.3 创建一个简单Web Service 658 30.1.4 使用Web Service获取天气预报 661 30.2 社会标签(Tags)技术 663 30.2.1 社会标签简介 ...

    web service学习笔记

    web service的概述,包括定义,基本架构,使用的主要技术以及常用框架的简单介绍

    XML与WEB Service技术(微软):第01章 概述.ppt

    XML与WEB Service技术(微软):第01章 概述.ppt

    Visual C#网络编程技术与实践源代码

     9.1 Web Service概述  9.1.1 Web服务基本概念  9.1.2 Web服务的优势  9.1.3 Web服务的架构  9.2 需求分析与设计  9.2.1 需求分析  9.2.2 文件功能设计  9.2.3 数据库设计  9.3 编写Web Service...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    1.1 经典Java EE应用概述 10 1.1.1 Java EE 6相关规范 10 1.1.2 经典Java EE应用的分层模型 11 1.1.3 经典Java EE应用的组件 13 1.1.4 经典Java EE应用架构的优势 13 1.1.5 常用的企业服务器 14 1.2 经典Java EE应用...

    JAVA程序开发大全---上半部分

    14.1 Web Service概述 244 14.2 使用MyEclipse创建Web Service项目 245 14.2.1 创建Web Service项目 245 14.2.2 开发Web Service服务器端 247 14.2.3 发布和运行Web Service 249 14.2.4 使用Web Services Explorer...

    面向.NET的WEB应用程序设计课件

    第13章 使用和创建 XML Web Service 第14章 状态管理 第15章 配置、优化和部署 Microsoft ASP.NET Web 应用程序 第16章 Web 安全性介绍 第17章 Web 应用程序安全性规划 第18章 验证用户输入 第19章 Internet 信息...

    JAX-WS自学笔记

    2、创建Web Service 2.1 从java开始 2.1.1 运行wsgen 2.1.2 生成的WSDL和XSD 2.1.3 目录结构 2.2 从WSDL开始 2.2.1 运行wsimport 2.2.2 生成的java代码 2.3发布Web Service 2.3.1在应用程序中发布 ...

    WebService之JAX-WS自学笔记

    2、创建Web Service 2.1 从java开始 2.1.1 运行wsgen 2.1.2 生成的WSDL和XSD 2.1.3 目录结构 2.2 从WSDL开始 2.2.1 运行wsimport 2.2.2 生成的java代码 2.3发布Web Service 2.3.1在应用程序...

    毕业文论:远程无线粮库实时监控管理系统的设计与实现.docx

    第三章详尽分析了基于Web Service 的远程无线粮库监控系统的需求,主要有需求概述、主要功能的需求分析、系统设计原则。 第四章详细叙述了系统的详细设计,包括总体设计、详细功能设计、通信 协议设计、数据库设计...

    PHP采用XML-RPC构造Web Service实例教程

    目前进行Web Service通信有两种协议标准,一种是XML-RPC,另外一种是SOAP。XML-RPC比较简单,出现时间比较早,SOAP比较复杂,主要是一些需要稳定、健壮、安全并且复杂交互的时候使用。 PHP自身就集成了XML-RPC和SOAP...

    Sybase ASE 15.7 开发文档:Web 服务用户指南

    概述 .......... 3 Adaptive Server Enterprise Web 服务 .......... 4 ASE Web 服务的优点 .......... 4 存储过程和函数 .......... 4 SQL .......... 4 安全性 .......... 5 LDAP .......... 5 用户定义的 Web ...

    CXF概述和开发WebService服务端

    Web Service、CXF跨平台和跨语言的通信

Global site tag (gtag.js) - Google Analytics