主要记录下hessian的一个简单例子
服务端
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fei</groupId>
<artifactId>hessian-server-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<!-- jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>
UserInfo.java
package com.fei.info;
import java.io.Serializable;
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.fei.hessian;
import com.fei.info.UserInfo;
public interface ITestService {
String hello(UserInfo user);
String say(String words);
int sum(int a,int b);
}
package com.fei.hessian;
import com.caucho.hessian.server.HessianServlet;
import com.fei.info.UserInfo;
public class TestService extends HessianServlet implements ITestService{
/**
*
*/
private static final long serialVersionUID = 1L;
public String hello(UserInfo user) {
return "hello "+user.getName();
}
public String say(String words) {
return "hi "+words;
}
public int sum(int a, int b) {
return a+b;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.fei.hessian.TestService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test.hessian</url-pattern>
</servlet-mapping>
</web-app>
客户端
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fei.hessian</groupId>
<artifactId>hessian-client-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
</dependencies>
</project>
UserInfo.java
package com.fei.info;
import java.io.Serializable;
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.fei.hessian;
import com.fei.info.UserInfo;
public interface ITestService1 {
String hello(UserInfo user);
String hi(String s);
}
package com.fei.hessian;
public interface ITestService2 {
String say(String words);
int sum(int a,int b);
}
package com.fei.hessian;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.fei.info.UserInfo;
public class MainTest {
public static void main(String[] args) throws MalformedURLException {
String url = "http://localhost:80/test.hessian";
HessianProxyFactory factory = new HessianProxyFactory();
ITestService2 t2 = (ITestService2) factory.create(ITestService2.class, url);
System.out.println(t2.say("王五"));
System.out.println(t2.sum(10, 20));
ITestService1 t1 = (ITestService1) factory.create(ITestService1.class, url);
UserInfo u = new UserInfo();
u.setName("张三");
System.out.println(t1.hello(u));
System.out.println(t1.hi("李四"));
}
}
tomcat启动服务端,然后执行客户端
客户端日志
发现客户端的接口,方法个数无需和服务端一致。。。。只是客户端调用某个方法时,如果服务端没有才会报错。。。。说明如果服务端升级,新增接口,如果某个客户端不需要新方法,那客户端无需修改