webservice 小结之jax-ws(2)

好了,开始动手做个简单例子吧:

[b]创建jax-ws service:[/b]
[img]http://dl.iteye.com/upload/attachment/0064/5229/e51faad0-60d1-392a-8419-c6badb2fd8e5.png[/img]
输入项目名称,framework选择JAX-WS,项目建好了之后你就会发现其实webservice项目就是一个普通的web项目。

[b]新建业务处理类:[/b]
[img]http://dl.iteye.com/upload/attachment/0064/5232/7d6d3252-0d95-3595-84ff-92afab890dbf.png[/img]

业务处理类要满足以下条件:
1、这个类必须是public类
2、这些类不能是final的或者abstract
 3、这个类必须有一个公共的默认构造函数
 4、这个类绝对不能有finalize()方法
5、实现方法必须非static

[b]新建webservice service[/b]
选中项目点击右键new->new webservice,如果找不到这个选项点击other查找下。
[img]http://dl.iteye.com/upload/attachment/0064/5234/6f177114-118d-3359-9281-59b9e5207765.png[/img]
选择对应的project,framework还是选择jax-ws,strategy选择下面的create web service from java bean。这个strategy指的是webservice的生成策略,有2中方式,基于java bean 和基于wsdl文件的。对应着webservice的2种设计原则,一种是code first,另外一种是wsdl first。
点击next进入下一步。

[img]http://dl.iteye.com/upload/attachment/0064/5244/63b70bf3-1cc4-375d-9ed4-0aace2f6e476.png[/img]
选择对应的业务处理类,soap style/use 选择默认的,MTOM Support 是实现webservice 之间的文件传输。如果要生成wsdl文件就勾选 generate wsdl in project。下面的三个选项可以设置webservice 的命名空间,服务名以及服务端口
点击finish
系统会自动生成webservice 代理类和相应的wsdl文件。为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。这些数据类型用来定义web service方法的参数和返回指。也就是对应的xsd文件。然后需要添加对应library然后整个系统的结构为:

[img]http://dl.iteye.com/upload/attachment/0064/5248/04269ba7-eca4-32b5-8459-ca9576cd9c85.png[/img]
然后将程序放到tomcat服务器,即可运行,例如:http://127.0.0.1:8088/jaxwsService/HelloWordPort?wsdl 如果能访问到对应wsdl文件就表示这个webservice发布成功。

可以看下wsdl文档内容:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. -->
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.test.com/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWordService" targetNamespace="http://ws.test.com/">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.test.com/" schemaLocation="HelloWordService_schema1.xsd"/>
</xsd:schema>
</types>
<message name="add">
<part element="tns:add" name="parameters"/>
</message>
<message name="addResponse">
<part element="tns:addResponse" name="parameters"/>
</message>
<message name="hello">
<part element="tns:hello" name="parameters"/>
</message>
<message name="helloResponse">
<part element="tns:helloResponse" name="parameters"/>
</message>
<portType name="HelloWordDelegate">
<operation name="add">
<input message="tns:add"/>
<output message="tns:addResponse"/>
</operation>
<operation name="hello">
<input message="tns:hello"/>
<output message="tns:helloResponse"/>
</operation>
</portType>
<binding name="HelloWordPortBinding" type="tns:HelloWordDelegate">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="add">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="hello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWordService">
<port binding="tns:HelloWordPortBinding" name="HelloWordPort">
<soap:address location="http://localhost:8080/jaxwsService/HelloWordService"/>
</port>
</service>
</definitions>


对照着简介一章所介绍wsdl文档结构看下。
第一部分是 <types>元素,主要定义webservice的数据类型,它实际是引用了另外一个xsd文件,xsd文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.test.com/" targetNamespace="http://ws.test.com/" version="1.0">

<xs:element name="add" type="tns:add"/>

<xs:element name="addResponse" type="tns:addResponse"/>

<xs:element name="hello" type="tns:hello"/>

<xs:element name="helloResponse" type="tns:helloResponse"/>

<xs:complexType name="hello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="helloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="add">
<xs:sequence>
<xs:element name="arg0" type="xs:int"/>
<xs:element name="arg1" type="xs:int"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="return" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

如果细心的朋友大家就会发现在new webservice那一步时,有个soap style/use 选项你可以看到myeclipse 6.5中默认的就是上面所说的 document/wrapped样式,而xsd里面也主要是使用xs:complexType节点来描述数据类型。
然后在接着看wsdl中的<message> 元素你可以看到message元素定义之后主要是使用在<portType> 元素中。
而<portType> 元素则可以告诉大家这个webservice有几个方法。方法名叫什么有没有返回值等等。<binding> 元素则是为每个端口定义消息格式和协议细节。你可以找到style="document"以及对应use="literal"等字样。这就规定了该webservice使用哪种编码样式来进行SOAP的编码。

下面来建一个webservice 客户端来模拟调用下。也是用myeclipse 6.5来进行开发。

[b]新建jax-ws client[/b]
新建个普通的java project,并建好对应包。点击右键选择 new web service client,选择对应项目,选择jax-ws然后点击next.
[img]http://dl.iteye.com/upload/attachment/0064/5272/5140a146-b176-3ca6-a9bc-dd1f4e3de1ec.png[/img]
选择wsdl URL,然后将wsdl地址复制上去,选择生成client类放入哪个包.点击next
[img]http://dl.iteye.com/upload/attachment/0064/5274/c0b8ab9c-55f2-3052-bd0c-b981695c270e.png[/img]
下个界面是进行wsdl的验证。如果没有报错直接点击next。添加对应所需要的library,点击finish。client 项目至此新建完成,项目结构为:
[img]http://dl.iteye.com/upload/attachment/0064/5276/33f667ca-3331-3542-af57-09e271c94de4.png[/img]
新建client 调用类:
[img]http://dl.iteye.com/upload/attachment/0064/5278/2bd98684-b42a-37df-9da1-5af461595178.png[/img]

至此大功告成,整个示例就完成了。但是咱们也别歇着,回头看看jax-ws的实现原理。
1)webservice 服务器端发布一个webservice项目,这时候会为这个webservice项目分配一个servlet,大家可以看下webservice服务器端项目下web.xml,然后根据不同的webservice分配不同的<url-pattern>,大家可以看下<url-pattern>里面的值其实就是webservice 对应的port.
2)然后webservice clinet会生成代理类,然后利用代理类发送SOAP消息去请求webservice 服务器端的servlet,而servlet会根据不同的port去请求不同的webservice。

流程图如下:
[img]http://dl.iteye.com/upload/attachment/0064/5202/59101196-e6d9-3890-9750-ff663dc0b351.png[/img]

可能有些同学还会问下这个问题。就是说你服务器端和客户端都用的是jax-ws,所以根据wsdl在myeclipse中就能自动生成客户端代码,但是如果服务器端用的是其他框架就不能自动生成客户端代码怎么办?别着急。既然有自动生成就有手动生成的。而手动生成是不管服务器端是使用框架的。

[b]wsgen [/b]
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

[b]wsimport [/b]
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值