0 0

请问用web service直接传输序列化的java对象妥当不妥当?0

刚接触web service,对web service的了解程度仅限于知道,通过web service可以实现远程方法的调用,其它几乎一无所知。
不像rmi,我发现web service调用方法的时候,必须在方法参数中显示申明参数的类型,貌似不支持多态?
为了让它支持多态,我简单的创建了这样一个java类:


@XmlAccessorType(XmlAccessType.FIELD)
public class TransferModel {

	public TransferModel() {

	}

	public TransferModel(Object obj) {
		this.setObject(obj);
	}

	private byte[] bytes;

	private Class<?> objClass;

	public void setObject(Object obj) {
		this.objClass = obj.getClass();
		ByteArrayOutputStream bo = null;
		ObjectOutputStream oo = null;
		try {
			bo = new ByteArrayOutputStream();
			oo = new ObjectOutputStream(bo);
			oo.writeObject(obj);
			bytes = bo.toByteArray();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			try {
				bo.close();
				oo.close();
			} catch (Exception e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}

	public Object getObject() {
		if (bytes == null) {
			return null;
		}
		ByteArrayInputStream bin = null;
		ObjectInputStream objIn = null;
		try {
			bin = new ByteArrayInputStream(bytes);
			objIn = new ObjectInputStream(bin);
			return objIn.readObject();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			try {
				bin.close();
				objIn.close();
			} catch (Exception e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}

	}

	public Class<?> getObjectClass() {
		return objClass;
	}
}

这里,我把java对象序列化为二进制数据,再通过web service进行传输,请问这样做会不会带来问题?
2012年8月04日 14:54

2个答案 按时间排序 按投票排序

0 0

传递序列化的对象时可以的,不过,这样没啥意义和好处。
只传递一个对象的话,完全可以传递一个XML文件,文件里包含这个对象的所有属性值,这样接收方也好接收和解析。如果是传递多个对象的话,你的序列化就不太好使了,这时候就需要传递序列的LIST等对象,个人建议不要使用。还是传递XML文件比较好

2012年8月08日 16:43
0 0

webservice原理:
传递数据:xml
传输协议:soap(soap=http+xml。基于http协议传输xml数据)
任何接口交互,只要搞清楚以上两点就融会贯通了。
如果想比较深入了解webservice,可参考我的博文(基于axis的源码分析)
http://dead-knight.iteye.com/blog/731024

2012年8月05日 22:39

相关推荐

Global site tag (gtag.js) - Google Analytics