环境 :
服务端: hessian 4.0.38 , spring 4.3.6 ; spring文档指出spring4.0以上的版本只能使用hessian 4.0以上的版本
客户端: hessian3.1.5
异常 :
服务端没有异常 ,客户端请求服务端抛出以下异常,无法获取请求结果。
Caused by : com.caucho.hessian.io.HessianProtocolException: expected hessian reply at 0x48(H)
at com.caucho.hessian.io.Hessian2Input.error(Hessian2Input.java:2701)
at com.caucho.hessian.io.Hessian2Input.startReply(Hessian2Input.java:405)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:218)
分析 : 从异常可以看出服务端使用的是hessian2协议(Hessian2Input可以看出) ,客户端使用hessian1协议无法解析结果。
处理 : 让服务端和客户端使用同一版本的jar包;不能换包可以使用一下方法,重写org.springframework.remoting.caucho.HessianServiceExporter的doInvoke方法,实例如下:
importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.remoting.caucho.HessianServiceExporter;importcom.caucho.hessian.io.AbstractHessianInput;importcom.caucho.hessian.io.AbstractHessianOutput;importcom.caucho.hessian.io.Hessian2Input;importcom.caucho.hessian.io.Hessian2Output;importcom.caucho.hessian.io.HessianInput;importcom.caucho.hessian.io.HessianOutput;importcom.caucho.hessian.server.HessianSkeleton;public class ExporterService extendsHessianServiceExporter {//是否使用hessian2协议
private booleanenableHessian2;public booleanisEnableHessian2() {returnenableHessian2;
}public void setEnableHessian2(booleanenableHessian2) {this.enableHessian2 =enableHessian2;
}
@Overridepublic voidhandleRequest(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {//可以进行权限拦截
super.handleRequest(request, response);
}
@Overrideprotected void doInvoke(HessianSkeleton skeleton, InputStream is, OutputStream os) throwsThrowable {//使用 hessian1协议
AbstractHessianInput in = newHessianInput(is);
AbstractHessianOutput out= newHessianOutput(os);//使用hessian2协议
if(isEnableHessian2()) {
in= newHessian2Input(is);
out= newHessian2Output(os);
}
skeleton.invoke(in, out);
}
}
参考 : http://blog.csdn.net/chjttony/article/details/6567511