hessian远程调用及spring中使用hessian

hessian远程调用及spring中使用hessian

官方文档:
使用方法:http://hessian.caucho.com/#TheServiceAPI
序列化协议:http://hessian.caucho.com/doc/hessian-serialization.html

hessian远程调用

接口API:

public interface BasicAPI {
  public String hello();
}

接口API实现:

public class BasicService extends HessianServlet implements BasicAPI {
  private String _greeting = "Hello, world";

  public void setGreeting(String greeting)
  {
    _greeting = greeting;
  }

  public String hello()
  {
    return _greeting;
  }
}

调用方式:

String url = "http://hessian.caucho.com/test/test";

HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);

System.out.println("hello(): " + basic.hello());

另外还需要有servlet定义,这个需要自己查看HessianServlet,官方没给出,这里不说明,因为重点是spring的。

spring中使用hessian

除了上面的接口API及其实现类定义外,需要使用spring来定义。
客户端配置代理类

<bean id="userLocaleService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://127.0.0.1:8081/support/serviceUrl" />
    <property name="serviceInterface" value="service.UserLocaleService" />
    <property name="allowNonSerializable" value="true" />
    <property name="hessian2" value="true" />
</bean>

服务端配置:
单独定义一个文件叫,spring-remote-servlet.xml文件

<?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">
    <!--userLocaleServiceImp是接口service.UserLocaleService的实现,通过注解@Service定义的-->
    <bean name="/userLocaleService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="userLocaleServiceImp"/>
        <property name="serviceInterface" value="service.UserLocaleService"/>
        <property name="allowNonSerializable" value="true"/>
    </bean>
</beans>

web.xml配置

<servlet>
    <servlet-name>spring-remote</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-remote-servlet.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>spring-remote</servlet-name>
    <url-pattern>/support/*</url-pattern>
</servlet-mapping>

这样即可,spring只处理 /support/* 的url,访问时只需要使用 /support/userLocaleService 即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值