`

Spring 的 Hessian 简单使用

阅读更多

一、 服务器端配置

<? xml version = "1.0" encoding = "UTF-8" ?>

< web-app version = "2.4"

    xmlns = "http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    < servlet >

       < servlet-name > remote </ servlet-name >         < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >

       < load-on-startup > 1 </ load-on-startup >

    </ servlet >

    < servlet-mapping >

        < servlet-name > remote </ servlet-name >

        < url-pattern > /remote/* </ url-pattern >

    </ servlet-mapping >

 

  < welcome-file-list >

    < welcome-file > index.jsp </ welcome-file >

  </ welcome-file-list >

</ web-app >

 

配置好 Servlet  servlet mapping DispatcherServlet

然后再在 WEB-INF 创建 Spring 初始化 HessianService 配置,如下:

该文件名一定是: 那个 servlet + -servlet.xml 这里我们创建 ”remote-servlet.xml” 文件,内容如下:

 

<? xml version = "1.0" encoding = "GBK" ?>

< beans xmlns = "http://www.springframework.org/schema/beans"

       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

       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.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >

 

    < bean id = "timeService" class = "com.sosee.TimeServiceImpl" />

   

    < bean name = "/HessianService" class = "org.springframework.remoting.caucho.HessianServiceExporter" >

      < property name = "service" ref = "timeService" />

      < property name = "serviceInterface" value = "com.sosee.TimeService" />

    </ bean >   

</ beans >

2. 创建接口和实现类

接口

com.sosee.TimeService

package com.sosee;

public interface TimeService{

    String getTime();

}

实现类:

package com.sosee;

import java.text.SimpleDateFormat;

import java.util.Date;

public class TimeServiceImpl implements TimeService {

    public String getTime() {

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

    }

}

 

       OK, 以上将服务器端的所有准备工作都搞定了,接下来就是客户端如何调用了。

二、 客户端 Spring 配置以及代码调用

1.        配置 Spring 容器

<? xml version = "1.0" encoding = "GBK" ?>

< beans xmlns = "http://www.springframework.org/schema/beans"

       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

       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.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >

      

    < bean name = "hessianService" class = "org.springframework.remoting.caucho.HessianProxyFactoryBean" >

      < property name = "serviceUrl" value = "http://localhost:8080/call/remote/HessianService" />

      < property name = "serviceInterface" value = "com.sosee.TimeService" />

</ bean ></ beans >

 

2.        调用代码:

 

package com.sosee;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args){

       ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );

       TimeService service = (TimeService)context.getBean( "hessianService" );

       System. out .println( "get systime:" +service.getTime());   }

}

 

OK ,测试成功 .

有可能你初次做这个测试的时候,有出现以下的异常情况:

Exception in thread "main" org.springframework.remoting.RemoteAccessException : Cannot access Hessian remote service at [http://localhost:8080/call2/remote/HessianService]; nested exception is com.caucho.hessian.io.HessianProtocolException : 400: java.io.IOException : Server returned HTTP response code: 400 for URL: http://localhost:8080/call2/remote/HessianService

    at org.springframework.remoting.caucho.HessianClientInterceptor.convertHessianAccessException( HessianClientInterceptor.java:254 )

    at org.springframework.remoting.caucho.HessianClientInterceptor.invoke( HessianClientInterceptor.java:225 )

    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ReflectiveMethodInvocation.java:171 )

    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( JdkDynamicAopProxy.java:204 )

    at $Proxy1.getTime(Unknown Source)

    at com.sosee.Main.main( Main.java:14 )

Caused by: com.caucho.hessian.io.HessianProtocolException : 400: java.io.IOException : Server returned HTTP response code: 400 for URL: http://localhost:8080/call2/remote/HessianService

    at com.caucho.hessian.client.HessianProxy.invoke( HessianProxy.java:185 )

 

 

这种异常一般都是由于 Spring 配置文件中

      < property name = "serviceUrl" value = "http://localhost:8080/call/remote/HessianService" />

( 我这里配置的 call  webroot 的名称 , 你如果直接放到服务器的根目录,就不需要这个了。 )

出现的问题,一定把 URL 写正确就 OK 了!

另外需要注意的就是 spring.jar 的版本问题了, Srping 版本更新较快,我这个用的 2.0.7 版本。

分享到:
评论

相关推荐

    spring 集成 hessian例子

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。参考文档地址...

    一个简单的Hessian示例

    一个简单的Hessian,简单介绍了Hessian的使用方式,介绍了Hessian和Spring集成的使用方式,以及单独使用Hessian的方式。

    SpringMVC整合Hessian简单案例

    SpringMVC整合Hessian的简单案例,使用Maven管理,idea工具开发,案例中配置了两个接口的服务发布

    基于spring+hessian框架的webservice实例

    webservice技术,基于spring+hessian框架开发的简单实例

    Hessian的使用配置步骤

    远程方法调用的比较,Hessian方法的介绍和相关配置.Hessian是一个轻量级的remoting on http工具,采用的是Binary RPC协议,所以它很适合于发送二进制数据,同时...这个文件可以是hessian在spring项目和web项目的简单配置

    spring、hessian通过tomcat的简单环境应用源代码

    spring、hessian通过tomcat的简单环境应用源代码 里面有具体的实例程序

    使用hessian简单使用【续】- 与spring结合使用

    NULL 博文链接:https://tom-seed.iteye.com/blog/1566681

    spring mvc hessian maven简单实例

    NULL 博文链接:https://asia007.iteye.com/blog/2020523

    hessian学习实例

    hessian学习实例,hessian框架例子,与Spring集成。包括了server端和client端

    Hessian样例源代码

    1、开发环境:eclipse3.1 + jdk1.5; 2、样例说明: 1)spring + hessian; 2)hessian。 3、样例内容: 1)HashMap传递; 2)简单对象传递; 3)异常处理

    基于java的Hessian实现

    基于java实现hessian进行服务器之间数据交互demo项目 实现功能: 1.基于spring 2.5.6+hessian3.1.6带有签名安全机制 2.基于servlet代理机制实现HessianServlet,进行简单IP地址校验功能!

    spring jar 包详解

    Spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟 HttpServletRequest和HttpServletResponse类在Web应用单元测试是很方便的。  如何选择这些发布包,决定选用哪些发布包其实相当简单。如果你正在构建...

    hessian-demo和hessian与spring整合demo.doc

    Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI(Remote Method Invocation,远程方法调用)的功能。采用的是二进制RPC(Remote Procedure Call Protocol,远程过程调用协议)协议,因为采用...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    Spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟HttpServletRequest和HttpServletResponse类在Web应用单元测试是很方便的。  如何选择这些发布包,决定选用哪些发布包其实相当简单。如果你正在构建...

    Spring 2.0 开发参考手册

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    最新最全的spring开发包

    spring jar包详细介绍 spring.jar是包含有完整发布的单个jar包,spring....Spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟HttpServletRequest和HttpServletResponse类在Web应用单元测试是很方便的。

Global site tag (gtag.js) - Google Analytics