服务器端配置和代码
pom 配置
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
web.xml 配置
<!-- Hessian通过Servlet提供远程服务,需要将某个匹配的模式映射到hessian服务中, -->
<!-- spring的dispatcherServlet能完成此功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务, -->
<!-- web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获, 转发给context的bean处理。 -->
<!-- 而HessianServiceExporter提供bean服务。 -->
<servlet>
<servlet-name>hessian-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian-servlet</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-base.xml</param-value>
</context-param>
编写接口
public interface UserInfo {
String GetUser(int userid);
}
实现类
@Component("userInfoImplRome")
public class UserInfoImpl implements UserInfo {
public String GetUser(int userid) {
// TODO Auto-generated method stub
return userid+"ss";
}
}
hessian接口注册
hessian-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 用户服务 -->
<bean name="/userClientServiceRemote" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="userInfoImplRome"/>
<property name="serviceInterface" value="com.hisan.domel.UserInfo"/>
</bean>
</beans>
服务器端的配置和简单的代码完成
客户端配置和代码
spring 配置
<!-- 用户接口 -->
<bean id="userClientService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://127.0.0.1:8010/com.hesan/remoting/userClientServiceRemote" />
<property name="serviceInterface" value="com.hesan.domels.UserInfo" />
<property name="chunkedPost" value="false" />
<property name="overloadEnabled" value="true" />
</bean>
Controller 调用
@Controller
public class OrderInfo {
@Resource(name="userClientService")
private UserInfo userClientService;
@RequestMapping("getorder")
@ResponseBody
public String GetOrder(){
System.out.println(userClientService.GetUser(7788));
return "98568564546546";
}
}