java hessian客户端,使用hessian开发远程服务

7月 26, 2015 |

Nix.Huang

开发非spring版本

1)定义一个简单的接口

public interface BasicAPI {

public String hello();

}

1

2

3

publicinterfaceBasicAPI{

publicStringhello();

}

2)定义一个该接口的实现类

public class BasicService implements BasicAPI {

private String _greeting = "Hello, javacoder.cn";

public void setGreeting(String greeting) {

_greeting = greeting;

}

public String hello() {

return _greeting;

}

}

1

2

3

4

5

6

7

8

9

10

publicclassBasicServiceimplementsBasicAPI{

privateString_greeting="Hello, javacoder.cn";

publicvoidsetGreeting(Stringgreeting){

_greeting=greeting;

}

publicStringhello(){

return_greeting;

}

}

3)在web.xml中声明HessianServlet

该servlet的作用是对url"/hello"的请求由"service-class"表示的类处理

hello

com.caucho.hessian.server.HessianServlet

service-class

cn.javacoder.test.hessian.BasicService

hello

/hello

1

2

3

4

5

6

7

8

9

10

11

12

13

hello

com.caucho.hessian.server.HessianServlet

service-class

cn.javacoder.test.hessian.BasicService

hello

/hello

4)开发客户端

String url = "http://localhost:8080/test-hessian/remote/hello";

HessianProxyFactory factory = new HessianProxyFactory();

BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);

System.out.println("hello(): " + basic.hello());

1

2

3

4

Stringurl="http://localhost:8080/test-hessian/remote/hello";

HessianProxyFactoryfactory=newHessianProxyFactory();

BasicAPIbasic=(BasicAPI)factory.create(BasicAPI.class,url);

System.out.println("hello(): "+basic.hello());

开发spring 版本

1)在pom.xml中添加相应的依赖

2)在web.xml中声明DispatcherServlet

remote

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:remote-servlet.xml

1

remote

/remote/*

1

2

3

4

5

6

7

8

9

10

11

12

13

14

remote

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:remote-servlet.xml

1

remote

/remote/*

3)添加spring的applicationContext xml文件,remote-servlet.xml

class="org.springframework.remoting.caucho.HessianServiceExporter">

1

2

3

4

5

6

7

8

9

class="org.springframework.remoting.caucho.HessianServiceExporter">

4)开发客户端

一样的过程,添加依赖到pom.xml,配置客户端的applicationContext.xml 和调用

1

2

3

4

调用的测试代码如下

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

BasicAPI basic = applicationContext.getBean(BasicAPI.class);

System.out.println("hello(): " + basic.hello());

applicationContext.close();

1

2

3

4

5

ClassPathXmlApplicationContextapplicationContext=newClassPathXmlApplicationContext("classpath:applicationContext.xml");

BasicAPIbasic=applicationContext.getBean(BasicAPI.class);

System.out.println("hello(): "+basic.hello());

applicationContext.close();

Posted in: WEB开发 | Tags: hessian

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值