Axis入门篇
1. 下载Axis,下载地址http://ws.apache.org/
2. 安装。Jboss中安装为例。
1) 在web部署目录下建立axis.war文件夹(名字可以任意取)。
2) 将下载的axis包解压找到webapp目录,将目录中的axis下的文件拷贝至axis.war下,启动JBoss,在浏览器中验证是否安装成功。输入:
http://localhost/axis
3. 开发环境Eclipse3.1,将axis包都加到工程类路径中来。建立一个run,在Main Class中输入:org.apache.axis.client.AdminClient,这个是用来部署Web Service的。在arguments中输入-lhttp://localhost/axis/services/AdminService src/deploy.wsdd
第一个例子
package net.wide.axis.service;
public class Greet { private int count = 0; public String sayHello(String name){ return "Hello " + name + " you are the " + (++count); } } |
编写wsdd文件,在src文件夹下建立deploy.wsdd文件,内容如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="Greet" provider="java:RPC"> <parameter name="scope" value="application"/> <parameter name="className" value="net.wide.axis.service.Greet"/> <parameter name="allowedMethods" value="sayHello"/> </service> </deployment> |
描述文件对这个service描述的内容有,
1) 服务名
2) 作用域范围,包括:
request:每次请求都是一个新的实例;
session:一个会话中一个实例;
application:全局一个实例。
点run完成部署,同时要把class文件拷贝至WEB-INF/classes/目录下。
重起JBoss,测试非常简单直接在浏览器中输入:http://localhost/axis/services/Greet?method=sayHello&name=me
就可以看到返回的结果了,是xml格式的。
在客户端编码测试:
package net.wide.axis.client;
import java.rmi.RemoteException;
import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call; import org.apache.axis.client.Service;
public class GreetClient { public static void main(String[] args) throws ServiceException, RemoteException { String endPoint = "http://localhost/axis/services/Greet?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endPoint); call.setOperationName(new QName("http://Greet","sayHello")); String result = (String) call.invoke(new Object[]{"zcy"}); System.out.println(result); } } |
第二个例子:简单对象
对于自定义对象需要序列化
1)SimpleObject.java
package net.wide.axis.bean;
import java.io.Serializable;
public class SimpleObject implements Serializable { private static final long serialVersionUID = -8764373511157843460L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } |
SimpleObjectCreator.java
package net.wide.axis.service;
import net.wide.axis.bean.SimpleObject;
public class SimpleObjectCreator { public SimpleObject createSimpleObject(String name){ System.out.println("创建SimpleObject..."); SimpleObject simpleObject = new SimpleObject(); simpleObject.setName(name); return simpleObject; } } |
书写描述文件
<service name="SimpleObjectCreator" provider="java:RPC"> <parameter name="className" value="net.wide.axis.service.SimpleObjectCreator"/> <parameter name="allowedMethods" value="createSimpleObject"/> <beanMapping xmlns:myNS="urn:Creator" qname="myNS:SimpleObject" languageSpecificType="java:net.wide.axis.bean.SimpleObject"/> </service> |
测试一样可以直接用浏览器来进行,下面是编码测试:
package net.wide.axis.client;
import java.rmi.RemoteException;
import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException;
import net.wide.axis.bean.SimpleObject;
import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory;
public class SimpleObjectCreatorClient {
/** * @param args * @throws ServiceException * @throws RemoteException */ public static void main(String[] args) throws ServiceException, RemoteException { String endPoint = "http://localhost/axis/services/SimpleObjectCreator?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endPoint); QName qname = new QName("urn:Creator","SimpleObject"); call.registerTypeMapping(SimpleObject.class, qname, new BeanSerializerFactory(SimpleObject.class, qname), new BeanDeserializerFactory(SimpleObject.class, qname)); call.setOperationName("createSimpleObject"); SimpleObject simpleObject = (SimpleObject) call.invoke(new Object[]{"Hello"}); System.out.println(simpleObject.getName()); } } |