【问题标题】:How to pass custom object through Web Service如何通过 Web Service 传递自定义对象
【发布时间】:2011-06-22 07:08:47
【问题描述】:

我用 Java 编写了一个 Web 服务,该 Web 服务运行正常,但我不知道如何通过它传递自定义对象列表。这是我的类对象:

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

}

这是我查询数据库并返回对象列表的 Web 服务方法。每次我尝试返回一个对象时,我都会收到一个axis2错误:[ERROR] org.apache.axis2.AxisFault: Mapping qname not fond for the package: wtp

无论如何,这是我的方法:

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

我在 Java 中使用 Eclipse Galileo,Axis2。任何关于如何传递这些对象的想法都将不胜感激。或者也许,甚至比低音自定义对象更好的方法。我对任何想法持开放态度。

谢谢!

编辑:我编辑了要使用的方法:

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

但是,它返回为空的对象。例如:

数据库中有 4 行,所以结果是:

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

有什么想法吗?

【问题讨论】:

  • 我用似乎让我更接近结果的代码编辑了上面的帖子。但是,无论如何,我都会收到空值。

标签: java android mysql web-services axis2


【解决方案1】:

我没有将 Axis2 用于 Web 服务,但我使用过 XFire 和 CXF。使用这些框架,您必须使用 XmlBeans 或 JAXB 用正确的注释标记您的域对象。 然后配置框架的 servlet 以使用您最喜欢的 Xml 库 (XmlBeans/JAXB)。

【讨论】:

    【解决方案2】:

    编辑的方法确实有效。我必须使用返回类型 Contacts[] 将返回类型设为联系人数组。与:

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

    按应有的方式工作。编辑到这个^^后,问题是我如何加载对象。正确的做法是:记住它在一个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;
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 2021-12-24
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2015-11-04
      • 2019-09-17
      相关资源
      最近更新 更多