Hessian
- hessian使用C/S方式,基于HTTP协议传输,使用Hessian二进制序列化。
- server端:
- 1.添加hessian的maven依赖
<dependencies>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
public interface UserService {
public String sayHello(String name);
}
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return name+"调用了hessian服务端的服务被成功调用了";
}
}
- 4.web.xml中配置HessianServlet
<servlet>
<servlet-name>HessianServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>com.stan.service.impl.UserServiceImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HessianServlet</servlet-name>
<url-pattern>/hessianServlet</url-pattern>
</servlet-mapping>
<build>
<plugins>
<!-- tomcat插件控制 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!--端口控制-->
<port>8080</port>
<!--项目路径控制意味着http://localhost:8080/-->
<path>/</path>
<!--编码-->
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
- 2.创建跟server端相同的接口UserService(同上)
- 3.创建测试类测试
public class ClientTest {
public static void main(String[] args) throws MalformedURLException {
String url="http://localhost:8080/hessianServlet";
HessianProxyFactory hessianProxyFactory = new HessianProxyFactory();
UserService userService = (UserService) hessianProxyFactory.create(UserService.class, url);
String sayHello = userService.sayHello("hessian客户端");
System.out.println(sayHello);
}
}