用axis开发webservice实践
李俊杰
什么是SOAP?
SOAP是一个基于XML的用于应用程序之间通信数据编码的传输协议。最初由微软和Userland Software提出,随着不断地完善和改进,SOAP很快被业界广泛应用。
什么是Axis?
Axis本质上就是一个SOAP引擎,提供创建服务器端、客户端和网关SOAP操作的基本框架。Axis目前版本是为Java编写的,不过为C++的版本正在开发中。
配置webservice具体步骤:
1. 安装weblogic。
2. 配置一个web应用,如“\\lijunjie\e$\software\bea\user_projects\domains\lijjdomain\applications\ cntapws “。
3. 从apache网站上下载axis-bin-1_2_1.tar.gz,解压后copy到cntapws下面。
4. 打开“http://localhost:7001/console“,配置cntapws 的web应用。
5. axis1.2和weblogic8.1有冲突,原因是weblogic 本身携带的webservice.jar使用的旧的javax.xml.soap.*,而不是像axis使用J2EE1.4新的版本。解决方案是在WEB-INF/weblogic.xml文件中如下的配置:
<weblogic-web-app>
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>
6. 按 http://localhost:8080/axis/happyaxis.jsp地址,可以显示你发布的webservice是否配置成功,如果显示必须的包缺失,则你的webservice不能正常运行。
开发webservice流程
1. 写普通的java类,该类中有你要发布到webservice上的有关方法。
2. 配置server-comfig.xml, 把你要发布的方法写入该文件。具体内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration>
<parameter name="adminPassword" value="admin"/>
<parameter name="attachments.Directory" value="C:\eclipse\workspace\bookstore\WEB-INF\attachments"/>
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
<parameter name="sendXsiTypes" value="true"/>
<parameter name="sendMultiRefs" value="true"/>
<parameter name="sendXMLDeclaration" value="true"/>
<parameter name="axis.sendMinimizedElements" value="true"/>
<requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session"/>
</handler>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request"/>
<parameter name="extension" value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<service name="Version" provider="java:RPC">
<parameter name="allowedMethods" value="getVersion"/>
<parameter name="className" value="org.apache.axis.Version"/>
</service>
<!--要配置的内容start,注意每一个要发布成webservice的类都要配置成一个service如下à
<service name="webserviceproxy" provider="java:RPC">
<parameter name="className" value="com.copia.webservice.server.proxy.WebServiceProxy"/>
<parameter name="allowedMethods" value="*"/>
<!—具体某个方法说明-->
<operation name="login" returnType="ns:LoginOutServiceBean">
<parameter name="branchNo" type="xsd:string"/>
<parameter name="userId" type="xsd:string"/>
<parameter name="password" type="xsd:string"/>
<parameter name="certificateNo" type="xsd:string"/>
</operation>
<!—如果该方法的参数有基本类型和对象类型,则不要声明,webservice能够找到相关的类及方法-->
<!—声明涉及到的所有类及其父类,引用类,还有它们的相关类,类似与对象树遍历,某个类声明缺失也会导致运行失败-->
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:LoginOutServiceBean"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.module.security.login.LoginOutServiceBean" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:User"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.client.User" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:GetBatchListInServiceBean"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.module.batch.service.GetBatchListInServiceBean" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:GetBatchListOutServiceBean"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.module.batch.service.GetBatchListOutServiceBean" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:BatchJobDefBean"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.module.batch.service.BatchJobDefBean" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:ValueObjectPage"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.voa.ValueObjectPage" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:ValueObjectColInfo"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.voa.ValueObjectColInfo" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:ValueObjectList"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.voa.ValueObjectList" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:PageInfo"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.voa.PageInfo" xmlns:ns="webserviceproxy"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
qname="ns:ValueObjectMetaData"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
type="java:com.copia.core.voa.ValueObjectMetaData" xmlns:ns="webserviceproxy"/>
</service>
<!--要配置的内容endà
<service name="AdminService" provider="java:MSG">
<parameter name="allowedMethods" value="AdminService"/>
<parameter name="enableRemoteAdmin" value="false"/>
<parameter name="className" value="org.apache.axis.utils.Admin"/>
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
</service>
<transport name="local">
<responseFlow>
<handler type="LocalResponder"/>
</responseFlow>
</transport>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
</transport>
</deployment>
3. 在地址栏输入http://localhost:7001/cntapws/services/webserviceproxy?wsdl,如没有什么错误,则表示你的webservice发布成功了。
Java webservice客户端编程
1. 启动你配置好的weblobic。
2. 进入dos界面,在你的临时目录(任意目录)下根据wsdl产生客户文件,如下指令:
E:\kk>java -Djava.ext.dirs=E:\software\bea\user_projects\domains\lijjdomain\appl
ications\axis\WEB-INF\axislib org.apache.axis.wsdl.WSDL2Java http://localhost:7001/cntapws/services/webserviceproxy?wsdl即可在kk目录下产生有关文件。
3. 把产生的文件copy到你的工程中,再写一个webservice调用测试类,如下:
public class TestWs {
public static void main(String[] args) {
WebServiceProxyService service = new WebServiceProxyServiceLocator();
try {
WebServiceProxy serviceProxy = service.getwebserviceproxy();
User user = new User();
user.setBranchNo("000001");
user.setClerkNo("1004");
GetBatchListInServiceBean in = new GetBatchListInServiceBean();
in.setCurPageNo(1);
String serviceName = "GetBatchListService";
GetBatchListOutServiceBean out = (GetBatchListOutServiceBean)serviceProxy.getBatchListService(user, in, serviceName);
BatchJobDefBean[] defBeans = out.getBatchJobDefBeans();
for(int i = 0 ; i < defBeans.length ; i++){
System.out.println(defBeans[i].getBatchName());
}
}catch(Exception ex){