hessian还是挺简单的,简单记录一下。注意hessian要使用官方下载的最新版本,不要用mvn仓库的,那是6年前的库了,呵呵。
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.duitang</groupId>
<artifactId>t3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<org.springframework.version>3.1.2.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>com.duitang.external</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
</project>
接口定义和实现:
public interface BasicAPI {
public String hello();
}
package test;
public class BasicService implements BasicAPI{
public String hello() {
return "Hello, world";
}
}
webx.xml配置servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>duitang.com</display-name>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-api</param-name>
<param-value>test.BasicAPI</param-value>
</init-param>
<init-param>
<param-name>home-class</param-name>
<param-value>test.BasicService</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
客户端调用
package test;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
public class Test{
public static void main(String[] args) throws MalformedURLException{
String url="http://localhost:8080/Hello";
HessianProxyFactory proxyFactory=new HessianProxyFactory();
BasicAPI basicAPI = (BasicAPI) proxyFactory.create(BasicAPI.class, url);
System.out.println(basicAPI.hello());
}
}
python客户端更简单
if __name__ == "__main__":
proxy = Hessian("http://localhost:8080/Hello")
try:
print proxy.hello()
except Error, v:
print "ERROR", v
Hello, world
hessian协议:
http://hessian.caucho.com/doc/hessian-serialization.html