关于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" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="fap.loginService"/>
<property name="serviceInterface" value="grp.pt.common.ILoginService"/>
</bean>

<!-- 用户管理 -->
<bean name="/remoteUserService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="fap.userService"/>
<property name="serviceInterface" value="grp.pt.common.IUserService"/>
</bean>

<bean name="/remoteTest" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="testbean"/>
<property name="serviceInterface" value="grp.pt.common.ITestService"/>
</bean>

</beans>


bean的定义我们只关注我们的测试类(common-context.xml):


<bean id="testbean" class="grp.pt.common.bs.TestService"></bean>


接口的定义:

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.123
10.123
0.00000
10.123


服务器打印:

服务端:10.90944

我们可以看到Hessian对于BigDemical类型的序列化后的传输是个大bug,它保留了它的精度,但数值全是0.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值