技术开发 频道

webservice学习笔记——理解篇



八、小结
开发webservice的基本步骤为
1、编写服务器端,要点有
 ①导入WebService包和WebMethod包
   import javax.jws.WebService;
   import javax.jws.WebMethod;
 ②实现的服务类前加@WebService符号 
 ③为了代码清晰,类提供的公开方法前加@WebMethod符号,这个不写对编译也没影响,
  
2、编译服务器端,要点为
 ①javac命令的classpath选项中要有javaee.jar的路径,如
  javac -classpath d:/Sun/SDK/lib/javaee.jar -d ./build src/endpoint/Hello.java
 ②用wsgen命令生成wsdl文件.
 ③将服务器端打包
 注意如果是sun的服务器,那么把service类直接编译到
 javaee5安装目录\domains\domain1\autodeploy下,可以自动完成②和③的工作。
 我们介绍的sun自带的入门脚本就是这么做的。
3、在客户端机器上自动生成stub类,要点为
 ①客户机上必须也装有jdk和javaee5
 ②用wsimport工具将服务器传过来的wsdl文件转换成本地的stub类
4、编写客户端调用代码,要点:
 ①导入WebServiceRef包
  import javax.xml.ws.WebServiceRef;
 ②导入本地生成的stub类,如
  import endpoint.HelloService;
  import endpoint.Hello;
 ③指明服务器的wsdl路径
  @WebServiceRef(wsdlLocation="
http://localhost:8080/myhello/HelloService?WSDL")
 ④声明一个静态的service对象
  static HelloService service;
 ⑤对要调用的远程方法声明一个代理对象,通过代理来调用真正的远程方法
  Hello port = service.getHelloPort();
     String ret = port.getHello(System.getProperty("user.name"));
5、编译客户端调用程序,注意classpath参数中要有
 ①stub类的路径
 ②javaee.jar的路径
 ③appserv-ws.jar的路径
 
6、用appclient执行客户端程序,要点为
 ①进入到客户端程序的上级目录
 ②把APPCPATH的值设置为当前目录" . "
 ③appclient的第一个参数为客户端程序名,
   后面的参数是传给客户端程序本身的命令行参数。

/*title: web service入门学习笔记(九)
**date: 2007/01/19
**author:laomai
**url:
http://blog.csdn.net/laomai/
*/ 
 
九、本文中用到的文件
1、WebTest项目文件列表 
WebTest\passwd 保存密码的文件,手工建立
WebTest\src 子目录 手工建立,内容为
 endpoint\Hello.java  服务器类的实现文件
 client\Client.java   客户类的实现文件
WebTest\build
 generated 子目录,手工建立,内容为
  HelloService.wsdl   由wsgen命令生成
  HelloService_schema1.xsd  由wsgen命令生成
 classes 子目录,手工建立,内容为
  endpoint\Hello.class  由javac命令生成
  endpoint\jaxws子目录,由wsgen命令自动生成,内容为
    GetHello.java
    GetHelloResponse.java
    GetHello.class
    GetHelloResponse.class
  stub 子目录,手工建立,内容为:
   client\Client.class 由javac命令生成
   endpoint 子目录 由wsimport命令自动生成,内容为:
    GetHello.java
    GetHelloResponse.java
    Hello.java
    HelloService.java
    ObjectFactory.java
    package-info.java
    package-info.class
    GetHello.class
    GetHelloResponse.class
    Hello.class
    HelloService.class
    ObjectFactory.class
WebTest\deploy子目录 手工建立,内容为
 hello.war 将WEB-INF子目录打包后生成的文件,由jar命令生成
 WEB-INF   打包的输入目录,手工建立。内容包括:
  web.xml   手工建立
  classes\endpoint子目录 为build\classes\endpoint的拷贝
  wsdl子目录,由build\generated拷贝而来

2、生成的HelloService.wsdl文件的内容

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <definitions targetNamespace="http://endpoint/" name="HelloService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://endpoint/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> <types> <xsd:schema> <xsd:import namespace="http://endpoint/" schemaLocation="HelloService_schema1.xsd"/> </xsd:schema> </types> <message name="getHello"> <part name="parameters" element="tns:getHello"/> </message> <message name="getHelloResponse"> <part name="parameters" element="tns:getHelloResponse"/> </message> <portType name="Hello"> <operation name="getHello"> <input message="tns:getHello"/> <output message="tns:getHelloResponse"/> </operation> </portType> <binding name="HelloPortBinding" type="tns:Hello"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="getHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloService"> <port name="HelloPort" binding="tns:HelloPortBinding"> <soap:address location="REPLACE_WITH_ACTUAL_URL"/> </port> </service> </definitions>
0
相关文章