Webserviceアクセス通常オブジェクトタイプ

4785 ワード

1、新しい学生類databean
package databean;

public class Student implements java.io.Serializable
 {

	private static final long serialVersionUID = 1L;

	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;
	}
}

 
 
2、新しいwebserviceサービスクラス、3種類の方法、String配列、byte配列、Studentオブジェクトbeanを含む
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

import databean.Student;

public class ObjectService {
	
    /*
     *  
     */
    public String[] getArray()
    {
        String[] strArray = new String[]{ " 1", " 2", " 3" };
        return strArray;
    } 
    /*
     *    Student 
     */
    public Student getStudent(String name,int age)
    {
    	Student student = new Student();
    	student.setName(name);
    	student.setAge(age);
        return student;
    }
    /*
     *   Student , 
     */
    public byte[] getStudentBytes(String name,int age) throws Exception 
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        
        //  student  ObjectOutputStream 
        Student student = new Student();
    	student.setName(name);
    	student.setAge(age);
        oos.writeObject(student);      
       
        return baos.toByteArray();
    }  
}

 
3、新規クライアントプログラム
package client;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.Date;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import databean.Student;

public class ObjectRPCClient
{

    public static void main(String[] args) throws Exception
    {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/ObjectService");
        options.setTo(targetEPR);
       
        System.out.println(new Date()+" ----  getArray-----");
        QName opAddEntry = new QName("http://ws.apache.org/axis2", "getArray");
        String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,new Object[]{}, new Class[]{String[].class })[0];
        for (String s : strArray){ 	
        	System.out.println(s);
        }

        System.out.println(new Date()+" ----  getStudent(String name,int age)-----");
        opAddEntry = new QName("http://ws.apache.org/axis2", "getStudent");
        databean.Student student = (databean.Student) serviceClient.invokeBlocking(
        		opAddEntry, 
        		new Object[]{" ",20},
        		new Class[]{databean.Student.class})[0];
        System.out.println(student.getName()+"  "+student.getAge());
       
        System.out.println(new Date()+" ----  getStudentBytes(String name,int age)-----");
        opAddEntry = new QName("http://ws.apache.org/axis2", "getStudentBytes");
        byte[] bytes = (byte[]) serviceClient.invokeBlocking(opAddEntry, 
        		new Object[]{" ",21},
        		new Class[]{byte[].class})[0];
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        student = (Student) ois.readObject();
        System.out.println(student.getName()+"  "+student.getAge());
    }
}

 
 
4、Studentコンパイル後のclassファイルをaxis 2WEB-INFclassesdatabeanディレクトリの下に置き、ObjectServices.JAvaコンパイル後のclassファイルはaxis 2WEB-INFpojoディレクトリの下に置かれます.
5、クライアントプログラムを実行する.
6、出力結果は以下の通り:
Thu Mar 15 21:29:38 CST 2012 ----  getArray-----
 1
 2
 3
Thu Mar 15 21:29:38 CST 2012 ----  getStudent(String name,int age)-----
   20
Thu Mar 15 21:29:38 CST 2012 ----  getStudentBytes(String name,int age)-----
   21