2

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
}
Dimi
  • 91
  • 2
  • 3
  • 10
  • Looks like you're outputting an object, not a string. – informatik01 Nov 08 '13 at 03:26
  • In other words, at the client end you have received a JSON, that's why it is printed as JSON string. But on the server side, when you print it, there is NO conversion jet happened, so you are printing/outputting an object (which is not jet converted to JSON) – informatik01 Nov 08 '13 at 03:40
  • @informatik01 Yes. thanks. I added a toString() method as mentioned below by JunedAhsan. It did the trick to the server end output. But it still gives the previous client end output saying its null. Can you please help me there? – Dimi Nov 08 '13 at 03:46
  • You mean null in `{"pricing":null}`? – informatik01 Nov 08 '13 at 03:49
  • Yes. Still gives that output. Maybe some coding error in the client side code? – Dimi Nov 08 '13 at 03:51
  • Well, you DO receive a JSON, it just it's pricing property is set to null. Something is not being set on the server side, can't say what at the moment. By the way, in my last Jersey project I didn't use `@XmlRootElement` property, I just had an ordinary JavaBean, and all worked fine (i.e. it was successfully converted to JSON) – informatik01 Nov 08 '13 at 03:53
  • Okay. I will try that. I hope there are no client side coding errors in this? – Dimi Nov 08 '13 at 03:56
  • Have you annotated your webservice method, that suppose to return JSON like the following: `@Produces("application/json")` ? And JavaBean of course has to have getters AND setters... – informatik01 Nov 08 '13 at 03:57
  • @Path("/loadtable") @GET @Produces(MediaType.APPLICATION_JSON) – Dimi Nov 08 '13 at 03:58
  • Annotations seem to be OK. By the way, seems like because you are returning a list as JSON, `@XmlRootElement` is needed here, according to [this answer](http://stackoverflow.com/a/14326264/814702). In my project I had a list inside an object that I was returning, not a list of objects – informatik01 Nov 08 '13 at 04:06
  • @informatik01 i think i got something here, i printed the clientresponse and it gave the following output. "GET http://localhost:8080/ServerETS/resources/access/loadtable?tablename=prices returned a response status of 200" – Dimi Nov 08 '13 at 04:09
  • Also see those answers: [Return JSONArray instead of JSONObject, Jersey JAX-RS](http://stackoverflow.com/a/10922566/814702) and [How can I customize serialization of a list of JAXB objects to JSON?](http://stackoverflow.com/a/3143214/814702) – informatik01 Nov 08 '13 at 04:09
  • Have to go, sorry. Hope the above link will help you )) – informatik01 Nov 08 '13 at 04:22

1 Answers1

1

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 + "]";
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 1
    Thanks. It did the trick on the server side output. But the client side still receives a null JSON. – Dimi Nov 08 '13 at 03:41