hessian参数序列化过程
程序员文章站
2022-03-16 15:10:41
...
hessian协议在调用api的时候会对参数进行序列化:
AbstractHessianOutput out = hessianClient.getHessianOutput(os);
out.call(methodName, args);
out.flush();
在out.call(methodName,args)的过程中hessian对args进行了序列化:
//com.caucho.hessian.io.HessianOutput
public void call(String method, Object []args)
throws IOException
{
startCall(method);
if (args != null) {
for (int i = 0; i < args.length; i++)
writeObject(args[i]);
}
completeCall();
}
/**
* 调用序列化器对参数进行序列化
*/
public void writeObject(Object object)
throws IOException
{
if (object == null) {
writeNull();
return;
}
Serializer serializer;
serializer = _serializerFactory.getSerializer(object.getClass());
serializer.writeObject(object, this);
}
hessian的序列化器工厂类:com.caucho.hessian.io.SerializerFactory
public Serializer getSerializer(Class cl)
throws HessianProtocolException
{
Serializer serializer;
serializer = (Serializer) _staticSerializerMap.get(cl);
if (serializer != null)
return serializer;
if (_cachedSerializerMap != null) {
synchronized (_cachedSerializerMap) {
serializer = (Serializer) _cachedSerializerMap.get(cl);
}
if (serializer != null)
return serializer;
}
for (int i = 0;
serializer == null && _factories != null && i < _factories.size();
i++) {
AbstractSerializerFactory factory;
factory = (AbstractSerializerFactory) _factories.get(i);
serializer = factory.getSerializer(cl);
}
if (serializer != null) {
}
else if (JavaSerializer.getWriteReplace(cl) != null)
serializer = new JavaSerializer(cl);
else if (HessianRemoteObject.class.isAssignableFrom(cl))
serializer = new RemoteSerializer();
else if (BurlapRemoteObject.class.isAssignableFrom(cl))
serializer = new RemoteSerializer();
else if (Map.class.isAssignableFrom(cl)) {
if (_mapSerializer == null)
_mapSerializer = new MapSerializer();
serializer = _mapSerializer;
}
else if (Collection.class.isAssignableFrom(cl)) {
if (_collectionSerializer == null) {
_collectionSerializer = new CollectionSerializer();
}
serializer = _collectionSerializer;
}
else if (cl.isArray())
serializer = new ArraySerializer();
else if (Throwable.class.isAssignableFrom(cl))
serializer = new ThrowableSerializer(cl);
else if (InputStream.class.isAssignableFrom(cl))
serializer = new InputStreamSerializer();
else if (Iterator.class.isAssignableFrom(cl))
serializer = IteratorSerializer.create();
else if (Enumeration.class.isAssignableFrom(cl))
serializer = EnumerationSerializer.create();
else if (Calendar.class.isAssignableFrom(cl))
serializer = CalendarSerializer.create();
else if (Locale.class.isAssignableFrom(cl))
serializer = LocaleSerializer.create();
else if (Enum.class.isAssignableFrom(cl))
serializer = new EnumSerializer(cl);
if (serializer == null)
serializer = getDefaultSerializer(cl);
if (_cachedSerializerMap == null)
_cachedSerializerMap = new HashMap(8);
synchronized (_cachedSerializerMap) {
_cachedSerializerMap.put(cl, serializer);
}
return serializer;
}
从这里大家可以看到Hessain支持的参数类型,如果不支持就会抛出:
Hessian exception:Serialized class #classFullName# must implement java.io.Serializable
上一篇: sadd