微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService大讲堂之Axis22:复合类型数据的传递

本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇WebService大讲堂之Axis2(1):用POJO实现0配置的WebService

    在实际的应用中,不仅需要使用WebService来传递简单类型的数据,有时也需要传递更复杂的数据,这些数据可以被称为复合类型的数据。数组与类(接口)是比较常用的复合类型。在Axis2中可以直接使用将WebService方法的参数或返回值类型声明成数组或类(接口)。但要注意,在定义数组类型时只能使用一维数组,如果想传递多维数组,可以使用分隔符进行分隔,如下面的代码所示:

String[] strArray = new String[]{ "自行车,飞机,火箭","中国,美国,德国","超人,蜘蛛侠,钢铁侠" } ;

    上面的代码可以看作是一个3*3的二维数组。

    在传递类的对象实例时,除了直接将数组类型声明成相应的类或接口,也可以将对象实例进行序列化,也就是说,将一个对象实例转换成字节数组进行传递,然后接收方再进行反序列化,还原这个对象实例。

    下面的示例代码演示了如何传递数组与类(接口)类型的数据,并演示如何使用字节数组上传图像。本示例的客户端代码使用JavaC#编写。要完成这个例子需要如下几步:

一、实现服务端代码

normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

    ComplexTypeService一个WebService类,该类的代码如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> import  java.io.FileOutputStream;
 data.DataForm;

public   class  ComplexTypeService
{
    
//   上传图像,imageByte参数表示上传图像文件的字节,
    
  length参数表示图像文件的字节长度(该参数值可能小于imageByte的数组长度)
     boolean  uploadImageWithByte( byte [] imageByte,  int  length)
    {
        FileOutputStream fos 
= null ;
        
try
        {
            
  将上传的图像保存在D盘的test1.jpg文件             fos  new  FileOutputStream( " d://test1.jpg );
            
  开始写入图像文件的字节             fos.write(imageByte,0);">0 , length);
            fos.close();
        }
        
catch  (Exception e)
        {
            
return false ;
        }
        
finally if  (fos  != )
            {
                

                {
                    fos.close();
                }
                
 (Exception e)
                {

                }
            }
        }
        
true ;
    }
    
  返回一维字符串数组  String[] getArray()
    {
        String[] strArray 
 String[]{  自行车 飞机 火箭  };
        
 strArray;
    } 
    
  返回二维字符串数组  String[] getMDArray()
    {
        String[] strArray 
自行车,飞机,火箭 中国,美国,德国 超人,蜘蛛侠,钢铁侠  } ;
        
 strArray;
    }
    
  返回DataForm类的对象实例  DataForm getDataForm()
    {
        
 DataForm();
    }
    
  将DataForm类的对象实例序列化,并返回序列化后的字节数组 [] getDataFormBytes()  throws  Exception 
    {
        java.io.ByteArrayOutputStream baos 
 java.io.ByteArrayOutputStream();
        java.io.ObjectOutputStream oos 
 java.io.ObjectOutputStream(baos);
        oos.writeObject(
 DataForm());        
        
 baos.toByteArray();
    }    
}

  normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

二、实现DataForm

    DataForm是要返回的对象实例所对应的类,该类的实现代码如下:

package  data;

 DataForm  implements  java.io.Serializable
{
    
private  String name  bill ;
    
 age  20 ;

    
 String getName()
    {
        
 name;
    }
    
void  setName(String name)
    {
        
this .name   getAge()
    {
        
 age;
    }
    
 setAge(  age)
    {
        
.age   age;
    }
}
normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

三、发布WebService

    由于本示例的WebService类使用了一个Java类(DataForm类),因此,在发布WebService之前,需要先将DataForm.class文件复制到<Tomcat安装目录>/webapps/axis2/WEB-INF/classes/data目录中,然后将ComplexTypeService.class文件复制到<Tomcat安装目录>/webapps/axis2/WEB-INF/pojo目录中,最后启动Tomcat(如果Tomcat已经启动,由于增加一个DataForm类,因此,需要重新启动Tomcat)。

四、使用Java编写调用WebService的客户端代码

    在客户端仍然使用了RPC调用方式,代码如下:

 client;

 javax.xml.namespace.QName;
 org.apache.axis2.addressing.EndpointReference;
 org.apache.axis2.client.Options;
 org.apache.axis2.rpc.client.RPCServiceClient;

 ComplexTypeRPcclient
{

    
static  main(String[] args)   Exception
    {
        RPCServiceClient serviceClient 
 RPCServiceClient();
        Options options 
 serviceClient.getoptions();
        EndpointReference targetEPR 
 EndpointReference(
                
http://localhost:8080/axis2/services/ComplexTypeService );
        options.setTo(targetEPR);
        
 下面的代码调用uploadImageWithByte方法上传图像文件          //////////////////////////////////////// /
        
 打开图像文件,确定图像文件的大小         java.io.File file   java.io.File( f://images.jpg );
        java.io.FileInputStream fis 
 java.io.FileInputStream( );
        
 创建保存要上传的图像文件内容的字节数组 [] buffer  [( ) file.length()];
        
 将图像文件内容读取buffer数组中  n   fis.read(buffer);
        System.out.println(
文件长度: +  file.length());
        Object[] opAddEntryArgs 
 Object[]{ buffer, n };
        Class[] classes 
 Class[]{ Boolean.  };
        QName opAddEntry 
 QName( http://ws.apache.org/axis2 uploadImageWithByte );
        fis.close();
        
 开始上传图像文件,并输出uploadImageWithByte方法的返回传         System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[ ]);
        
/
        
        
 下面的代码调用了getArray方法,并返回一维String数组 /           opAddEntry  getArray );
        String[] strArray 
 (String[]) serviceClient.invokeBlocking(opAddEntry,255);">
                            new
 Object[]{},0);"> Class[]{String[].  })[ ];
        
for  (String s : strArray)
            System.out.print(s 
   );
        System.out.println();
        

        

        
 下面的代码调用了getMDArray方法,并返回一维String数组 getMDArray );
        strArray 

                                                          new
})[  (String s : strArray)
        {
            String[] array 
 s.split( (String ss: array)
                System.out.print(
<  ss  );
            System.out.println();
        }
        System.out.println();
        


        
 下面的代码调用了getDataForm方法,并返回DataForm对象实例 getDataForm );
        data.DataForm df 
 (data.DataForm) serviceClient.invokeBlocking(opAddEntry,
                                                                 
 Class[]{data.DataForm. ];
        System.out.println(df.getAge());
        
 下面的代码调用了getDataFormBytes方法,并返回字节数组,最后将返回的字节数组反序列化后,转换成DataForm对象实例 /           getDataFormBytes );
        buffer 
 ( []) serviceClient.invokeBlocking(opAddEntry,0);"> Class[]{ []. ];
        java.io.ObjectInputStream ois 
 java.io.ObjectInputStream(
                
 java.io.ByteArrayInputStream(buffer));
        df 
 (data.DataForm) ois.readobject();
        System.out.println(df.getName());
        
//////////////////////////////////////////
    }
}

normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

    运行上面的程序,将输出如下的内容

3617

true

自行车 飞机 火箭 

>

中国 美国 德国 超人 蜘蛛侠 钢铁侠

bill

五、使用C#编写调用WebService的客户端代码

    Visual Studio中使用WebService就简单得多。假设引用WebService时的引用名为complexType,则下面的代码调用uploadImageWithByte方法上传图像文件。在Visual Studio引用WebService时,uploadImageWithByte方法多了两个out参数,在使用时要注意。

complexType.ComplexTypeService cts   WSC.complexType.ComplexTypeService();
System.IO.FileStream fs 
 System.IO.FileStream( @" f:/images.jpg stem.IO.FileMode.Open);
[fs.Length];
fs.Read(buffer, (
)fs.Length);
bool  r;
 rs;
cts.uploadImageWithByte( buffer,0);">)fs.Length,255);">out
 r,0);"> rs);
normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

    在获得二维数组时,可以将数据加载到DataGridView或其他类似的控件中,代码如下:

String[] strArray   cts.getMDArray();
 i  ; i   strArray.Length; i ++ )
{
    
  用正则表达式将带分隔符的字符串转换成String数组     String[] columns   strArray[i].Split( ' );
    
  如果DataGridView的表头不存在,向DataGridView控件添加三个带表头的列  (dataGridView1.Columns.Count  == )
        
 j  ; j   columns.Length; j )
            dataGridView1.Columns.Add(
column  (j  1 ).ToString(),0);">列 ).ToString());
    
  添加     dataGridView1.Rows.Add( ( )
    {
        dataGridView1.Rows[i].Cells[j].Value 
 columns[j];                       
    }                
}

normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

    DataGridView控件添加数据后的效果如图1所示。



图1 

normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 /* Style DeFinitions */ table.MsonormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";}

    对于其他的WebService方法调用都非常简单,读者可以自己做这个实验。

    要注意的是,由于.netjava序列化和反序列化的差异,通过序列化的方式传递对象实例只使用于客户端与服务端为同一种语言或技术的情况,如客户端和服务端都使用Java来编写。

    如果读者要上传文件,应尽量使用FTP的方式来传递,而只通过WebService方法来传递文件名等信息。这样有助于提高传输效率。
下一篇WebService大讲堂之Axis2(3):使用services.xml文件发布WebService

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐