简体   繁体   中英

Jersey webservice returning a null JSON

I want to get the data of a database table to the client end. I'm sending the data via a JSON. When I print the output result in the client end it gives the following result.

{"pricing":null}

When I print return statement in the server end, it outputs the following

[Connection.Pricing@3d5bae2]

There are no errors. What have I done wrong?

Here is my Client Side Code

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;

This is my Server End

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

Here is my POJO class in the server end

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

You need to override toString method in your Pricing class to print the object in a beautiful way. The default toString () method shows the object class and its hash code separated by @ character and hence you see this

Pricing@3d5bae2

Here is one implmentation of toString method for Pricing class:

@Override
public String toString() {
    return "Pricing [category=" + category + ", lower_limit=" + lower_limit
            + ", class_no=" + class_no + ", value=" + value + ", employee="
            + employee + ", upper_limit=" + upper_limit + "]";
}

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