服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring boot整合hessian的示例

spring boot整合hessian的示例

2020-12-03 09:48sias1991 Java教程

本文通过实例代码给大家介绍了spring boot整合hessian的方法,需要的朋友可以参考下

首先添加hessian依赖

?
1
2
3
4
5
<dependency> 
   <groupId>com.caucho</groupId> 
    <artifactId>hessian</artifactId> 
    <version>4.0.38</version>
</dependency>

服务端:HessianServer,端口号:8090

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public interface HelloWorldService {
  String sayHello(String name);
}
@Service("HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
  @Override
  public String sayHello(String name) {
    return "Hello World! " + name;
  }
}
@SpringBootApplication
public class HessianServerApplication {
  @Autowired
  private HelloWorldService helloWorldService;
  public static void main(String[] args) {
    SpringApplication.run(HessianServerApplication.class, args);
  }
//发布服务
  @Bean(name = "/HelloWorldService")
  public HessianServiceExporter accountService() {
    HessianServiceExporter exporter = new HessianServiceExporter();
    exporter.setService(helloWorldService);
    exporter.setServiceInterface(HelloWorldService.class);
    return exporter;
  }
}

客户端代码:HessianClient,同服务端一样引入hessian依赖,端口号:8092

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public interface HelloWorldService {
  String sayHello(String name);
}
@SpringBootApplication
public class HessianClientApplication {
  @Bean
  public HessianProxyFactoryBean helloClient() {
    HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
    factory.setServiceUrl("http://localhost:8090/HelloWorldService");
    factory.setServiceInterface(HelloWorldService.class);
    return factory;
  }
  public static void main(String[] args) {
    SpringApplication.run(HessianClientApplication.class, args);
  }
}
@RestController
public class TestController {
  @Autowired
  private HelloWorldService helloWorldService;
  @RequestMapping("/test")
  public String test() {
    return helloWorldService.sayHello("Spring boot with Hessian.");
  }
}

访问地址即可:http://localhost:8092/test

PS:springboot hessian

注意把hessian的依赖换成4.0.38或者把git文件里的4.0.37放到maven私服中去,推荐使用4.0.37版本。38版本存在序列化bigdecimal的问题。

?
1
2
3
4
5
<dependency>
     <groupId>com.caucho</groupId>
     <artifactId>hessian</artifactId>
     <version>4.0.37</version>
  </dependency>

git:

https://git.oschina.net/wong_loong/rpc.git

以上所述是小编给大家介绍的spring boot整合hessian的示例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/sias1991/article/details/75270547

延伸 · 阅读

精彩推荐