使用Hessian创建接口

使用Hessian创建接口

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
详细介绍

1. Hessian集成Spring配置

加入Hessian依赖

<hessian.version>4.0.38</hessian.version>  
...
<!-- hessian -->
<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>${hessian.version}</version>
</dependency>  

Hessian依赖已SpringMVC,首先需要配置SpringMVC环境,SpringMVC配置略!
首先创建hessian.xml,引入Spring Bean schema

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</beans> 

在SpringMVC配置文件中加入

<import resource="classpath:hessian-api.xml"/>  

2. 创建接口及实现

接口

package com.tiamaes.gjds.app.hessian.api;

import java.util.List;

import com.tiamaes.gjds.app.bean.TrafficReport;

/**  
 * <p>类描述: 路况信息API ,Hessian接口</p>
 * <p>创建人:王成委  </p>
 * <p>创建时间:2015年1月16日 下午3:05:04  </p>
 * <p>版权说明: © 2015 Tiamaes </p>
 */
public interface TrafficReportApi {

    /**
     * 获取全部路况信息
     * @author 王成委
     * @return
     */
    public List<TrafficReport> getAll();

    /**
     * <p>方法描述:畅行指数</p>
     * <p>创建人: 王成委  </p>
     * <p>创建时间: 2015年1月10日 上午10:50:06 </p>
     * @return
     */
    public Double getCurrentTrafficIndex();

}

实现类

package com.tiamaes.gjds.app.hessian.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.tiamaes.gjds.app.bean.TrafficReport;
import com.tiamaes.gjds.app.service.TrafficReportService;

/**  
 * <p>类描述: 路况查询 </p>
 * <p>创建人:王成委  </p>
 * <p>创建时间:2015年1月16日 下午3:15:29  </p>
 * <p>版权说明: © 2015 Tiamaes </p>
 */
public class TrafficReportApiImpl implements TrafficReportApi{

    @Autowired
    private TrafficReportService trafficReportService;

    @Override
    public List<TrafficReport> getAll() {
        try {
            List<TrafficReport> list = this.trafficReportService.getAllCongestion();
            //System.out.println(JackSonUtils.bean2Json(list));
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Double getCurrentTrafficIndex() {
        return this.trafficReportService.getCurrentTrafficIndex();
    }

}

3、Spring配置

   <bean name="busWatingApi" class="com.tiamaes.gjds.app.hessian.api.BusWaitingApiImpl" /> 
   <bean name="/hessian/busWatingApi" class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <!-- service引用具体的实现实体Bean-->  
        <property name="service" ref="busWatingApi" />
        <property name="serviceInterface" value="com.tiamaes.gjds.app.hessian.api.BusWaitingApi" />
    </bean>   

4、测试

    /**
     * Test method for {@link com.tiamaes.gjds.app.hessian.api.BusLineApiImpl#getLineInfo(String,String)}.
     * @throws ClassNotFoundException 
     * @throws MalformedURLException 
     * @throws AppInterfaceException 
     */
    @Test
    public void testQueryBusStationInfo() throws MalformedURLException, ClassNotFoundException {
        String url = "http://127.0.0.1:808/AppInterface/hessian/busLineApi";
        HessianProxyFactory factory = new HessianProxyFactory();
        BusLineApi  api = (BusLineApi) factory.create(BusLineApi.class,url);
        List<GjdsBusStationInfo> reports = api.queryBusStationInfo("B2", "1");
        assertTrue(reports.size() > 0);
    }  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大扑棱蛾子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值