一:生成Web Service实现类
Web Service类与java普通类没有太大区别,只是在类前面加上各项annotation然后编译发布即成为Web Service。annotation是JDK1.5版本提供的新功能。WebLogic9使用了此功能。
示范程序:
package com.mycompany.webservices.moto;
// Import the standard JWS annotation interfaces
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
// Import the WebLogic-specific JWS annotation interface
import weblogic.jws.WLHttpTransport;
// Standard JWS annotation that specifies that the portType name of the Web
// Service is "BOLServicePortType", its public service name is "BOLService",
// and the targetNamespace used in the generated WSDL is "http://127.0.0.1"
@WebService(serviceName="BOLService", name="BOLServicePortType", targetNamespace="http://127.0.0.1")
// Standard JWS annotation that specifies this is a document-literal-wrapped
// Web Service
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
// WebLogic-specific JWS annotation that specifies the context path and service
// URI used to build the URI of the Web Service is "MOTOWebService/BOLService"
@WLHttpTransport(contextPath="MOTOWebService", serviceUri="BOLService", portName="BOLServicePort")
public class BOLServiceImpl {
public String testAction(String s) {
System.out.println("The testAction in BOL webservice has "+
"been invoked with arguments: " + s );
String returnValue = "The testAction in BOL webservice has "+
"been invoked with arguments: " + s ;
return returnValue;
}
}
说明:
上面的黑体字是JAVA普通类的基础上增加上去的。没有这些annotation,这就是一普通的JAVA类。
类中默认所有public方法是对外公布的,如果想控制public方法是否对外公布,可以使用@WebMethod annotation来进行控制。
对Web Service类进行编译,发布
整体目录结构为:
MOTOWebService 工作目录
output 编译输出目录
src 源代码
build.xml ant编译文件
setEnv.cmd 内容为:call X:\bea91\weblogic91\samples\domains\wl_server\setExamplesEnv.cmd
build.xml文件内容参考:
<project name="webservices-moto" default="all">
<!-- set global properties for this build -->
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="manager001" />
<property name="wls.hostname" value="127.0.0.1" />
<property name="wls.port" value="7001" />
<property name="wls.server.name" value="AdminServer" />
<property name="ear.deployed.name" value="MOTOWebService" />
<property name="motowebservice-output" value="output" />
<property name="ear-dir" value="${motowebservice-output}/MOTOWebService" />
<property name="ear-name" value="MOTOWebService.ear" />
<taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" />
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/>
<target name="all" depends="clean,build-service,deploy" />
<target name="clean" depends="undeploy">
<delete dir="${motowebservice-output}"/>
</target>
<target name="build-service">
<jwsc srcdir="src" destdir="${ear-dir}" keepGenerated="true">
<jws file="com/mycompany/webservice/moto/BOLServiceImpl.java" />
</jwsc>
<jar jarfile="${motowebservice-output}/${ear-name}" basedir="${ear-dir}" />
</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>
</project>
注意内容中的黑体字,为编译Web Service源代码的内容。
先执行setEnv.cmd以设置环境,然后可以输入ant build-service仅编译文件,如果直接输入ant将自动undeploy,然后编译,然后再deploy,发布应用到WebLogic域中。
在控制台中查看WSDL
打开WebLogic的console,在左边树中选择“Deployments”;然后在右边的页面单击“MOTOWebService”超链接;在出现的页面中再单击最下方的“BOLService”。
在新页面中,选择标签“Testing”,如果出现的BOLService的Comments为“This Web Service isnot currentlyavailable”,则需要重新启动WebLogic,正常情况,可以扩展此处的BOLService节点,然后可以查看它的WSDL等内容。
二:生成测试客户端
示范程序如下:
package client;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class Client {
public static void main(String[] args) throws Exception{
String wsdl = args[0];
BOLService service = new BOLService_Impl(wsdl);
BOLServicePortType port = service.getBOLServicePort();
String result = port.testAction("Hi there!");
System.out.println("the result is:" + result);
}
}
黑体字为生成服务接口。如果有不明白的,可以参考ant编译输出目录output\clientclass 目录下面的java文件与class进行比照。
对客户端程序进行编译,运行
目录结构为:
MotoWebServiceClient 工作目录
output 编译输出
src 源代码
build.xml ant编译文件
setEnv.cmd 内容为:call X:\bea91\weblogic91\samples\domains\wl_server\setExamplesEnv.cmd
build.xml文件内容参考:
<project name="webservices-client" default="all">
<!-- set global properties for this build -->
<property name="wls.hostname" value="127.0.0.1" />
<property name="wls.port" value="7001" />
<property name="motowebservice-client-output" value="output" />
<property name="clientclass-dir" value="${motowebservice-client-output}/clientclass" />
<path id="client.class.path">
<pathelement path="${clientclass-dir}"/>
<pathelement path="${java.class.path}"/>
</path>
<taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" />
<target name="all" depends="clean,client,run"/>
<target name="clean">
<delete dir="${motowebservice-client-output}"/>
</target>
<target name="client">
<clientgen wsdl=http://${wls.hostname}{wls.port}/MOTOWebService/BOLService?WSDL destDir="${clientclass-dir}" packageName="client"/>
<javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="client/**/*.java"/>
<javac srcdir="src" destdir="${clientclass-dir}" includes="client/**/*.java"/>
</target>
<target name="run">
<java fork="true" classname="client.Client" failonerror="true" >
<classpath refid="client.class.path"/>
<arg line=HTTP://${wls.hostname}{wls.port}/MOTOWebService/BOLService?WSDL />
</java>
</target>
</project>
黑体字为通过WSDL生成java源文件与class的内容。用到了clientgen,具体可以参考bea公司提供的资料。
先执行setEnv.cmd以设置环境,然后可以输入ant client仅编译文件;如果直接输入ant将自动编译,然后运行;输入ant run则只是运行程序。
到此,WebLogic9中的Web Service基本开发工作结束。