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