代码段:
public static void main(String[] args) throws IOException {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("1","2");
byte[] userByte = getData(hashMap);
UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream( userByte);
HessianInput input = new HessianInput(inputStream);
Object o = input.readObject(Serializable.class);
Assert.assertNotNull(o);
}
private static <V extends Serializable> byte[] getData(V v) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
//Hessian的序列化输出
HessianOutput ho = new HessianOutput(os);
ho.writeObject(v);
ho.flush();
os.flush();
return os.toByteArray();
}
错误原因:
com.caucho.hessian.io.HessianInput
/**
* Reads an object from the input stream with an expected type.
*/
public Object readObject(Class cl)
throws IOException
{
if (cl == null || cl == Object.class)
return readObject();
int tag = read();
switch (tag) {
case 'N':
return null;
case 'M': {
String type = readType();
// hessian/3386 ----------------------------------------------------------- 原因在这里
if ("".equals(type)) {
Deserializer reader;
reader = _serializerFactory.getDeserializer(cl);
return reader.readMap(this);
}
else {
Deserializer reader;
reader = _serializerFactory.getObjectDeserializer(type);
return reader.readMap(this);
}
}
case 'V': {
String type = readType();
int length = readLength();
Deserializer reader;
reader = _serializerFactory.getObjectDeserializer(type);
if (cl != reader.getType() && cl.isAssignableFrom(reader.getType()))
return reader.readList(this, length);
reader = _serializerFactory.getDeserializer(cl);
Object v = reader.readList(this, length);
return v;
}
case 'R': {
int ref = parseInt();
return _refs.get(ref);
}
case 'r': {
String type = readType();
String url = readString();
return resolveRemote(type, url);
}
}
_peek = tag;
// hessian/332i vs hessian/3406
//return readObject();
Object value = _serializerFactory.getDeserializer(cl).readObject(this);
return value;
}
这个type 在map传输过程中,不知为何会是"",导致不能反序列化,但是继承HashMap的实现类,或者实现序列化接口的类,均不会出现此问题
找了很久,发现一篇2009年的博客,有具体信息描述
链接:http://www.blogjava.net/BlueDavy/archive/2009/08/06/290003.html