I have a SmartGWT 4.0 application, and I have a working RESTful web-service (Spring MVC controller), and using a standard DataSource where I am overriding the transformResponse.
The DataSource works great when I pass an ID to the controller, an I get JSON data back which works fine. The problem is when I get a NULL back from the restful web-service.
For SmartGWT datasources, should I even get a null back? If I pass in an id to my web-service, and it doesn't find a matching record in the database, should I return a null back from my web-service, in which case, there would be no JSON, just an empty string ("").
So, I could update my transformResponse to handle this as follows:
ListGridRecord[] recordArray = new ListGridRecord[]{};
dsResponse.setData(recordArray);
dsResponse.setStartRow(0);
dsResponse.setEndRow(0);
dsResponse.setStatus(RPCResponse.STATUS_SUCCESS);
dsResponse.setTotalRows(0);
data = "{}";
But this still generates an error, which I'd like to avoid.
Or, another possibility would be to return an empty object. So, in the case when there is an object in the database, that object would be populated. If there is no matching data, then I could return an empty object from the web-service as follows:
MyObject myObject = new MyObject();
Which is preferable? I am leaning toward the latter ... an empty object.
I'll update this question when I have some more info. Thanks!
Set a property in DS Response and read it at client side to check for no result found.
Please try this code:
Server Side:
import com.isomorphic.datasource.DSResponse;
DSResponse dsResponse = new DSResponse(DSResponse.STATUS_SUCCESS);
dsResponse.setProperty("json_string", null);
dsResponse.setTotalRows(0);
Client Side:
import com.smartgwt.client.data.DSResponse;
String json=response.getAttribute("json_string");