Axis2系列之对象类型的简单例子【3】

之所以说是简单例子,是因为类文件仍在data文件夹下,但包括了一维、二维数组及返回值是对象类型

1.服务器端代码

首先建一个bean

package data;



import java.io.Serializable;



public class User implements Serializable {



	private static final long serialVersionUID = 1L;

	private int id;

	private String name;

	private String address;

	private String email;



	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;

	}



	public String getEmail() {

		return email;

	}



	public void setEmail(String email) {

		this.email = email;

	}



}

 注意:此bean有package

  而下面的service方法仍没有package

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Random;



import data.User;



public class SimpleService {



	public String upload4Byte(byte[] b, int len) {

		String path = "";

		FileOutputStream fos = null;

		String dir = System.getProperty("user.dir");

		File file = new File(dir + "/" + new Random().nextInt(100) + ".jsp");

		try {

			fos = new FileOutputStream(file);

			fos.write(b, 0, len);

			path = file.getAbsolutePath();

			System.out.println("file path:" + file.getAbsolutePath());

		} catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				fos.close();

			} catch (IOException e) {

				e.printStackTrace();

			}

		}

		return path;

	}



	public int[] getArray(int i) {

		int[] array = new int[i];

		for (int j = 0; j < i; j++) {

			array[j] = new Random().nextInt(1000);

		}

		return array;

	}



	public String[][] getTwoArray() {

		return new String[][] { { "中国", "北京" }, { "日本", "东京" }};

	}



	public User getUer() {

		User user = new User();

		user.setAddress("JingsanRoad");

		user.setEmail("[email protected]");

		user.setName("spark");

		user.setId(2);

		return user;

	}

}

将SimpleService.class文件拷贝到pojo文件夹下

值得注意的是这个User对象的package是datas,需要在tomcat目录下的webapps中的axis2的WEB-INF目录下创建一个datas目录,和你的User对象的目录保持一致。否则你的WebService将会出现ClassNotFontException异常。然后重启你的tomcat,虽然axis2支持热部署 

2.客户端代码

package com.client;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;



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 SimpleServiceClient {



	public static void main(String args[]) {

		RPCServiceClient client;

		try {

			client = new RPCServiceClient();

			Options options = client.getOptions();

			String address = "http://localhost:8080/axis2/services/SimpleService";

			String defualNameSpace = "http://ws.apache.org/axis2";

			EndpointReference epr = new EndpointReference(address);

			options.setTo(epr);



			QName qname = new QName(defualNameSpace, "upload4Byte");

			String path = System.getProperty("user.dir");

			File file = new File(path + "/WebRoot/index.jsp");

			FileInputStream fis = new FileInputStream(file);

			int len = (int) file.length();

			byte[] b = new byte[len];

			int read = fis.read(b);

			fis.close();

			Object[] result = client.invokeBlocking(qname, new Object[] { b,

					len }, new Class[] { String.class });

			System.out.println("upload4Byte:" + result[0]);



			qname = new QName(defualNameSpace, "getTwoArray ");

			result = client.invokeBlocking(qname, new Object[] {},

					new Class[] { String[][].class });

			String[][] strs = (String[][]) result[0];

			System.out.println("getTwoArray :");

			for (String[] s : strs) {

				for (String str : s) {

					System.out.println(str);

				}

			}

			// for(int i=0;i<strs.length;i++){

			// String[] temp=strs[i];

			// for(int j=0;j<temp.length;j++){

			// System.out.println(temp[j]);

			// }

			// }

			qname = new QName(defualNameSpace, "getArray ");

			result = client.invokeBlocking(qname, new Object[] { 3 },

					new Class[] { int[].class });

			int[] arr = (int[]) result[0];

			System.out.println("getArray: ");

			for (int i = 0; i < arr.length; i++) {

				System.out.println(arr[i]);

			}

			

			qname = new QName(defualNameSpace, "getUer");

			result = client.invokeBlocking(qname, new Object[] {},

					new Class[] { User.class });

			System.out.println("getUer:");

			User user = (User) result[0];

			System.out.println("name=" + user.getName());

			System.out.println("address=" + user.getAddress());

		} catch (AxisFault e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

}

 输出结果:

upload4Byte:C:\Program Files\apache-tomcat-6.0.24\bin\69.jsp
getTwoArray :
中国
北京
日本
东京
getArray:
231
56
411
getUer:
name=spark
address=JingsanRoad

你可能感兴趣的:(axis2)