2

我想将数据库表的数据获取到客户端。我通过 JSON 发送数据。当我在客户端打印输出结果时,它会给出以下结果。

{"pricing":null}

当我在服务器端打印返回语句时,它输出以下内容

[Connection.Pricing@3d5bae2]

没有错误。我做错了什么?

这是我的客户端代码

public String loadTable(String tablename) throws ClientProtocolException, IOException {
    pathParams.add("tablename", tablename);
    ClientResponse response = service.path("access").path("loadtable").queryParams(pathParams).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    String responseString = response.getEntity(String.class);
    return responseString;

这是我的服务器端

@Path("/loadtable")
@GET
@Produces(MediaType.APPLICATION_JSON) 
public List<Pricing> loadTable(@QueryParam("tablename") String tablename) throws Exception {
    List<Pricing> pricing = new ArrayList<Pricing>();
    try {
        query = c.prepareStatement("select * from " + tablename);
        ResultSet ets_rs = query.executeQuery();

        while (ets_rs.next()) {
        pricing.add(new Pricing(ets_rs.getString(1), ets_rs.getString(2),  ets_rs.getString(3), ets_rs.getString(4), ets_rs.getString(5), ets_rs.getString(6)));
        }
        query.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return pricing;

这是我在服务器端的 POJO 类

@XmlRootElement
class Pricing {

String category;
String lower_limit;
String class_no;
String value;
String employee;
String upper_limit;

public Pricing() {
}



Pricing(String a, String b, String c, String d, String e, String f) {
    category = a;
    lower_limit = b;
    upper_limit = c;
    class_no = d;
    value = e;
    employee = f;
}
//getters
}
4

1 回答 1

1

您需要toString在您的类中重写方法以Pricing以漂亮的方式打印对象。默认toString() 方法显示对象类及其按@字符分隔的哈希码,因此您会看到

Pricing@3d5bae2

这是类toString方法的一种实现Pricing

@Override
public String toString() {
    return "Pricing [category=" + category + ", lower_limit=" + lower_limit
            + ", class_no=" + class_no + ", value=" + value + ", employee="
            + employee + ", upper_limit=" + upper_limit + "]";
}
于 2013-11-08T03:26:36.997 回答