编写发布hessian接口

目录

不使用Spring

所需jar包:

hessian-4.0.7.jar

步骤

  1. 服务端编写接口和实现类

  2. 服务端编写web.xml

  3. 将接口所在包打包导出并发布服务

  4. 客户端导入接口对应jar包

  5. 客户端编写测试类

服务端编写接口和实现类

接口:
public interface HessianTest {
    public String sayHello(String name);
}

实现类:
import com.xxx.hessiantest.HessianTest;

public class HessianTestImpl implements HessianTest {

    public String sayHello(String name) {
        return "hello " + name;
    }
}

服务端编写web.xml

HessianServlet以及对应的初始化参数home-class,home-api写法固定。其中,home-class对应实现类全包名,home-api对应接口全报名。

  <servlet>
    <servlet-name>hessiantest</servlet-name>
    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
    <init-param>
        <param-name>home-class</param-name>
        <param-value>com.xxx.hessiantestimpl.HessianTestImpl</param-value>
    </init-param>
    <init-param>
        <param-name>home-api</param-name>
        <param-value>com.xxx.hessiantest.HessianTest</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>hessiantest</servlet-name>
    <url-pattern>/hessian</url-pattern>
  </servlet-mapping>

打成jar包,发布服务

只需要将接口所在的包打成jar包即可。

客户端导入jar包客户端编写测试类

import com.caucho.hessian.client.HessianProxyFactory;
import com.xxx.hessiantest.HessianTest;

public class TestHessianInterface {

    public static void main(String[] args) throws Exception {

        String url = "http://localhost:8080/Hessian_Server/hessian";
        HessianProxyFactory factory = new HessianProxyFactory();
        HessianTest hessianTest = (HessianTest) factory.create(HessianTest.class, url);
        String hello = hessianTest.sayHello("阿紫");
        System.out.println(hello);
    }
}

打印输出结果

hello 阿紫

使用Spring

所需要jar包

aopalliance-1.0.jar
commons-logging-1.1.1.jar
hessian-4.0.7.jar
junit-4.10.jar
spring-aop-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-orm-3.2.5.RELEASE.jar
spring-test-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
spring-webmvc-3.2.5.RELEASE.jar

步骤

  1. 服务端编写接口和实现类

  2. 服务端编写web.xml

  3. 服务端编写spring的配置文件hessian-server.xml

  4. 服务端将接口所在的包打包发布服务

服务端编写接口和实现类

服务端接口

public interface HessianTest {
    public String sayHello(String name);
}

服务端实现类

import com.xxx.hessiantest.HessianTest;

public class HessianTestImpl implements HessianTest {

    public String sayHello(String name) {
        return "hello " + name;
    }
}

服务端编写web.xml

  <servlet>
    <servlet-name>hessian</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:hessian-server.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>/hessian/*</url-pattern>
  </servlet-mapping>

服务端编写hessian-server.xml文件

其中bean的name与web.xml中的URL拼接成hessian接口的路径地址。
property中name属性固定

<?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:aop="http://www.springframework.org/schema/aop"  
  xmlns:context="http://www.springframework.org/schema/context"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  http://www.springframework.org/schema/aop   
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <bean id="hessianTestImpl" class="com.xxx.hessiantestimpl.HessianTestImpl"></bean>
  <bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="hessianTestImpl"></property>
    <property name="serviceInterface" value="com.xxx.hessiantest.HessianTest"></property>
  </bean>
</beans>

打成jar包并发布服务

客户端编写测试代码

不带spring
import com.caucho.hessian.client.HessianProxyFactory;
import com.xxx.hessiantest.HessianTest;

public class TestHessianInterface {

    public static void main(String[] args) throws Exception {

        String url = "http://localhost:8080/Hessian_Server/hessian/hello";
        HessianProxyFactory factory = new HessianProxyFactory();
        HessianTest hessianTest = (HessianTest) factory.create(HessianTest.class, url);
        String hello = hessianTest.sayHello("阿紫");
        System.out.println(hello);
    }
}
带Spring
客户端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" xmlns:aop="http://www.springframework.org/schema/aop"  
  xmlns:context="http://www.springframework.org/schema/context"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  http://www.springframework.org/schema/aop   
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="testHessian" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl" value="http://localhost:8080/Hessian_Server/hessian/hello"></property>
        <property name="serviceInterface" value="com.xxx.hessiantest.HessianTest"></property>
    </bean> 
</beans>


测试类:
    @Test
    public void testHessian(){

        ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
        HessianTest bean = (HessianTest) context.getBean("testHessian");
        String sayHello = bean.sayHello("阿紫");
        System.out.println(sayHello);
    }

输出结果

hello 阿紫
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值