简体   繁体   中英

How to pass custom object through Web Service

I wrote a web service in Java and the web service is functioning correctly, but I can't figure out how to pass a list of custom objects through it. Here is my class object:

public class Contacts{
private int id;
private String username;
private String location;
private Date updated_at;

public void setId(int id) {
    this.id = id;
}
public int getId() {
    return id;
}
public void setUsername(String username) {
    this.username = username;
}
public String getUsername() {
    return username;
}
public void setLocation(String location) {
    this.location = location;
}
public String getLocation() {
    return location;
}
public void setUpdated_at(Date updated_at) {
    this.updated_at = updated_at;
}
public Date getUpdated_at() {
    return updated_at;
}

}

Here is my web service method to query the database and return the List of objects. Every time I try to return a object I get an axis2 error: [ERROR] org.apache.axis2.AxisFault: Mapping qname not fond for the package: wtp

Anyway, here is my Method:

    public String getUsers()
  {     
        ResultSet mainRS = null;
        String username = "root";
        String password = "ticket";
        String tablename = "users";
        String fieldname = "*";

        String query = "SELECT " + fieldname + " FROM " + "android." + tablename + ";";
        ArrayList<Contacts> lstc = new ArrayList<Contacts>();

        /* this chnk of code can appear more or less verbatim */
        /* in your database apps (including JSPs) */
        String url = "jdbc:mysql://"my IP address":3306/android";
        String test = " ";
        try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection(url, username, password);
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        mainRS = rs;

        while (rs.next()){
            Contacts cont = new Contacts();

            if (!(rs.getString("username") == null)){
            cont.setUsername(rs.getString("username"));
            test = rs.getString("username");
            }

            if (!(rs.getString("location") == null)){
             cont.setLocation(rs.getString("location"));
            }

            if (!(rs.getString("updated_at") == null)){
             cont.setUpdated_at(rs.getDate("updated_at"));
            }

            if (!(rs.getString("idUsers") == null)){
             cont.setId(rs.getInt("idUsers"));

            }

            lstc.add(cont);
        }

        rs.close();
        stmt.close();
        con.close();

        }catch(Exception e){
            test = e.toString();
        }
        return test;            
        }

I am using Eclipse Galileo, Axis2 in Java. Any ideas on how to pass these objects would be much appreciated. Or maybe, even a better way to do it then to bass custom objects. I am open to any ideas.

Thanks!

EDIT: I edited the method to use:

Contacts lst[] = new Contacts["count rows here"];

But, it returns Objects that are null. For example:

There are 4 rows in the database so the result is:

SoapObject: [result:null, result:null, result:null,result:null]

Any Ideas?

I haven't used Axis2 for web services, but I've used XFire and CXF. With these frameworks, you have to mark your domain object with the correct annotation using either XmlBeans or JAXB. Then you configure the framework's servlet to use your favorite Xml Library (XmlBeans/JAXB).

The edited method did work.. I had to make the return type a Array of Contacts using the return type Contacts[]. With the:

Contacts contact[] = new Contacts[Count rows here];

Worked as it should. After editing to this^^ the problem was how I was loading the objects. The correct way is: Remember it's in a While(ResultSet.next)

int num = 0;
Contacts cont = new Contacts();
cont.setId = "id";
cont.setLocation = "location";
//finish filling your object
contact[num] = cont;
num = num + 1;

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM