Java6 WebService 使用复杂对象

Java6 WebService 使用复杂对象
 
复杂对象可表示的语义比简单对象强大很多,这里看看在Java6的WebService中如何使用复杂对象参数。
这里的参数分两类,一类是入参,一类是返回值。
 
复杂对象在并不像想象中那么简单,其中还有一些玄机。请看实例:
 
1、开发服务,统统在服务端
 
package lavasoft.server;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
* Java6开发的WebService服务端
*
* @author leizhimin 2009-11-16 10:24:13
*/
@WebService
public class Java6WS {

         //返回一个复杂对象
         public UserBean doSomething1(String username, String address, boolean flag) {
                 return new UserBean(username, address, flag);
        }

         //接收一个复杂对象
         public String doSomething2(Foo foo) {
                 return foo.toString();
        }

         public static void main(String[] args) {
                 //发布一个WebService
                Endpoint.publish( "http://192.168.14.117:9999/java6ws/Java6WS", new Java6WS());
        }
}
 
package lavasoft.server;

/**
* 用户信息
*
* @author leizhimin 2009-11-16 16:43:35
*/
public class UserBean {
         private String username;
         private String address;
         private boolean flag;

         public UserBean() {
        }

         public UserBean(String username, String address, boolean flag) {
                 this.username = username;
                 this.address = address;
                 this.flag = flag;
        }

         public String getUsername() {
                 return username;
        }

         public void setUsername(String username) {
                 this.username = username;
        }

         public String getAddress() {
                 return address;
        }

         public void setAddress(String address) {
                 this.address = address;
        }

         public boolean isFlag() {
                 return flag;
        }

         public void setFlag( boolean flag) {
                 this.flag = flag;
        }

        @Override
         public String toString() {
                 return "UserBean{" +
                                 "username='" + username + '\'' +
                                 ", address='" + address + '\'' +
                                 ", flag=" + flag +
                                '}';
        }
}
 
package lavasoft.server;

/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-11-16 17:09:46
*/
public class Foo {
         private String name;
         private String sex;

         public Foo() {
        }

         public Foo(String name, String sex) {
                 this.name = name;
                 this.sex = sex;
        }

         public String getName() {
                 return name;
        }

         public void setName(String name) {
                 this.name = name;
        }

         public String getSex() {
                 return sex;
        }

         public void setSex(String sex) {
                 this.sex = sex;
        }

        @Override
         public String toString() {
                 return "Foo{" +
                                 "sex='" + sex + '\'' +
                                 ", name='" + name + '\'' +
                                '}';
        }
}
 
2、生成客户端代码
 
Java6 WebService 使用复杂对象_第1张图片
 
 
可以清楚的看出,在客户端代码中,已经生成了服务接口和服务实现类,还生成了UserBean和Foo两个类,而这两个类和服务端的内容不同。
 
3、写客户端测试代码
 
package lavasoft;

import lavasoft.wsclient.Foo;
import lavasoft.wsclient.Java6WS;
import lavasoft.wsclient.Java6WSService;
import lavasoft.wsclient.UserBean;

/**
* 测试Java6 WS生成的客户端代码
*
* @author leizhimin 2009-11-16 17:05:00
*/
public class TestClient {
         public static void main(String[] args) {
                 //创建一个客户端服务对象
                Java6WS java6WS = new Java6WSService().getJava6WSPort();

                System.out.println( "----------java6WS.doSomething1-------------");
                 //调用服务方法,并得到方法返回值
                UserBean userBean = java6WS.doSomething1( "zhangsan", "华中路", true);
                 //打印服务的返回值
                System.out.println(userBean.toString());
                System.out.println(userBean.getUsername() + " " + userBean.getAddress() + " " + userBean.isFlag());

                System.out.println( "----------java6WS.doSomething2-------------");
                Foo f = new Foo();
                f.setName( "李四");
                f.setSex( "Man");
                String sf = java6WS.doSomething2(f);
                System.out.println(sf);
        }
}
 
4、执行客户端
 
执行前先启动服务端。
执行结果如下:
 
可以看出,测试很成功。
 
5、总结
 
这里面其实有些细节,需要说明白:
1)、对于复杂对象,必须有一个不带参数的构造方法。
2)、服务端覆盖Object的方法不能在客户端复原,比如toString方法。
3)、服务端的带参数构造方法在客户端失效。
 
以上三点是非常重要的,如果不知道会很郁闷,老出错还找不到原因。

你可能感兴趣的:(webservice,职场,Java6,休闲)