我用的Hessian是Hessian 4.0.7版本
我们用它与spring集成
以下是web.xml内容
以下是Hessian发布配置(common-remote.xml):
bean的定义我们只关注我们的测试类(common-context.xml):
接口的定义:
接口实现类的定义:
测试代码:
最后服务发布后,测试结果为:
服务器打印:
我们可以看到Hessian对于BigDemical类型的序列化后的传输是个大bug,它保留了它的精度,但数值全是0.
我们用它与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.
本文介绍了使用Hessian 4.0.7版本与Spring集成过程中遇到的BigDecimal类型序列化问题,通过具体示例展示了发布服务及客户端调用时BigDecimal类型的精度保留问题。
287

被折叠的 条评论
为什么被折叠?



