一:服务端
web.xml
<!-- DispatcherServlet -->
<servlet>
<servlet-name>Spring-DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext-hessianServer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Spring-DispatcherServlet</servlet-name>
<url-pattern>*.hessian</url-pattern>
</servlet-mapping>
package com.xx.service;
/**
* 服务接口
*/
public interface ISayHelloService {
/**
* @param name
* @return
*/
String doSayHello(String name);
/**
* @param name
* @param welcomeStr
* @return
*/
String doSayHello(String name, String welcomeStr);
}
package com.xx.service.impl;
import com.xx.service.ISayHelloService;
/**
* 服务接口实现
*/
public class DefaultSayHelloServiceImpl implements ISayHelloService {
public String doSayHello(String name) {
return doSayHello(name, "hello");
}
public String doSayHello(String name, String welcomeStr) {
return name + "," + welcomeStr;
}
}
spring配置文件applicationContext-hessianServer.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-lazy-init="false">
<!-- 服务端 实现-->
<bean id="sayHelloService" class="com.xx.service.impl.DefaultSayHelloServiceImpl" />
<bean id="sayHelloService_hessianRPC" class="org.springframework.remoting.caucho.HessianServiceExporter" >
<property name="service" ref="sayHelloService" />
<!-- 服务接口 -->
<property name="serviceInterface" value="com.xx.service.ISayHelloService" />
</bean>
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/sayHelloService.hessian" value-ref="sayHelloService_hessianRPC" />
</map>
</property>
</bean>
</beans>
二:客户端 调用服务
package com.xx.service;
/**
* 服务接口
*/
public interface ISayHelloService {
/**
* @param name
* @return
*/
String doSayHello(String name);
/**
* @param name
* @param welcomeStr
* @return
*/
String doSayHello(String name, String welcomeStr);
}
<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 客户端 -->
<bean id="sayHelloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!-- 重载方法支持 不然会抛出异常 :Caused by: com.caucho.hessian.io.HessianProtocolException: is an unknown code -->
<property name="overloadEnabled" value="true" />
<property name="serviceUrl" value="http://localhost:8080/spring_hessian/sayHelloService.hessian" />
<!-- 服务接口 -->
<property name="serviceInterface" value="com.xx.service.ISayHelloService"/>
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.service.ISayHelloService;
public class ClientMain {
public static void main(String[] args) {
ISayHelloService sayHelloService = getBean(ISayHelloService.class, "sayHelloService");
System.out.println(sayHelloService.doSayHello("王五"));
//客户端必须启用重载方法支持 不然会抛出Caused by: com.caucho.hessian.io.HessianProtocolException: '�' is an unknown code异常
System.out.println(sayHelloService.doSayHello("李四", "welcome"));
}
public static <T> T getBean(Class<T> clazs, String beanName) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-client.xml");
return clazs.cast(applicationContext.getBean(beanName));
}
}
依赖的jar文件: