hessian(一)--基本使用

一、hessian简介

hessian是RPC框架的一种,即可远程调用方法,采用二进制序列化,使用http协议进行传输。

由于hessian是使用http协议传输,通常hessian服务是采用web项目的方式进行开发的。

二、hessian服务端开发步骤

1、添加maven依赖

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.7</version>
</dependency>

2、创建服务,即是接口和相应的实现类

public interface HelloService {
    String sayHello(String name);
}
public class HelloServiceImpl implements HelloService{
    public String sayHello(String name) {
        return "hello,"+name;
    }
}
3、配置web.xml

主要在servlet中配置接口、实现类及访问地址,如:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>helloService</servlet-name>
        <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
        <init-param>
            <param-name>home-class</param-name>
            <param-value>service.impl.HelloServiceImpl</param-value>
        </init-param>
        <init-param>
            <param-name>home-api</param-name>
            <param-value>service.HelloService</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>helloService</servlet-name>
        <url-pattern>/helloService</url-pattern>
    </servlet-mapping>
</web-app>
这样,地址为http://localhost:8080/helloService的hessian服务便配置完成,启动web项目即可。

三、hessian客户端开发步骤

1、添加maven依赖

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.7</version>
</dependency>
2、添加hessian服务接口的jar包(本例就是含有HelloSerivce的包)

3、配置访问地址,并访问服务,如:

public class HessianClient {
    public static void main(String[] args) throws MalformedURLException {
        String url = "http://localhost:8080/helloService";
        HessianProxyFactory factory = new HessianProxyFactory();
        HelloService helloService = (HelloService)factory.create(HelloService.class,url);
        System.out.println(helloService.sayHello("apple"));
    }
}
输出结果:

hello,apple




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值