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