[开发前的准备工作]
1. 创建一个Domain, 记得要选中对Web Service的支持, 更多详细信息可参考: http://download.oracle.com/docs/cd/E14571_01/web.1111/e13758/setenv.htm#CACBAAGE.
选择 「开始」菜单\程序\Oracle WebLogic\WebLogic Server 11gR1\Tools\Configuration Wizard , 启动 Fussion Middleware Configuration Wizard.
第一页欢迎页, 选择 Create a new WebLogic domain;
第二页, 选择 Generate a domain configured automatically to support the following products:
复选下列这项:
WebLogic Advanced Web Services for JAX-WS Extension
然后一路选择Next, 这里为了简化只包含了一个AdminServer, 用户名和密码分别为 weblogic/weblogic1.
创建Domain完毕后, 启动此WebLogic, 并进入控制台 http://localhost:7001/console.
2. 启动Eclipse(使用纯Eclipse即可, 例如Eclipse IDE for Java Developers).
选择菜单项 Window -> Preferences, 展开节点Ant > Runtime, 然后在 Classpath 标签页的 Global Entries 下添加如下几个jar包(点击右侧按钮 Add External JARs…进行安装):
$JAVA_HOME\lib\tools.jar
$BEA_HOME\wlserver_10.3\server\lib\weblogic.jar
$BEA_HOME\wlserver_10.3\server\lib\wseeclient.jar,
如下图所示:
至此开发环境已经准备完毕.
[开发服务端]
下面来开发完成一个最简单的Web Service.
使用Eclipse新建一个Java项目, 名称为wls_hellows, 此项目的JDK必须选择JDK 1.6, 否则项目可能会出现问题, 例如无法编译.
在src目录下新建包examples.webservices.hello_world.
在此包下新建类文件HelloWorldImpl, 代码如下所示:
package examples.webservices.hello_world; // Import the @WebService annotation import javax.jws.WebService; @WebService(name = "HelloWorldPortType", serviceName = "HelloWorldService") /** * This JWS file forms the basis of simple Java-class implemented WebLogic * Web Service with a single operation: sayHelloWorld * http://download.oracle.com/docs/cd/E14571_01/web.1111/e13758/use_cases.htm */ public class HelloWorldImpl { // By default, all public methods are exposed as Web Services operation public String sayHelloWorld(String message) { try { System.out.println("sayHelloWorld:" + message); } catch (Exception ex) { ex.printStackTrace(); } return "Here is the message: '" + message + "'"; } }
然后在项目根目录下创建文件build.xml, 内容如下所示
<project name="webservices-hello_world" default="all"> <!-- set global properties for this build --> <property name="wls.username" value="weblogic" /> <property name="wls.password" value="weblogic1" /> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="wls.server.name" value="AdminServer" /> <property name="ear.deployed.name" value="helloWorldEar" /> <property name="example-output" value="output" /> <property name="ear-dir" value="${example-output}/helloWorldEar" /> <property name="clientclass-dir" value="${example-output}/clientclasses" /> <path id="client.class.path"> <pathelement path="${clientclass-dir}" /> <pathelement path="${java.class.path}" /> </path> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy" /> <target name="all" depends="clean,build-service,deploy,client" /> <target name="clean" depends="undeploy"> <delete dir="${example-output}" /> </target> <target name="build-service"> <jwsc srcdir="src" destdir="${ear-dir}"> <jws file="examples/webservices/hello_world/HelloWorldImpl.java" type="JAXWS" /> </jwsc> </target> <target name="deploy"> <wldeploy action="deploy" name="${ear.deployed.name}" source="${ear-dir}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="undeploy"> <wldeploy action="undeploy" name="${ear.deployed.name}" failonerror="false" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="client"> <clientgen wsdl="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.hello_world.client" type="JAXWS" /> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java" /> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/hello_world/client/**/*.java" /> </target> <target name="run"> <java classname="examples.webservices.hello_world.client.Main" fork="true" failonerror="true"> <classpath refid="client.class.path" /> <arg line="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService" /> </java> </target> </project>
. 打开此文件, 在Outline视图中的all节点上(Ant的Target)右键点击Run As > Ant Build, 即可启动并完成下列几个操作:
clean 移除部署, 删除生成的临时文件
build-service 编译服务
deploy 部署生成的Web Service到WebLogic服务器
client 生成并编译客户端文件
, 正确运行后的一个输出如下所示:
Buildfile: E:\workspace\wls_hellows\build.xml [wldeploy] weblogic.Deployer -verbose -noexit -name helloWorldEar -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -password ******** -undeploy [wldeploy] weblogic.Deployer invoked with options: -verbose -noexit -name helloWorldEar -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -undeploy [wldeploy] <Oct 25, 2010 5:49:19 AM VET> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating undeploy operation for application, helloWorldEar [archive: null], to AdminServer .> [wldeploy] [Deployer:149001]No application named ‘helloWorldEar’ exists for operation undeploy clean: build-service: [jwsc] JWS: processing module /examples/webservices/hello_world/HelloWorldImpl [jwsc] Parsing source files [jwsc] Parsing source files [jwsc] 1 JWS files being processed for module /examples/webservices/hello_world/HelloWorldImpl [jwsc] JWS: E:\workspace\wls_hellows\src\examples\webservices\hello_world\HelloWorldImpl.java Validated. [jwsc] Processing 1 JAX-WS web services… [jwsc] warning: Annotation types without processors: [javax.xml.bind.annotation.XmlRootElement, javax.xml.bind.annotation.XmlAccessorType, javax.xml.bind.annotation.XmlType, javax.xml.bind.annotation.XmlElement] [jwsc] 1 warning [jwsc] Compiling 3 source files to C:\DOCUME~1\jackyl\LOCALS~1\Temp\_gwf3vm [jwsc] Building jar: E:\workspace\wls_hellows\output\helloWorldEar\examples\webservices\hello_world\HelloWorldImpl.war [jwsc] Created JWS deployment outputFile: E:\workspace\wls_hellows\output\helloWorldEar\examples\webservices\hello_world\HelloWorldImpl.war [jwsc] [EarFile] Application File : E:\workspace\wls_hellows\output\helloWorldEar\META-INF\application.xml [AntUtil.deleteDir] Deleting directory C:\DOCUME~1\jackyl\LOCALS~1\Temp\_gwf3vm deploy: [wldeploy] weblogic.Deployer -verbose -noexit -name helloWorldEar -source E:\workspace\wls_hellows\output\helloWorldEar -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -password ******** -deploy [wldeploy] weblogic.Deployer invoked with options: -verbose -noexit -name helloWorldEar -source E:\workspace\wls_hellows\output\helloWorldEar -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -deploy [wldeploy] <Oct 25, 2010 5:49:24 AM VET> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating deploy operation for application, helloWorldEar [archive: E:\workspace\wls_hellows\output\helloWorldEar], to AdminServer .> [wldeploy] Task 6 initiated: [Deployer:149026]deploy application helloWorldEar on AdminServer. [wldeploy] Task 6 completed: [Deployer:149026]deploy application helloWorldEar on AdminServer. [wldeploy] Target state: deploy completed on Server AdminServer [wldeploy] [wldeploy] Target Assignments: [wldeploy] + helloWorldEar AdminServer client: [clientgen] [clientgen] *********** jax-ws clientgen attribute settings *************** [clientgen] [clientgen] wsdlURI: http://localhost:7001/HelloWorldImpl/HelloWorldService?WSDL [clientgen] packageName : examples.webservices.hello_world.client [clientgen] destDir : E:\workspace\wls_hellows\output\clientclasses [clientgen] [clientgen] *********** jax-ws clientgen attribute settings end *************** [clientgen] Consider using <depends>/<produces> so that wsimport won’t do unnecessary compilation [clientgen] parsing WSDL… [clientgen] generating code… [clientgen] compiling code… all: BUILD SUCCESSFUL Total time: 7 seconds |
. 至此, 整个Web Service的服务端已经开发部署完毕, 并可在WebLogic Console的Deployments下看到此应用.
[开发客户端]
接着开发一个Java客户端来调用此Web Service.
刷新项目目录, 可以看到如下所示的目录结构:
可以看到已经自动生成了服务端代码(位于output/helloWorldEar下)和客户端骨架代码(基于JAX-WS), 让我们在 output/clientclasses/examples/webservices/hello_world/client 目录下新建一个Java文件 Main.java:
package examples.webservices.hello_world.client; import javax.xml.namespace.QName; public class Main { public static void main(String[] args) { // Take argument from command-line HelloWorldService service = new HelloWorldService(); HelloWorldPortType port = service.getHelloWorldPortTypePort(); String result = port.sayHelloWorld("Call webService from Java client test"); System.out.println("result = " + result); } }
接着转向build.xml, 运行一次名为client的Ant target, 即可将此文件编译完毕. 最后运行名为run的Ant target, 则可以看到调用Web Service的完整输出如下:
Buildfile: E:\workspace\wls_hellows\build.xml
run:
[java] result = Here is the message: ‘Call webService from Java client test’
BUILD SUCCESSFUL
Total time: 2 seconds
. Not bad, 至此第一个简单的Web Service服务端和客户端已经开发完毕了.