在AXIS服务间传递JavaBean

自定义JavaBean如下:

package org.angus.webservice.object;

import java.io.Serializable;

public class CustomerObj implements Serializable {

    private static final long serialVersionUID = -8342351994159763818L;

    private String userName;

    private String userId;

    private int age;

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

    public String getUserId() {

       return userId;

    }

    public void setUserId(String userId) {

       this.userId = userId;

    }

    public String getUserName() {

       return userName;

    }

    public void setUserName(String userName) {

       this.userName = userName;

    }

}

web服务:


package org.angus.webservice;

import org.angus.webservice.object.CustomerObj;

public class CustomerObjTest {

    public String inCusObj(CustomerObj cusObj)

    {

       String cusStr = "";

       if(cusObj != null)

       {

           cusStr += "UserId: "+cusObj.getUserId()+

              ";UserName: "+cusObj.getUserName()+";Age: "+cusObj.getAge();

       }

       return cusStr;

    }

    public CustomerObj outCusObj(String userId,String userName,int age)

    {

       CustomerObj customerObj = new CustomerObj();

       customerObj.setUserId(userId);

       customerObj.setUserName(userName);

       customerObj.setAge(age);

       return customerObj; 

    }

}

将上述java文件编译后的class文件及包文件夹拷贝至axis/WEB-INF/classes目录下
修改修改服务器端配置文件server-config.wsdd

<service name="CustomerObjTest" provider="java:RPC">

       <parameter name="allowedMethods" value="*" />

       <parameter name="className"

           value="org.angus.webservice.CustomerObjTest" />

       <beanMapping

           languageSpecificType="java:org.angus.webservice.object.CustomerObj"

           qname="ns1:CustomerObj" xmlns:ns1="urn:BeanService" />

</service>

与之前的部署比较多了
< beanMapping

           languageSpecificType="java:org.angus.webservice.object.CustomerObj"

           qname = "ns1:CustomerObj" xmlns:ns1 = "urn:BeanService" />
languageSpecificType属性指定JavaBean类文件位置,例如:
languageSpecificType="java:com.ronghao.axis.Order"
qname属性指定JavaBean类的名字
其他是固定的。

客户端访问代码(部分):
//导入的类是用WTP生成的客户端代码,在此不做介绍

package com.webclient;

import java.net.URL;

import org.angus.webservice.object.CustomerObj;

import org.angus.webservice.object.CustomerObject;

import localhost.axis.services.CustomerObjTest.CustomerObjTest;

import localhost.axis.services.CustomerObjTest.CustomerObjTestService;

import localhost.axis.services.CustomerObjTest.CustomerObjTestServiceLocator;

 

public class TestClient {

       /**

        * @param args

        */

       public static void main(String[] args)throws Exception {

              try

              {

                     //自定义对象(简单类型)测试

                     CustomerObjTestService CustomerObjService = new CustomerObjTestServiceLocator();

                     //CustomerObjTest customerObjTest =  CustomerObjService.getCustomerObjTest();

                     CustomerObjTest customerObjTest =  CustomerObjService.getCustomerObjTest(new URL("http://localhost:8080/axis/services/CustomerObjTest"));

                     CustomerObj customerObj = new CustomerObj();

                    

                     customerObj.setUserId("25");

                     customerObj.setUserName("钰莹");

                     customerObj.setAge(20);

                    

                     String customerStr = customerObjTest.inCusObj(customerObj);

                     System.out.println(customerStr);

          

                     CustomerObj outCustomerObj = customerObjTest.outCusObj("99", "晓明", 25);

                     System.out.println("ID: "+outCustomerObj.getUserId()+

                                   "Name: "+outCustomerObj.getUserName()+

                                   "Age: "+outCustomerObj.getAge());

              }

              catch(Exception e)

              {

                     e.printStackTrace();

              }

       }

}

你可能感兴趣的:(webservice,javabean,axis)