欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java和c#使用hessian通信的方法

程序员文章站 2023-12-20 09:07:58
本文实例讲述了java和c#使用hessian通信的方法,是非常实用的技巧。分享给大家供大家参考。具体分析如下: 首先,hessian主页为:http://hessian...

本文实例讲述了java和c#使用hessian通信的方法,是非常实用的技巧。分享给大家供大家参考。具体分析如下:

首先,hessian主页为:http://hessian.caucho.com/

下面通过一个简单的例子学习hessian服务:服务端为java,客户端为c#。

先要准备好c#和java的第三方类库,下载地址:http://hessian.caucho.com/

下载 hssiancharp.dll及hessian-4.0.37.jar

hessian服务端(java):

打开eclipse创建一个dynamic web project,将hessian-4.0.37.jar放到lib下,大概如图所示:

java和c#使用hessian通信的方法

创建一个通信接口ihello:

package hessian.test.server;
import java.util.arraylist;
public interface ihello {
  string sayhello(string msg);  
  void sayhello2(int bean);
  void print(string msg); 
  hellobean getdata(hellobean bean);
  arraylist<hellobean> getbeanlist();
  complexdata getcomplexdata();
  
}

ihello接口的一个实现:helloimpl.java

package hessian.test.server;
import java.util.arraylist;
public class helloimpl implements ihello{
  public string sayhello(string msg){
    return "hello " + msg;
  }
  public void sayhello2(int bean){
    system.out.println("hello " + bean);
  }
  public void print(string msg){
    system.out.println(msg);
  }
  public hellobean getdata(hellobean bean){
    hellobean result = new hellobean();
    result.setname("lu xiaoxun a new name");
    result.setage(26);
    system.out.print(bean.getname());
    return result;
  }
  public arraylist<hellobean> getbeanlist(){
    arraylist<hellobean> beans = new arraylist<hellobean>();
    
    hellobean b1 = new hellobean();
    b1.setname("lu1");
    b1.setage(26);
    beans.add(b1);
    
    hellobean b2 = new hellobean();
    b2.setname("lu2");
    b2.setage(27);
    beans.add(b2);
    
    return beans;
  }
  public complexdata getcomplexdata(){
    complexdata data = new complexdata();
    arraylist<hellobean> beans = getbeanlist();
    data.setdata(beans, beans.size());
    return data;
  }
}

定义用来进行数据传输的类,两个类都必须实现serializable接口:

hellobean.java

package hessian.test.server;
import java.io.serializable;
public class hellobean implements serializable {
  private static final long serialversionuid = 570423789882653763l;
  private string name;
  private int age;
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public int getage(){
    return age;
  }
  public void setage(int age){
    this.age = age;
  }
}

complexdata.java

package hessian.test.server;
import java.io.serializable;
import java.util.arraylist;
import java.util.hashmap;
import java.util.map;

public class complexdata implements serializable{
  private static final long serialversionuid = 1l;
  private arraylist<hellobean> hellobeans;
  //private map<string, hellobean> hellobeanmap;
  private int number;
  public int getnumber(){
    return number;
  }
  public arraylist<hellobean> gethellobeans(){
    return hellobeans;
  }
  public void setdata(arraylist<hellobean> beans, int num){
    this.number = num;
    this.hellobeans = beans;
//    hellobeanmap = new hashmap<string, hellobean>();
//    for (hellobean hellobean : beans) {
//      if(!hellobeanmap.containskey(hellobean.getname()))
//      {
//        hellobeanmap.put(hellobean.getname(), hellobean);
//      }
//    }
  }
}

web.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="webapp_id" version="3.0">
  <display-name>hessian server</display-name>
  <servlet>
    <servlet-name>hessian</servlet-name>
    <servlet-class>com.caucho.hessian.server.hessianservlet</servlet-class>
    <init-param>
      <param-name>service-class</param-name>
      <param-value>hessian.test.server.helloimpl</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>/hessian</url-pattern>
  </servlet-mapping>
</web-app>

hessian客户端(c#):

定义一个与服务端对应的ihello接口:ihello.cs

  public interface ihello
  {
    string sayhello(string msg);
    void sayhello2(int bean);
    void print(string msg);
    hellobean getdata(hellobean bean);
    hellobean[] getbeanlist();
    complexdata getcomplexdata();
  }

定义与服务器端一致的的通信数据类:

hellobean.cs:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace hessian.test.server
{
  public class hellobean
  {
    public string name 
    {
      set { name = value; }
      get { return name; }
    }

    private string name; //类型和名称需要和服务器端一致

    public int age
    {
      set { age = value; }
      get { return age; }
    }

    private int age; //类型和名称需要和服务器端一致

    public override string tostring()
    {
      return "name: "+ name + " age: " + age;
    }

  }
}

complexdata.cs:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace hessian.test.server
{
  public class complexdata
  {
    private hellobean[] hellobeans;

    //private dictionary<string, hellobean> hellobeanmap;

    private int number;

    public int getnumber()
    {
      return number;
    }

    public hellobean[] getbeans()
    {
      return hellobeans;
    }

    //public dictionary<string, hellobean> getbeansdic()
    //{
    //  return hellobeanmap;
    //}
  }
}

在主项目中添加hessiancsharp.dll引用。

测试代码:

using system;
using system.collections.generic;
using system.linq;
using system.text;

using hessiancsharp.client;
using hessian.test.server;

namespace hessianclienttest
{
  class program
  {
    static void main(string[] args)
    {
      string url = @"http://localhost:8080/hessianservertest/hessian";
      chessianproxyfactory factory = new chessianproxyfactory();

      ihello test = (ihello)factory.create(typeof(ihello), url);

      //test function
      console.writeline(test.sayhello("lu"));  //打印从服务器端获取的字符串
      test.sayhello2(12);            //在服务器端控制台打印 "hello 12"  
      test.print("hessian");          //在服务器端控制台打印 "hessian" 

      //test object
      hellobean bean = new hellobean();
      //bean.setname("lu xiaoxun");
      bean.name = "luxiaoxun";
      hellobean result = test.getdata(bean);
      console.writeline(result.name);
      console.writeline(result.age);
      console.writeline(result);

      //test object array
      hellobean[] beans = test.getbeanlist();
      if (beans != null)
      {
        foreach (hellobean data in beans)
        {
          console.writeline(data.tostring());
        }
      }

      //test complex data
      complexdata complexdata = test.getcomplexdata();
      if (complexdata != null)
      {
        console.writeline("array number: " + complexdata.getnumber());

        hellobean[] comarray = complexdata.getbeans();
        if (comarray != null)
        {
          foreach (hellobean data in comarray)
          {
            console.writeline(data.tostring());
          }
        }

        //dictionary<string, hellobean> hellobeanmap = complexdata.getbeansdic();
        //if (hellobeanmap != null)
        //{
        //  foreach (string key in hellobeanmap.keys)
        //  {
        //    console.writeline(hellobeanmap[key].gethellobeaninfo());
        //  }
        //}
      }

      console.readkey();
    }
  }
}

测试结果如下图所示:

java和c#使用hessian通信的方法

注意事项:

1、服务端和客户端用于数据传递的对象的命名空间要一致

ihello接口所在命名空间服务端和客户端可以不一致,但是ihello中用到的hellobean和complexdata在java服务端和c#客户端中两个hellobean类所在的命名空间要一致。

2、类的字段要一致

用于数据传输的类的字段名和字段类型要一致(修饰类型可以不一致)。

3、服务端的类要序列化

4、尽量使用基本的数据类型
从上面的测试可以看出,传递基本的类型没有问题,传递普通的类对象没有问题,传递arraylist的时候也没有问题(c#客户端使用array数组),但是传递hashmap字典的时候会有问题,c#这边使用dictionary没法对应一致,可能是由于hash函数内部实现不一致导致的,具体原因不明。

感兴趣的朋友可以测试一下本文实例,源码点击此处本站下载

上一篇:

下一篇: