/*title: web service入门学习笔记
**date: 2007/01/16
**author:laomai
**url: http://blog.csdn.net/laomai/
*/
web service入门学习笔记
最近要做一个java项目,里面用到了webservice技术,
经过一个多月的磕磕绊绊的摸索,总算如了点门。现将我的学习笔记贴出来,供大家参考。
说明,本笔记第七部分主要参考了
http://blog.csdn.net/lin_bei/archive/2006/11/07/1371131.aspx
的内容,由于这位兄弟翻译的不是很通顺,我就按照自己的理解来改编成了
hellowrold的例子:-)。
其他部分为我原创,转载时请注明出处。
一、实验环境
win2k + jdk1.6 + javee5.0 + Myeclipse5.1
jdk和javee5.0均可从
http://java.sun.com/javase/downloads/index.jsp
下载,安装文件名为
jdk-6-windows-i586.exe
java_ee_sdk-5_02-windows.exe
没有myeclipse的也可以用eclipse代替,只要ide能执行
ant脚本就可以.
/*title: web service入门学习笔记(二)
**date: 2007/01/16
**author:laomai
**url: http://blog.csdn.net/laomai/
*/
二、第一个最简单的例子
jsee5安装以后会在系统中建立一个Application Server pe9,这是sun自带的网络服务器,
和tomcat、weblogic的性质类似。
在D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws下有一个自带的web service入门例子,
(D:\Sun\SDK\为我机器上javaee5的安装路径)
我们就先实验它,来理解webservice的本质
1、把jdk的安装路径如D:\Java\jdk1.6.0\bin 加到系统的path环境变量中
2、运行sun自带的网络服务器,具体方法为
开始->程序->Sun Microsystems->Application Server PE 9->Start Default Server
然后当弹出的cmd窗口中出现提示“按任意键继续时”输入回车,窗口会关闭,此时在浏览器输入
http://localhost:8080,应该出现如下内容:
Sun Java System Application Server Platform Edition 9.0
Your server is up and running!
说明服务器已经启动了
3、在Myeclipse打开刚才的例子目录D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws
下的build.xml文件,这个一个ant脚本,具体含义我们以后再讲,现在先执行它
3、在build.xml文件中单击右键,在弹出菜单中选择"run as"->"1 ant build",此时build.xml里的
内容会被执行,在Myeclipse的console中会输出:
buildfile: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build.xml
init:
compile-deploy-service:
[echo] d:/Sun/SDK
get-artifacts-windows:
get-artifacts-unix:
get-artifacts:
compile-client:
[javac] Compiling 1 source file to D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
run-client-windows:
[exec] Hello result = Hello Administrator!
run-client-unix:
run-client:
all:
BUILD SUCCESSFUL
Total time: 43 seconds
其中
[exec] Hello result = Hello Administrator!
的输出结果说明客户端调用服务器的webservice已经成功。
第一个例子就完成了。我们下面会对这个例子进行详细讲解.
/*title: web service入门学习笔记(三)、(四)
**date: 2007/01/16
**author:laomai
**url: http://blog.csdn.net/laomai/
*/
三、WebService的本质
从搞c的程序员的眼光来看,webservice实际上就是用java实现的rpc(远端过程调用),
或者说是dll的变形。服务器把它的接口对外发布成一个wsdl文件,客户端根据这个wsdl的内容生成
本地的代理类,再通过代理类调用远端的接口,代理再把接口的执行执行结果回传给客户端,
进行下一步处理。
四、例子源代码剖析
D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws(以后简称为hello-jaxws)
项目里的源文件只有三个
hello-jaxws\src\endpoint\Hello.java 提供webservice的服务器端实现类
hello-jaxws\src\client\Client.java 调用webservice的客户端测试类
hello-jaxws\build.xml 自动编译的ant脚本
下面我们把这三个文件的内容看一下
1、服务器端的service实现类文件Hello.java,
/* src\endpoint\Hello.java文件的内容 */
package endpoint;
import javax.jws.WebService;
@WebService
public class Hello
{
public String getHello(String name)
{
return "Hello " + name + "!";
}
}
有这个文件可以看出,编写一个service的实现类,把编写普通的java类
差不多,只不过又多了三点
①要在该类前一个@WebService说明,
②引入javax.jws.WebService;
③在公开的方法前加@WebMethod说明,如果不加的话,
所有的public方法都会自动变成service的对外接口.
2、客户端测试类的实现文件
/* src\client\Client.java文件的内容 */
package client;
import javax.xml.ws.WebServiceRef;
import endpoint.HelloService;
import endpoint.Hello;
public class Client
{
@WebServiceRef(wsdlLocation="http://localhost:8080/Hello/HelloService?WSDL")
static HelloService service;
public static void main(String[] args)
{
Client client = new Client();
client.doHello();
}
public void doHello()
{
try
{
Hello port = service.getHelloPort();
String ret = port.getHello(System.getProperty("user.name"));
System.out.println("Hello result = " + ret);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
客户端调用代码要点为:
①导入WebServiceRef包
import javax.xml.ws.WebServiceRef;
②导入本地生成的stub类,另外编译时也要指明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"));
3、ant 脚本build.xml
<!-- ant 脚本build.xml的内容 -->
<?xml version="1.0" encoding="UTF-8"?>
<project name="hello-jaxws" default="all" basedir=".">
<!-- include user specific build properties -->
<!-- 导入预先j2ee预先写好的设置文件-->
<property file="../../../bp-project/build.properties"/>
<property file="${user.home}/build.properties"/>
<property file="../../../bp-project/app-server.properties"/>
<!-- 设置发布目录和类的输出目录 -->
<property name="autodeploydir" value="${javaee.domaindir}/autodeploy"/>
<property name="classesdir" value="./build"/>
<!-- 设置java的类库路径 -->
<path id="classpath">
<pathelement location="${javaee.home}/lib/j2ee.jar"/>
<pathelement location="${classesdir}"/>
</path>
<!-- 项目的最终任务 -->
<target name="all" depends="run-client">
<!--antcall target="restore"/-->
</target>
<!-- 测试操作系统环境-->
<target name="init">
<condition property="windows">
<os family="windows" />
</condition>
<condition property="unix">
<os family="unix" />
</condition>
</target>
<!-- 编译服务器端并把它自动发布到sun的服务器上 -->
<target name="compile-deploy-service" depends="init">
<mkdir dir="${classesdir}"/>
<echo message="${javaee.home}"/>
<javac
srcdir="./src"
includes="endpoint/**"
destdir="${autodeploydir}"
classpath="${javaee.home}/lib/j2ee.jar"
/>
<waitfor maxwait="100" maxwaitunit="second">
<or>
<available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
<available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
</or>
</waitfor>
<condition property="deploy_succeeded">
<available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
</condition>
<condition property="deploy_failed">
<available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
</condition>
</target>
<target name="get-artifacts" depends="compile-deploy-service,get-artifacts-windows,get-artifacts-unix"/>
<!-- 生成客户端所需的stub -->
<target name="get-artifacts-windows" if="windows">
<exec executable="${javaee.home}/bin/wsimport.bat">
<arg line="-keep -d ${classesdir} http://${javaee.server.name}:${javaee.server.port}/Hello/HelloService?WSDL"/>
</exec>
</target>
<target name="get-artifacts-unix" if="unix">
<exec executable="${javaee.home}/bin/wsimport">
<arg line="-keep -d ${classesdir} http://${javaee.server.name}:${javaee.server.port}/Hello/HelloService?WSDL"/>
</exec>
</target>
<!-- 编译客户端 -->
<target name="compile-client" depends="get-artifacts">
<javac srcdir="./src/client" destdir="${classesdir}">
<classpath refid="classpath"/>
</javac>
</target>
<target name="run-client" depends="compile-client,run-client-windows,run-client-unix"/>
<!-- 执行客户端 -->
<target name="run-client-windows" if="windows">
<exec executable="${javaee.home}/bin/appclient.bat" dir="${classesdir}">
<arg value="client.Client"/>
</exec>
</target>
<target name="run-client-unix" if="unix">
<exec executable="${javaee.home}/bin/appclient" dir="${classesdir}" failifexecutionfails="false">
<arg value="client.Client"/>
</exec>
</target>
<!-- 以下几个任务用与清理和卸载-->
<!-- 删除生成的类文件-->
<target name="clean">
<delete dir="${classesdir}"/>
</target>
<!-- 删除和卸载服务器的webservice-->
<target name="restore">
<delete>
<fileset dir="${autodeploydir}/endpoint" includes="Hello*.*"/>
</delete>
</target>
<target name="undeploy">
<antcall target="restore"/>
</target>
</project>
这个脚本有许多在windows平台用不到的步骤,下面我们对其进行改造,把它精简一下.
/*title: web service入门学习笔记(五)
**date: 2007/01/18
**author:laomai
**url: http://blog.csdn.net/laomai/
*/
五、精简后的ant脚本
1、卸载webservice
执行原build.xml里的clean和undeploy任务,把安装好的webservice删除掉,具体办法为:
(1)在myeclipse里打开build.xml文件
(2)在build.xml文件里单击右键菜单中的""run as"->"2 ant build..."",
(3)在弹出的对话框中只选择clean和undelpoy任务。然后单击"run"按钮。
此时再访问http://localhost:8080/Hello/HelloService?WSDL,出现http 404错误,说明卸载成功
2、简化后的脚本内容,在hello-jaxws目录下新建一个buildtest.xml文件,内容为
<?xml version="1.0" encoding="UTF-8"?>
<project name="hello-jaxws" default="all" basedir=".">
<!-- javaee安装目录的设置,代替了原来脚本中的导入语句 -->
<property name="javaee.home" value = "d:/Sun/SDK"/>
<property name="javaee.domaindir" value="${javaee.home}/domains/domain1"/>
<property name="javaee.server.name" value="localhost"/>
<property name="javaee.server.port" value="8080"/>
<!-- 设置发布目录和类的输出目录 -->
<property name="autodeploydir" value="${javaee.domaindir}/autodeploy"/>
<property name="classesdir" value="./build"/>
<!-- 设置java类库路径 -->
<path id="classpath">
<pathelement location="${javaee.home}/lib/j2ee.jar"/>
<pathelement location="${classesdir}"/>
</path>
<target name="all" depends="run-client">
<!--antcall target="restore"/-->
</target>
<target name="run-client" depends="compile-client,run-client-windows" />
<!-- 运行测试类,为项目的最后一个步骤-->
<target name="run-client-windows">
<exec executable="${javaee.home}/bin/appclient.bat" dir="${classesdir}">
<arg value="client.Client" />
</exec>
</target>
<!-- 编译测试webservice的客户类,为项目的第三个步骤,本步骤的输出文件为
${classesdir}/client/Client.class
-->
<target name="compile-client" depends="get-artifacts">
<javac srcdir="./src/client" destdir="${classesdir}">
<classpath refid="classpath" />
</javac>
</target>
<target name="get-artifacts" depends="compile-deploy-service,get-artifacts-windows"/>
<!-- 本步骤的目的是生成客户端的stub文件,是项目的第二个步骤,本步骤的输出文件为
在${classesdir}下面自动生成了如下的文件
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
-->
<target name="get-artifacts-windows">
<exec executable="${javaee.home}/bin/wsimport.bat">
<arg line="-keep -d ${classesdir} http://${javaee.server.name}:${javaee.server.port}/Hello/HelloService?WSDL" />
</exec>
</target>
<!-- 本步骤的目的是编译服务器类,并自动产生wsdl.是项目的第一个步骤,本步骤的主要输出文件为
${autodeploydir}\endpoint\Hello.class
${autodeploydir}\domain1\autodeploy\endpoint\Hello.class_deployed
在d:\Sun\SDK\domains\domain1\applications\j2ee-modules
下建立一个endpoint_Hello目录,
在d:\Sun\SDK\domains\domain1\generated\ejb\j2ee-modules建立一个
endpoint_Hello目录,这个目录下又建立了一个endpoint\jaxws目录,里面有如下内容
GetHello.java
GetHelloResponse.java
GetHello.class
GetHelloResponse.class
在D:\Sun\SDK\domains\domain1\generated\xml\j2ee-modules\下建立一个
endpoint_Hello目录,这个目录下又有一个WEB-INF子目录,内容为
wsdl子目录
sun-web.xml 文件
web.xml 文件
webservices.xml 文件
wsdl子目录下又有两个文件
HelloService.wsdl
HelloService_schema1.xsd
当我们在浏览器输入http://localhost:8080/Hello/HelloService?WSDL时,显示的正是
这个domains\domain1\generated\xml\j2ee-modules\endpoint_Hello\WEB-INF\wsdl
文件的内容
-->
<target name="compile-deploy-service">
<mkdir dir="${classesdir}" />
<echo message="${javaee.home}" />
<javac
srcdir="./src"
includes="endpoint/**"
destdir="${autodeploydir}"
classpath="${javaee.home}/lib/j2ee.jar"
/>
<waitfor maxwait="100" maxwaitunit="second">
<or>
<available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
<available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
</or>
</waitfor>
<condition property="deploy_succeeded">
<available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
</condition>
<condition property="deploy_failed">
<available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
</condition>
</target>
<!-- 以下的任务用于清理和卸载-->
<target name="clean">
<delete dir="${classesdir}"/>
</target>
<target name="restore">
<delete>
<fileset dir="${autodeploydir}/endpoint" includes="Hello*.*"/>
</delete>
</target>
<target name="undeploy">
<antcall target="restore"/>
</target>
</project>
3、运行结果
在myeclipse里执行这个刚才编写的buildtest.xml脚本,console里的输出为
Buildfile: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\buildtest.xml
compile-deploy-service:
[mkdir] Created dir: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
[echo] d:/Sun/SDK
[javac] Compiling 1 source file to D:\Sun\SDK\domains\domain1\autodeploy
get-artifacts-windows:
get-artifacts:
compile-client:
[javac] Compiling 1 source file to D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
run-client-windows:
[exec] Hello result = Hello Administrator!
run-client:
all:
BUILD SUCCESSFUL
Total time: 50 seconds
也执行成功
/*title: web service入门学习笔记(六)
**date: 2007/01/19
**author:laomai
**url: http://blog.csdn.net/laomai/
*/







