Xstream处理xfire的webservice复杂对象的传输

xfire项目需要引入xpp3_min-1.1.3.4.O.jar,xstream-1.2.2.jar包
客户端和服务端都是webservice项目
服务端代码
服务端复杂的实体对象
package com.testxmlstream.domain;

import java.util.List;

public class PersonMess {
public final static String PERSONMESS="personMess";
public final static String USER="user";
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
private List<User> userlist;
public PersonMess(String firstname,String lastname){
this.firstname=firstname;
this.lastname=lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public PhoneNumber getPhone() {
return phone;
}
public void setPhone(PhoneNumber phone) {
this.phone = phone;
}
public PhoneNumber getFax() {
return fax;
}
public void setFax(PhoneNumber fax) {
this.fax = fax;
}
public List<User> getUserlist() {
return userlist;
}
public void setUserlist(List<User> suserlisterlist) {
this.userlist = suserlisterlist;
}

}

package com.testxmlstream.domain;

public class PhoneNumber {
private int code;
private String number;

public PhoneNumber(int code, String number) {
this.code = code;
this.number = number;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}

}

package com.testxmlstream.domain;

public class User {
private String userId;
private String userName;

public User(String userId, String userName) {
this.userId = userId;
this.userName = userName;
}
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;
}

}

服务端接口与实现类
package com.testxmlstream.service;

public interface WebServiceXmlStreamService {
public String getXmlStream();
}

package com.testxmlstream.service;

import java.util.ArrayList;
import java.util.List;

import com.testxmlstream.domain.PersonMess;
import com.testxmlstream.domain.PhoneNumber;
import com.testxmlstream.domain.User;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class WebServiceXmlStreamServiceImp implements
WebServiceXmlStreamService {

public String getXmlStream() {
// TODO Auto-generated method stub
XStream xstream = new XStream(new DomDriver());
xstream.alias(PersonMess.PERSONMESS, PersonMess.class);
xstream.alias(PersonMess.USER, User.class);
xstream.alias("person", PersonMess.class);
xstream.alias("phonenumber", PhoneNumber.class);
PersonMess joe = new PersonMess("Joe", "Walnes");
User user=new User("11111","22222222");
User user2=new User("33333333","4444444444");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
List <User> userlist=new ArrayList<User>();
userlist.add(user);
userlist.add(user2);
joe.setUserlist(userlist);
String xml = xstream.toXML(joe);
System.out.println(xml);
return xml;
}

}

客户端通过myeclipse生成IWebServiceXmlStreamServiceClient.java,然后在生成类客户端类的main方法中加入以下代码
public static void main(String[] args) {
       

        IWebServiceXmlStreamServiceClient client = new IWebServiceXmlStreamServiceClient();
       
//create a default service endpoint
        IWebServiceXmlStreamServicePortType service = client.getIWebServiceXmlStreamServiceHttpPort();
        String xml=service.getXmlStream();
        XStream xstream = new XStream(new DomDriver());
        xstream.alias(PersonMess.PERSONMESS, PersonMess.class);
xstream.alias(PersonMess.USER, User.class);
xstream.alias("person", PersonMess.class);
xstream.alias("phonenumber", PhoneNumber.class);
PersonMess newJoe = (PersonMess)xstream.fromXML(xml);
System.out.println("-------------------------------");
System.out.println(newJoe.getFirstname());
System.out.println(newJoe.getPhone().getNumber());
List<User>userlist=newJoe.getUserlist();
for(User user:userlist){
System.out.println(user.getUserId());
}
//TODO: Add custom client code here
        //
        //service.yourServiceOperationHere();
       
System.out.println("test client completed");
        System.exit(0);
    }


你可能感兴趣的:(webservice)