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 mappingDispatcherServlet

然后再在 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 版本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值