从maven库中下载的hession包缺少相关序列化和反序列化的配置,实际使用中BigDecimal会丢失精度,可以从Hessian-4.0.37jar包中获取,文件位于\META-INF\hessian,将其粘贴到本地库的jar包中,即可修复此bug。
hession对List、Map、BigDecimal等非基础类型进行了特殊处理,不能直接使用自定义泛型、使用继承List、Map的自定义类在传输过程中会丢失额外增加的字段数据。但可以参考BigDecimalDeserializer和StringValueSerializer等进行实现,然后在\META-INF\hessian进行配置。 hessian支持的数据类型请参考hessian java 类型 接口定义实例如下
public interface ZcpayService { /** * 获取菜单信息 * @return 菜单信息集合 */ List<ZcpayMenu> getMenus(); }需要实现spring的@Service接口,托管到spring中
@Service public class ZcpayServiceImpl implements ZcpayService { @Autowired private ZcpayMenuService zcpayMenuService; @Override public List<ZcpayMenu> getMenus() { return zcpayMenuService.selectByExample(null); }PS:如果系统使用了shiro或其他权限过滤,需要将/hessian/**地址设置为不校验权限。shiro配置如下
<!-- shiroFilter其他配置省略 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> ....... <property name="filterChainDefinitions"> <value> /hessian/**=anon </value> </property> </bean>需要引用服务端API的maven依赖和hessian
<!-- hessian --> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> <!-- 服务端api --> <dependency> <groupId>com.whty</groupId> <artifactId>zcpay-api</artifactId> <version>1.0.0.zc</version> </dependency>spring配置:
<?xml version="1.0" encoding="UTF-8" ?> <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.xsd" default-lazy-init="false"> <description>Spring hessian配置</description> <bean id="zcpayService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <!-- 配置文件zcpay_url=http://localhost:8094/zcpay --> <value>${zcpay_url}/zcpay/hessian/pgsm</value> </property> <property name="serviceInterface"> <value>com.whty.pgsm.api.service.PgsmService</value> </property> </bean> </beans>以上配置完成后,即可在客户端使用@Autowired进行注入后使用
@Autowired private ZcpayService zcpayService;