日期:2014-05-19  浏览次数:20707 次

WebService的一个问题
使用axis2-1.6.1开发webservice(Eclipse-jee-3.7,axis2-1.6.1,Tomcat-6.0.24)

网上找的例子,
ComplexTypeService类:
Java code
import data.User;

public class ComplexTypeService {
    
    public String[][] getTwoArray() {
        return new String[][] { { "中国", "北京" }, { "日本", "东京" }, { "中国", "上海", "南京" } };
    }

    public User getUser() {
        User user = new User(0, "Jack", "Beijing");
        return user;
    }
}


User类:
Java code
package data;

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 1715338269623256997L;
    
    private int id;
    private String name;
    private String address;

    public User(int id, String name, String address) {
        super();
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}


下面就是部署了。

1.axis2.war包放到tomcat的webapps下解压为axis2的目录。
2.将ComplexTypeService.class文件放置到TOMCAT_HOME\webapps\axis2\WEB-INF\pojo目录下。
3.将User.class文件放置到TOMCAT_HOME\webapps\axis2\WEB-INF\data目录下(data就是User类所在的包名,自己新建的)。
网上查到另外的版本,说User.class文件应该放置在TOMCAT_HOME\webapps\axis2\WEB-INF\classes\data目录下。
这两个方法都试过了,可惜客户端调用的时候,都是找不到data.User类。

写客户端调用程序:
Java code
package webservice;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import data.User;

public class ComplexTypeServiceClient {

    public static void main(String[] args) {
        RPCServiceClient rpcServClient = null;

        try {
            rpcServClient = new RPCServiceClient();
        } catch (AxisFault e) {
            e.printStackTrace();
        }

        Options options = rpcServClient.getOptions();
        String url = "http://localhost:8080/axis2/services/ComplexTypeService";
        EndpointReference endpointRef = new EndpointReference(url);
        options.setTo(endpointRef);

        //第一个服务
        QName qname = new QName("http://ws.apache.org/axis2", "getTwoArray");
        Object[] result = null;    
        try {
            result = rpcServClient.invokeBlocking(qname, new Object[] {}, new Class[] { String[][].class });
        } catch (AxisFault e) {
            e.printStackTrace();
        }

        String[][] strss = (String[][]) result[0];
        for (String[] strs : strss) {
            for (String str : strs) {
                System.out.print(str + '\t');
            }
            System.out.println();
        }

        //第二个服务
        qname = new QName("http://ws.apache.org/axis2", "getUser");
        try {
            result = rpcServClient.invokeBlocking(qname, new Object[] {}, new Class[] { data.User.class });
        } catch (AxisFault e) {
            e.printStackTrace();
        }
        
        User user = (User)result[0];
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.get