最近有个项目中需要访问第三方的 WebService 服务,因为图方便,简单就打算用 Hessian 来 交互。
这里有以下步骤:
1. 在 WebService 端要提供给客户端访问的 公共服务接口(例如: CommentHessianService),相应的客户端 也要这个(CommentHessianService)公共服务接口。 一般的都把 服务端的CommentHessianService直接 copy丢到 客户端。 有了这个 CommentHessianService 公共服务接口,下面应该要实现具体实现服务接口里面的方法。(CommentHessianServiceImpl)。
2.公共服务接口定义好后,在服务端做好相应的配置。例如有一个对应的 Service的配置文件:
context-comment-service.xml
hessian对应的 service的配置文件: remoting-servlet.xml
web.xml的配置:
3. 都配置好后,运行工程,测试Hessain是否正常运行: http://localhost/test/hessian/hessianCommentService 如果页面提示 405 则成功了。
4. client的Hessian配置:(上面提到有公共服务接口 CommentHessianService,直接copy到 客户端的工程目录下) 我这边对应的配置文件内容是: context-member-service.xml
客户端的配置比较简单,引用对服务端的配置。
这里有以下步骤:
1. 在 WebService 端要提供给客户端访问的 公共服务接口(例如: CommentHessianService),相应的客户端 也要这个(CommentHessianService)公共服务接口。 一般的都把 服务端的CommentHessianService直接 copy丢到 客户端。 有了这个 CommentHessianService 公共服务接口,下面应该要实现具体实现服务接口里面的方法。(CommentHessianServiceImpl)。
2.公共服务接口定义好后,在服务端做好相应的配置。例如有一个对应的 Service的配置文件:
context-comment-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<bean id="commentHessianServiceImpl"class="com.service.impl.CommentHessianServiceImpl"/>
</beans>
hessian对应的 service的配置文件: remoting-servlet.xml
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
<bean name="/hessianCommentService"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要导出的目标bean-->
<property name="service" ref="commentHessianServiceImpl" />
<!-- Hessian服务的接口-->
<property name="serviceInterface" value="com.service.CommentHessianService" />
</bean>
</beans>
web.xml的配置:
<servlet>
<display-name>spring_servlet</display-name>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/com/config/spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/httpTest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
3. 都配置好后,运行工程,测试Hessain是否正常运行: http://localhost/test/hessian/hessianCommentService 如果页面提示 405 则成功了。
4. client的Hessian配置:(上面提到有公共服务接口 CommentHessianService,直接copy到 客户端的工程目录下) 我这边对应的配置文件内容是: context-member-service.xml
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- DataSource -->
<bean id="base-memberService"
class="com.member.impl.MemberServiceImpl">
<property name="hessianService">
<ref bean="mHessianClient" />
</property>
<property name="memberDAO">
<ref bean="base-memberDAO" />
</property>
</bean>
<bean id="myHessianClient"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://yourHost/test/hessian/hessianCommentService</value>
</property>
<property name="serviceInterface">
<value>com.member.CommentHessianService</value>
</property>
<!--<property name="overloadEnabled" value="true"></property>
反向代理设置
<property name="chunkedPost" value="false"/>
--></bean>
</beans>
客户端的配置比较简单,引用对服务端的配置。