`
tianshibaijia
  • 浏览: 1122802 次
文章分类
社区版块
存档分类
最新评论

Jdk6 WebService入门--结合实际修改了一些内容

 
阅读更多

一、Web Services简介
什么是Web Services
Web service 就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web来调用这个应用程序。
基于浏览器的瘦客户应用程序,即BS 结构,是目前流行的,使得Web Services的应用越来越广泛。Web Services 是一种构建应用程序的模型,并能在所有支持 Internet 通讯的操作系统上实施运行。Web Services 令基于组件的开发和 Web 的结合达到最佳,基于组件的对象模型,利用 SOAP 和 XML对这些模型在通讯方面作了进一步的扩展以消除特殊对象模型的障碍。因为是使用XML作为传输的介质,所以可以跨平台跨语言。
Web Services 实现远程访问,有点类似RMI(远程方法调用)。 但它是利用 HTTP 和 SOAP 协议是商业数据在 Web 上传输,SOAP通过 HTTP 调用商业对象执行远程功能调用,Web 用户能够使用 SOAP 和 HTTP通过 Web 调用的方法来调用远程对象。
Web Services结构
客户根据WSDL描述文档,会生成一个 SOAP 请求消息。Web Services 都是放在Web服务器上面,客户生成的SOAP请求会被嵌入在一个HTTP POST请求中,发送到 Web 服务器来。Web 服务器再把这些请求转发给 Web Services 请求处理器。请求处理器的作用在于,解析收到的 SOAP 请求,调用 Web Services,然后再生成相应的 SOAP 应答。Web 服务器得到 SOAP 应答后,会再通过 HTTP应答的方式把信息送回到客户端。
什么是WSDL
WSDL是WebServicesDescriptionLanguage 的缩写,是一个用来描述Web服务和说明如何与Web服务通信的XML语言。Web Services服务器把一个对像绑定到一个URL 上(如http://localhost:8080/webservices/hello),客户端就可以能过绑定的地址(如:http: //localhost:8080/webservices/hello?wsdl)取得WSDL文件,该文件是标准的XML 格式,描述了被绑定对像的信息,包括可调用的方法,参数,及参数类型,返回值类型,异常类型等。客户端就是通过这些信息调用服务器的方法。
二、JKD6 对Web Services的支持
JDK6提供了对Web Service原生的支持,对Web Service进行了完美的封装,完全隐藏了底层内容,甚至可以不用了解wsdl的具体规范。使用Web Service就像使用本地方法一样简单。下面来举个例子,依然从最简单的HelloWorld入手。
HelloWorld例子
STEP 1,服务器端Bean说明
服 务器端的Java类若要成为一个实现了Web Service的bean,它需要遵循下边这些原则:这个类必须是public类、不能是final的或者abstract、必须有一个公共的默认构造函 数、绝对不能有finalize()方法。若要成为一个实现了Web Service的Bean的方法必须遵循这些原则:
这个方法必须是public,它的参数、返回值、和异常在每个JAX RPC规范中都描述了Java转化成XML/WSDL映射文件的规则,参数和返回值可以是原始类型、数组等等。
下面是服务器端的类HelloWorld.java:
packageorg.young.ws;
importjavax.jws.WebMethod;
importjavax.jws.WebParam;
importjavax.jws.WebResult;
importjavax.jws.WebService;
importjavax.jws.soap.SOAPBinding;
@WebService(targetNamespace ="http://localhost")
@SOAPBinding(style =SOAPBinding.Style.RPC)
publicclassHelloWorld {
@WebMethod(action="toSayHello",operationName="toSayHello",exclude=false)
@WebResult(name="returnWord")//自定义该方法返回值在WSDL中相关的描述
publicString sayHello(@WebParam(name="userName")String userName) {
return"Hello:"+ userName;
}
@WebMethod
publicintgetExp(inti,intj) {
returni / j;
}
}
这是服务器端普通的业务类,通过@WebService、@WebMethod等注释描述来生成WSDL文件。
STEP 2,执行wsgen命令
本 例中到HelloWorld类所在的目录中新建一个命名为wsdl的文件夹,运行:wsgen -cp ./bin -r ./wsdl -s ./src -d ./bin -wsdlorg.young.ws.HelloWorld。执行后会在wsdl文件夹中生成HelloWorld的wsdl描述文件,src文件夹中生成 依赖类,如异常说明类,bin中生成依赖类的class文件
STEP 3发布Web Service Bean ---JDK6内置的http server
启动服务类StartService.java:
packageorg.young.ws;
importjavax.xml.ws.Endpoint;
publicclassStartService {
publicstaticvoidmain(String[] args) {
Endpoint.publish("http://localhost:8080/webservice/hws",newHelloWorld());
}
}
此类很简单,能过Endpoint类的publish()方法发布实例发布地址为:
http://localhost:8080/webservice/hws,必需明确指明http协议,主机IP 地址及端口号,在IE上输入http://localhost:8080/webservice/hws?wsdl返回以下内容说明发布成功
<?xml version="1.0" encoding="UTF-8"?>
<definitionsxmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://localhost"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
targetNamespace="http://localhost"
name="HelloWorldService">
<types/>
<message name="toSayHello">
<part name="userName" type="xsd:string" />
</message>
<message name="toSayHelloResponse">
<part name="returnWord" type="xsd:string" />
</message>
<message name="getExp">
<part name="arg0" type="xsd:int" />
<part name="arg1" type="xsd:int" />
</message>
<message name="getExpResponse">
<part name="return" type="xsd:int" />
</message>
<portType name="HelloWorld">
<operation name="toSayHello"parameterOrder="userName">
<input message="tns:toSayHello" />
<output message="tns:toSayHelloResponse" />
</operation>
<operation name="getExp"parameterOrder="arg0 arg1">
<input message="tns:getExp" />
<output message="tns:getExpResponse" />
</operation>
</portType>
<binding name="HelloWorldPortBinding"type="tns:HelloWorld">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="toSayHello">
<soap:operation soapAction="toSayHello" />
<input>
<soap:body use="literal"
namespace="http://localhost" />
</input>
<output>
<soap:body use="literal"
namespace="http://localhost" />
</output>
</operation>
<operation name="getExp">
<soap:operation soapAction="" />
<input>
<soap:body use="literal"
namespace="http://localhost" />
</input>
<output>
<soap:body use="literal"
namespace="http://localhost" />
</output>
</operation>
</binding>
<service name="HelloWorldService">
<port name="HelloWorldPort"
binding="tns:HelloWorldPortBinding">
<soap:address
location="http://localhost:8080/webservice/hws" />
</port>
</service>
</definitions>

增加一个Servlet,用于启动时注册服务
package org.young.ws.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.ws.Endpoint;

import org.young.ws.HelloWorld;

public class WebServiceStarter extends HttpServlet {
public WebServiceStarter() {
super();
}

public void destroy() {
super.destroy();
}

public void init() throws ServletException {
System.out.println("准备启动服务");
Endpoint.publish("http://localhost:8080/webservice/hws", new HelloWorld());
System.out.println("服务启动完毕");
}
}


web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">

<description>
WebService Examples.
</description>
<display-name>WebService Examples</display-name>

<!-- Define servlets that are included in the example application -->
<servlet>
<servlet-name>WebServiceStarter</servlet-name>
<servlet-class>org.young.ws.servlet.WebServiceStarter</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


</web-app>




STEP 4生成客户端执行类
在cmd命令中执行 wsimport -d ./bin -s ./src- p org.young.ws.client
http://10.168.189.182:8080/webservice/hws?wsdl后在在src目录下生成客户端调用的两个类:
org.young.ws.client.HelloWorld.java根据wsdl描述生成的客户端执行类
org.young.ws.client.HelloWorldServices.java 通过此类负责解悉wsdl初始化客户端HelloWorld实例
在bin目录下生成对应的类文件。
注意:执行wsimport命令时STEP 3的服务必需启动,否则无法生成
STEP 5客户端调用
客户端调用过程ClientRun.java:
packageorg.young.ws.client;
importorg.young.ws.*;
publicclassClientTest {
publicstaticvoidmain(String[] args) {
HelloWorldService hws =newHelloWorldService();
HelloWorld hw = hws.getHelloWorldPort();
System.out.println(hw.getExp(9, 3));
System.out.println(hw.toSayHello("kevin"));
}
}
启动STEP 3中的服务,运行Client后,制控台输出:
3
Hello:kevin
注意:经测试,用Endpoint.publish("http://localhost:8080/webservice/hws", new HelloWorld())方式在Tomcat6中发布,不会存在端口号与路径冲突。
JKD6中定义的Web Service注释
1.@WebService 标注要暴露为WebServices的类或接口,用于申修饰类或接口,包含属性
targetNamespace 定义命名空间,默认为”http://”+”包名倒排”
nameWeb Service的名称,默认为类名,例如:
<definitions targetNamespace="http://localhost/"
name="HelloWorldService">
portNameWeb Service的端口名称
serviceNameWeb Service的服务名称,例如
<service name="HelloWorldService">
<port name="HelloWorldPort"
binding="tns:HelloWorldPortBinding">
...
</port>
</service>
2.@SOAPBinding 定义Web Service 在SOAP中的消息协议,用于申修饰类或接口,包含属性
style 定义消息的编码类型
user 定义消息的格式化类型
3.@WebMethod 定义Web Service运作的方法,包含属性
action 操作的活动
operationName与此方法匹配的 wsdl:operation 的名称
exclude 标注此方法是否被暴露,默认为false
4.@WebResult 定义返回值,返回值类型不能为接口类或抽象类,而且必须有个不带参的构造函数,包含属性
name返回值的名称
partName表示此返回值的 wsdl:part 的名称
targetNamespace返回值的 XML 名称空间
header如果为 true,则结果是从消息头而不是消息正文获取的
5.@WebParam 定义方法的参数,参数类型不能为接口类或抽象类,而且必须有个不带参的构造函数,包含属性
name参数名称
partName表示此参数的 wsdl:part 的名称
targetNamespace参数的 XML 名称空间
header如果为 true,则结果是从消息头而不是消息正文获取的
mode参数的流向(IN、OUT 或 INOUT 之一)
wsgenwsimport命令说明
wsgen 命令的主要功能是用来生成合适的JAX-WS。它读取Web Service的终端类文件,在我们的例子中就是 org.young.ws.HelloWorld,同时生成所有用于发布Web Service所依赖的源代码文件和经过编译过的二进制类文件,通常Web Service Bean中用到的异常类会另外生成一个描述Bean。它还能生成WSDL和符合规范的HelloWorld类Web Service。wsgen从资源文件生成一个完整的操作列表并验证是合法的。如果Web Service Bean中的主法有申明抛出异常,这一步是必需的,否则服务器无法绑定该对像。
命令参数说明:
-cp 定义classpath
-r 生成 bean的wsdl文件的存放目录
-s 生成发布Web Service的源代码文件的存放目录(如果方法有抛出异常,则会生成该异常的描述类源文件)
-d 生成发布Web Service的编译过的二进制类文件的存放目录(该异常的描述类的class文件)
wsimport命令的主要功能是根据wsdl文件生成客户端存根及框架,负责与Web Service 服务器通信,并在将其封装成实例,客户端可以直接使用,就像使用本地实例一样。
命令参数说明:
-d 生成客户端执行类的class文件的存放目录
-s 生成客户端执行类的源文件的存放目录
-p 定义生成类的包名
三、附录:WSDL说明
1、WSDL 文档结构
WSDL 文档是利用这些主要的元素来描述某个web service 的:
元素
定义
<portType>
web service 执行的操作
<message>
web service 使用的消息
<types>
web service 使用的数据类型
<binding>
web service 使用的通信协议
一个WSDL 文档的主要结构是类似这样的:
<definitions>
<types>
definition of types........
</types>
<message>
definition of a message....
</message>
<portType>
definition of a port.......
</portType>
<binding>
definition of a binding....
</binding>
</definitions>
WSDL 文档可包含其它的元素,比如extension 元素,以及一个 service 元素,此元素可把若干个 web services 的定义组合在一个单一的 WSDL 文档中。
WSDL端口
<portType> 元素是最重要的WSDL 元素。
它可描述一个web service、可被执行的操作,以及相关的消息。
可以把<portType> 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。
WSDL消息
<message> 元素定义一个操作的数据元素。
每个消息均由一个或多个部件组成。可以把这些部件比作传统编程语言中一个函数调用的参数。
WSDL types
<types> 元素定义web service 使用的数据类型。
为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。
WSDL Bindings
<binding> 元素为每个端口定义消息格式和协议细节。
WSDL实例
这是某个WSDL 文档的简化的片段:
<message name="getTermRequest">
<part name="term" type="xs:string"/>
</message>
<message name="getTermResponse">
<part name="value" type="xs:string"/>
</message>
<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest"/>
<output message="getTermResponse"/>
</operation>
</portType>
在这个例子中,<portType> 元素把 "glossaryTerms" 定义为某个端口的名称,把 "getTerm" 定义为某个操作的名称。
操作"getTerm" 拥有一个名为 "getTermRequest" 的输入消息,以及一个名为 "getTermResponse" 的输出消息。
<message> 元素可定义每个消息的部件,以及相关联的数据类型。
对比传统的编程,glossaryTerms 是一个函数库,而 "getTerm" 是带有输入参数 "getTermRequest" 和返回参数 getTermResponse 的一个函数。
2、WSDL 端口
<portType>元素是最重要的WSDL 元素。
它可描述一个web service、可被执行的操作,以及相关的消息。
端口定义了指向某个web service 的连接点。可以把它元素比作传统编程语言中的一个函数库(或一个模块、或一个类),而把每个操作比作传统编程语言中的一个函数。
操作类型
请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:
类型
定义
One-way
此操作可接受消息,但不会返回响应。
Request-response
此操走可接受一个请求并会返回一个响应
Solicit-response
此操作可发送一个请求,并会等待一个响应。
Notification
此造作可发送一条消息,但不会等待响应。
One-Way操作
一个one-way 操作的例子:
<message name="newTermValues">
<part name="term" type="xs:string"/>
<part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
<operation name="setTerm">
<input name="newTerm" message="newTermValues"/>
</operation>
</portType >

在这个例子中,端口 "glossaryTerms" 定义了一个名为 "setTerm" 的 one-way 操作。

这个 "setTerm" 操作可接受新术语表项目消息的输入,这些消息使用一条名为 "newTermValues" 的消息,此消息带有输入参数 "term" 和 "value"。不过,没有为这个操作定义任何输出。
Request-Response 操作

一个 request-response 操作的例子:

<message name="getTermRequest">
<part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
<part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest"/>
<output message="getTermResponse"/>
</operation>
</portType>

在这个例子中,端口 "glossaryTerms" 定义了一个名为 "getTerm" 的 request-response 操作。

"getTerm" 操作会请求一个名为 "getTermRequest" 的输入消息,此消息带有一个名为 "term" 的参数,并将返回一个名为 "getTermResponse" 的输出消息,此消息带有一个名为 "value" 的参数。

WSDL 绑定可为 web service 定义消息格式和协议细节。
绑定到 SOAP

一个 请求 - 响应 操作的例子:


<message name="getTermRequest">
<part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
<part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest"/>
<output message="getTermResponse"/>
</operation>
</portType>

<binding type="glossaryTerms" name="b1">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<operation>
<soap:operation
soapAction="http://example.com/getTerm"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>

binding 元素有两个属性 - name 属性和 type 属性。

name 属性定义 binding 的名称,而 type 属性指向用于 binding 的端口,在这个例子中是 "glossaryTerms" 端口。

soap:binding 元素有两个属性 - style 属性和 transport 属性。

style 属性可取值 "rpc" 或 "document"。在这个例子中我们使用 document。transport 属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP。

operation 元素定义了每个端口提供的操作符。

对于每个操作,相应的 SOAP 行为都需要被定义。同时您必须如何对输入和输出进行编码。在这个例子中我们使用了 "literal"。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics