美文网首页
编写发布Hessian接口

编写发布Hessian接口

作者: George_Antonio | 来源:发表于2017-12-26 19:53 被阅读0次

目录

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

  <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 阿紫

相关文章

  • 编写发布Hessian接口

    目录 [TOC] 不使用Spring 所需jar包: hessian-4.0.7.jar 步骤 服务端编写接口和实...

  • 编程问题

    1.hessian方法重载导致报错 客户端B 调用 服务端A 的hessian接口,hessian正常启动,浏览器...

  • java序列化之Hessian

    一、Hessian序列化的前提 Hessian要实现序列化,前提是被序列化的类得实现Serializable接口。...

  • 2019-01-12

    C#接口开发之WebService接口的编写、发布、访问 开发工具:VS2013 一、接口的开发 首先新建一个We...

  • Hessian, Jacobian and Residual

    Hessian, Jacobian and Residual Hessian, 雅克比和残差 1. Hessian...

  • 【编码日常】微服务接口兼容性升级之序列化

    dubbo rpc 接口兼容升级 hessian2 序列化 任何使用微服务架构的团队,作为接口提供方若对扩展性没有...

  • 接口编写

    这里写接口用的Python3,用的flask+pymysql.cursors 导入mysql文件 appstore...

  • 接口编写

    最简单的编写 新建 DemoRestController pom.xml 添加依赖 点击右上角运行 验证在浏览器或...

  • 一键生成 API 文档的妙招

    一般接口文档编写完成后,就要开始编写调用接口的代码,很多开发团队都采用传统的通过接口文档的方式来编写接口。但在接口...

  • 测试平台系列(20) 编写项目的增删改查接口和页面(2)

    回顾 上回说到,编写项目权限相关接口,但是我们只完成了核心方法的编写,这次我们就来定义相关的接口。 编写接口 先实...

网友评论

      本文标题:编写发布Hessian接口

      本文链接:https://www.haomeiwen.com/subject/cunsgxtx.html