微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

三、 复杂对象类型的WebService

1、这次我们编写复杂点的WebService方法,返回的数据是我们定义属性带getter、setter方法JavaBean,一维数组、二维数组等。

代码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import data.User;

/**
 * function:复杂类型数据的WebService:字节数组、返回一维int数组、二维String数组及自定义JavaBean对象等
 * @author hoojo
 * @createDate 2011-1-13 下午03:34:52
 * @file ComplexTypeService.java
 * @package 
 * @project Axis2WebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
public class ComplexTypeService {
	
	public String upload4Byte(byte[] b,int len) {
		String path = "";
		FileOutputStream fos = null;
		try {
			String dir = System.getProperty("user.dir");
			File file = new File(dir + "/" + new Random().nextInt(100) + ".jsp");
			fos = new FileOutputStream(file);
			fos.write(b,len);
			path = file.getAbsolutePath();
			System.out.println("File path: " + file.getAbsolutePath());
		} catch (Exception e) {
			e.printstacktrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printstacktrace();
			}
		}
		return path;
	}
	
	public int[] getArray(int i) {
		int[] arr = new int[i];
		for (int j = 0; j < i; j++) {
			arr[j] = new Random().nextInt(1000);
		}
		return arr;
	}
	
	public String[][] getTwoArray() {
		return new String[][] { { "中国","北京" },{ "日本","东京" },{ "中国","上海","南京" } };
	}
	
	public User getUser() {
		User user = new User();
		user.setAddress("china");
		user.setEmail("[email protected]");
		user.setName("jack");
		user.setId(22);
		return user;
	}
}

上面的WebService服务分别是传递字节完成数据上传,返回一维int数组和二维字符串数组,以及返回User JavaBean对象。

下面看看User Bean代码

package data;

import java.io.Serializable;

/**
 * function:User Entity
 * @author hoojo
 * @createDate Dec 16,2010 10:20:02 PM
 * @file User.java
 * @package com.hoo.entity
 * @project AxisWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
public class User implements Serializable {
	private static final long serialVersionUID = 677484458789332877L;
	private int id;
	private String name;
	private String email;
	private String address;
	
	//getter/setter

	@Override
	public String toString() {
		return this.id + "#" + this.name + "#" + this.email + "#" + this.address;
	}
}

值得注意的是这个User对象的package是data,如果是其它的package,你就需要在tomcat目录下的webapps中的axis2的WEB-INF目录下

创建一个data目录,和你的User对象的目录保持一致。否则你的WebService将会出现ClassNotFontException异常。然后重启你的tomcat,

虽然axis2支持热部署。

2、编写调用上面WebService的客户端代码代码如下:

package com.hoo.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.hoo.entity.User;

/**
 * function:复杂类型数据WebService客户端调用代码
 * @author hoojo
 * @createDate 2011-1-13 下午03:36:38
 * @file ComplexTypeServiceClient.java
 * @package com.hoo.service
 * @project Axis2WebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
public class ComplexTypeServiceClient {

	public static void main(String[] args) throws IOException {
		RPCServiceClient client = new RPCServiceClient();
		Options options = client.getoptions();
		String address = "http://localhost:8080/axis2/services/ComplexTypeService";
		EndpointReference epr = new EndpointReference(address);
		options.setTo(epr);
		
		QName qname = new QName("http://ws.apache.org/axis2","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);
		//System.out.println(read + "#" + len + "#" + new String(b));
		fis.close();
		Object[] result = client.invokeBlocking(qname,new Object[] { b,len },new Class[] { String.class });
		System.out.println("upload:" + result[0]);
		
		qname = new QName("http://ws.apache.org/axis2","getArray");
		result = client.invokeBlocking(qname,new Object[] { 3 },new Class[] { int[].class });
		int[] arr = (int[]) result[0];
		for (Integer i : arr) {
			System.out.println("int[] :" + i);
		}
		
		qname = new QName("http://ws.apache.org/axis2","getTwoArray");
		result = client.invokeBlocking(qname,new Object[] {},new Class[] { String[][].class });
		String[][] arrStr = (String[][]) result[0];
		for (String[] s : arrStr) {
			for (String str : s) {
				System.out.println("String[][]: " + str);
			}
		}
		
		qname = new QName("http://ws.apache.org/axis2","getUser");
		result = client.invokeBlocking(qname,new Class[] { User.class });
		User user = (User) result[0];
		System.out.println("User: " + user);
	}
}

上面的代码运行后的结果是:

upload:D:/tomcat-6.0.28/bin/28.jsp

int[] :548

int[] :201

int[] :759

String[][]: 中国

String[][]: 北京

String[][]: 日本

String[][]: 东京

String[][]: 中国

String[][]: 上海

String[][]: 南京

User: 22#jack#[email protected]#china

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐