使用axis1.4编写Webservice入门

蜀川居 2011-12-30

使用axis1.4生成客户端调用web service的大概步骤如下:

  1. 新建WEB工程test,然后下载axis1.4,解压将其放于D盘.把axis1.4下的jar包及activation.jar,mail.jar这两个JAR包一并放入新工程下.
  2. 编写服务端代码
public class HelloService {
 public int add(int a,int b){
  return a+b;
 }
 public String sayHello(String name){
  System.out.println("hello :"+name);
  return "hello"+name;
 }
 public boolean saveEmp(Employee emp){
  System.out.println("empName:"+emp.getEmpName());
  /**
   * 可获取连接,执行保存数据操作.(省略)
   */
  return true;
 }
 public boolean saveEmp(Employee[]emp){
  for(int i=0;i<emp.length;i++){
   System.out.println(emp[i].getEmpName());
  }
  return true;
 }
 

3.配置server-config.wsdd

写道
<?xml version="1.0" encoding="UTF-8"?>

<deploymentxmlns="http://xml.apache.org/axis/wsdd/"xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<globalConfiguration>

<parametername="sendMultiRefs"value="true"/>

<parametername="disablePrettyXML"value="true"/>

<parametername="adminPassword"value="admin"/>

<parametername="attachments.Directory"value="E:\apache-tomcat-6.0.29\webapps\test\WEB-INF\attachments"/>

<parametername="dotNetSoapEncFix"value="true"/>

<parametername="enableNamespacePrefixOptimization"value="false"/>

<parametername="sendXMLDeclaration"value="true"/>

<parametername="attachments.implementation"value="org.apache.axis.attachments.AttachmentsImpl"/>

<parametername="sendXsiTypes"value="true"/>

<requestFlow>

<handlertype="java:org.apache.axis.handlers.JWSHandler">

<parametername="scope"value="session"/>

</handler>

<handlertype="java:org.apache.axis.handlers.JWSHandler">

<parametername="scope"value="request"/>

<parametername="extension"value=".jwr"/>

</handler>

</requestFlow>

</globalConfiguration>

<handlername="URLMapper"type="java:org.apache.axis.handlers.http.URLMapper"/>

<handlername="LocalResponder"type="java:org.apache.axis.transport.local.LocalResponder"/>

<handlername="Authenticate"type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>

<servicename="AdminService"provider="java:MSG">

<parametername="allowedMethods"value="AdminService"/>

<parametername="enableRemoteAdmin"value="false"/>

<parametername="className"value="org.apache.axis.utils.Admin"/>

<namespace>http://xml.apache.org/axis/wsdd/</namespace>

</service>

<servicename="Version"provider="java:RPC">

<parametername="allowedMethods"value="getVersion"/>

<parametername="className"value="org.apache.axis.Version"/>

</service>

<servicename="HelloService"provider="java:RPC">

<parametername="allowedMethods"value="*"/>

<parametername="className"value="com.lee.webservice.HelloService"/>

<typeMapping

xmlns:ns2="http://xml.apache.org/axis/wsdd/"

qname="ns2:Employee"

type="java:com.shich.test.Employee"

serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"

deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"

encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"/>

</service>

<transportname="http">

<requestFlow>

<handlertype="URLMapper"/>

<handlertype="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>

</requestFlow>

<parametername="qs:list"value="org.apache.axis.transport.http.QSListHandler"/>

<parametername="qs:wsdl"value="org.apache.axis.transport.http.QSWSDLHandler"/>

<parametername="qs.list"value="org.apache.axis.transport.http.QSListHandler"/>

<parametername="qs.method"value="org.apache.axis.transport.http.QSMethodHandler"/>

<parametername="qs:method"value="org.apache.axis.transport.http.QSMethodHandler"/>

<parametername="qs.wsdl"value="org.apache.axis.transport.http.QSWSDLHandler"/>

</transport>

<transportname="local">

<responseFlow>

<handlertype="LocalResponder"/>

</responseFlow>

</transport>

</deployment>

4.生成客户端

set Axis_Lib=d:\axis-1_4\lib 
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% 
set Output_Path=E: 
set Package=com.client.test
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% http://127.0.0.1:8081/test/services/HelloService?wsdl

 此时会生成

HelloService_PortType

HelloServiceService

HelloServiceServiceLocator

HelloServiceSoapBindingStub

Employee5个JAVA类

5编写客户端测试代码public class Test {

 public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {

HelloServiceServiceservice=newHelloServiceServiceLocator();

Employeeemp=newEmployee();

emp.setEmpName("jack");

service.getHelloService().saveEmp(emp);

}

}

相关推荐