编写发布Hessian接口

目录

[TOC]

不使用Spring

所需jar包:

hessian-4.0.7.jar

步骤

  1. 服务端编写接口和实现类
  1. 服务端编写web.xml
  1. 将接口所在包打包导出并发布服务

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

  1. 客户端编写测试类

服务端编写接口和实现类

接口:
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对应接口全报名。

  
    hessiantest
    com.caucho.hessian.server.HessianServlet
    
        home-class
        com.xxx.hessiantestimpl.HessianTestImpl
    
    
        home-api
        com.xxx.hessiantest.HessianTest
    
  
  
  
    hessiantest
    /hessian
  

打成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

  
    hessian
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:hessian-server.xml
    
    2
  
  
    hessian
    /hessian/*
  

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

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



  
  
  
    
    
  

打成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的配置文件:


  
    
        
        
     



测试类:
    @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 阿紫

你可能感兴趣的:(编写发布Hessian接口)