Hessian介绍:

  Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

  Hessian分为服务端和客户端两部分。

一、服务端

1、添加pom.xml依赖

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

2、添加interface 接口

package com.sxdx.oa_service.hessian.service;

public interface HelloWorldService {

    String sayHello(String name);
}

3、实现HelloWorldService 这个接口

package com.sxdx.oa_service.hessian.service.impl;

import com.sxdx.oa_service.hessian.service.HelloWorldService;
import org.springframework.stereotype.Service; @Service
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String sayHello(String name) {
return "Hello World! " + name;
}
}

4、发布服务

package com.sxdx.oa_service.hessian.controller;

import com.sxdx.oa_service.hessian.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Controller; @Controller
public class HessianController { @Autowired
private HelloWorldService helloWorldService; //发布服务 (此处定义外部访问路径,比如本例中,对外提供的API 为 http://127.0.0.1:8080/oaframe/HelloWorldService)
@Bean(name = "/HelloWorldService")
public HessianServiceExporter accountService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(helloWorldService);
exporter.setServiceInterface(HelloWorldService.class);
return exporter;
}
}

二、客户端

1、添加pom.xml依赖

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

2、添加interface 接口(需要和服务端接口类的类名、方法名完全一样,对包路径是否相同没有要求)

package cn.org.csip.vim.sso.service;

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

3、添加Hessian客户端组件(我本来把这部分代码写在了一个Controller中,但是启动会报错,放在启动类中就可以,暂时没确定原因,感觉像是spring注册bean顺序的原因)

package cn.org.csip.vim.sso;

import cn.org.csip.vim.sso.service.HelloWorldService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean; @SpringBootApplication
public class HessiantestApplication {
@Bean
public HessianProxyFactoryBean helloClient() {
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
factory.setServiceUrl("http://127.0.0.1:8080/oaframe/HelloWorldService");
factory.setServiceInterface(HelloWorldService.class);
return factory;
}
public static void main(String[] args) {
SpringApplication.run(HessiantestApplication.class, args);
}
}

4、添加访问测试代码

package cn.org.csip.vim.sso.controller;

import cn.org.csip.vim.sso.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HessianController {
@Autowired
private HelloWorldService helloWorldService; @RequestMapping("/test")
public String test() {
return helloWorldService.sayHello("Spring boot with Hessian.");
} }

5、浏览器访问测试(返回成功)

参考文章:https://blog.csdn.net/sias1991/article/details/75270547

最新文章

  1. SOAPUI使用教程-入门REST测试
  2. 移动端 h5调试技巧
  3. sd卡脱机烧写系统的方法(测试成功)
  4. Python2.7.12开发环境构建(自动补全)
  5. 在iis中设置文件下载而不是在浏览器上打开
  6. SQL2008 SQL2012 远程连接配置方法
  7. iOS 深浅拷贝
  8. Linux:永久修改网卡的MAC地址
  9. C# 基础(5)--字符串
  10. T-SQL笔记
  11. ”sql Server2008 应用程序无法启动,因为应用程序的并行配置不正确。 找不到从属程序集。“C:\windows\SysWOW64\DTSPipelinePerf100.dll”的激活上下文生成失败“的解决方案
  12. web工作方式,浏览网页,打开浏览器,输入网址按下回车键,然后会显示出内容,这个过程是怎样的呢?
  13. 获取WebView里的网页文本内容
  14. 跟我一起写Makefile-陈皓
  15. MVC3+EF4.1学习系列(十一)----EF4.1常见的问题解决
  16. php基础的第一天 任务点滴,event对象方法概括 ing....
  17. eclipse hibernate plugin
  18. Java的错误类型
  19. 遇到npm报错read ECONNRESET怎么办
  20. WinMerge 过滤器用法

热门文章

  1. Linux命令:unlias
  2. [Redis]Redis的五种数据类型与键值/服务器相关命令
  3. Sql Server数据库之四个增删改查
  4. 【原创】python嗅探QQ消息实战
  5. 在网站中使用UEditor富文本编辑器
  6. linux grep (linux查找关键字在php出现的次数)
  7. ucore-lab1-练习5report
  8. 528. Random Pick with Weight index的随机发生器
  9. [leetcode]52. N-Queens II N皇后
  10. findViewById(R.id.btn_first) 给写成 R.layout.