关于java的BigDecimal类型经Hessian发布出现Bug的示例
我用的Hessian是Hessian 4.0.7版本
我们用它与spring集成
以下是web.xml内容
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/*-context.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet><servlet-name>remoting</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/*-remote.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>remoting</servlet-name><url-pattern>/remoting/*</url-pattern></servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
以下是Hessian发布配置(common-remote.xml):
<?xml version="1.0" encoding="gb2312"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- 此文件定义上下文内容,各模块需各自按以下指定的文件名在各模块的src文件夹下维护、管理上下文内容。此文件只作管理用途,不允许随意修改!--> <bean name="/remoteLoginService" ref="fap.loginService"/> <property name="serviceInterface" value="grp.pt.common.ILoginService"/> </bean> <!-- 用户管理 --> <bean name="/remoteUserService" ref="fap.userService"/> <property name="serviceInterface" value="grp.pt.common.IUserService"/> </bean> <bean name="/remoteTest" ref="testbean"/> <property name="serviceInterface" value="grp.pt.common.ITestService"/> </bean> </beans>
bean的定义我们只关注我们的测试类(common-context.xml):
<bean id="testbean" name="code">package grp.pt.common;import java.math.BigDecimal;public interface ITestService {public BigDecimal testBig();public float testfloat();public double testdouble();}
接口实现类的定义:
package grp.pt.common.bs;import grp.pt.common.ITestService;import java.math.BigDecimal;public class TestService implements ITestService {public BigDecimal testBig() {BigDecimal big=new BigDecimal( "10.90944" );System.out.println("服务端:"+big);return big;}public double testdouble(){return 10.123d;}public float testfloat(){return 10.123f;}}
测试代码:
public static void main(String[] args) throws Exception { String url = "http://localhost:8080/realware/remoting/remoteTest"; HessianProxyFactory factory = new HessianProxyFactory(); ITestService test = (ITestService) factory.create(ITestService.class,url); BigDecimal bi=test.testBig(); System.out.println(test.testdouble()); System.out.println(test.testfloat()); System.out.println(new BigDecimal(bi.toString())); BigDecimal big=new BigDecimal("10.123"); System.out.println(big.toString());}
最后服务发布后,测试结果为:
10.12310.1230.0000010.123
服务器打印:
服务端:10.90944
我们可以看到Hessian对于BigDemical类型的序列化后的传输是个大bug,它保留了它的精度,但数值全是0.
1 楼 xiaoZ5919 2011-07-26 我也遇到了类似的问题!
问题在BigDecimal类型的应该使用BigDecimalDeserializer,
在basic没有BigDecimal的deserializer,需要使用customer根据类名查找
public Deserializer getCustomDeserializer(Class cl)
Class serClass = Class.forName(cl.getName() + "HessianDeserializer",
false, cl.getClassLoader());
结果类名错误拼错导致找不到相应的反序列化类 2 楼 monkey9047 2011-08-02 有没有相应的解决方案呢? 3 楼 eighteencold 2011-08-02 monkey9047 写道有没有相应的解决方案呢?
这应该是Hessian的一个bug,用String传值吧 4 楼 步青龙 2012-05-31 解决办法:
在 hessian.jar 的 META-INF/hessian 目录下加入 serializers 和 deserializers 这两个文件, 两个文件的内容如下:
serializers
--------------------------
java.math.BigDecimal=com.caucho.hessian.io.StringValueSerializer
deserializers
--------------------------
java.math.BigDecimal=com.caucho.hessian.io.BigDecimalDeserializer