spring mvc 结合 Hessian 配置
1、先在web.xml中配置
<!-- Hessian配置 -->
<servlet>
<servlet-name>Hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern> <!-- 匹配的请求地址 -->
</servlet-mapping>
2、 在原来的基础上 新增 -->,/WEB-INF/Hessian-servlet.xml
<!-- spring mvc dispatcher 初始化分发器指定文件到 /WEB-INF/dispatcher-servlet.xml-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml,/WEB-INF/Hessian-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3、 在WEB_INF下面新增配置文件Hessian-servlet.xml 加载这个配置
<!-- 业务类 -->
<bean id="hessianService" class="com.uenpay.hessian.service.BasicService"/> <!-- 自定义接口实现类 -->
<!-- http://localhost/ncpay/hessian/hessianService 测试调用. 地址:端口/项目名称/hessian(servlet配置的)/接口名称 -->
<!-- 远程服务 -->
<bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="hessianService"/>
<property name="serviceInterface">
<value>
com.uenpay.hessian.service.Basic <!-- 自定义接口名称 -->
</value>
</property>
</bean>
4、涉及到的jar
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
5、部分代码片段
//Basic接口类
package com.uenpay.hessian.service;
/**
* @author wangjiesheng
* Specification : 文档说明:
*/
public interface Basic {
String hello(String name);
}
//BasicService接口实现类
package com.uenpay.hessian.service;
import com.caucho.hessian.server.HessianServlet;
/**
* @author wangjiesheng
* Specification : 文档说明:
*/
public class BasicService extends HessianServlet implements Basic {
private static final long serialVersionUID = 1L;
@Override
public String hello(String name) {
return "Hello " + name;
}
}
//main方法测试Hessian。 OK
public static void main(String[] args) throws Exception{
String url = "http://localhost/ncpay/hessian/hessianService";
HessianProxyFactory factory = new HessianProxyFactory();
Basic hessianServer =
(Basic)factory.create(Basic.class, url);
String str = hessianServer.say("王结胜");
System.out.println(str);
}