Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to customize format expected from RESTData responce

    I tried to replace the RestDataSource in GWT sample code: RestfuleDataSourceSample.java with actual RestDataSource from our server (Progress OpenEdge REST server).

    The response from our server is as follows:-
    {"response":{"status":0,"ttCountry":{"ttCountry":[{"continent":"EU","countryName":"United Kingdom","countryCode":"UK"},{"continent":"Asia","countryName":"China","countryCode":"CN"}]}}}

    It does not conform to the format as shown in SmartGWT document, which says format should be:-
    {"response":{"status":0,"data"[......]....

    The response from my server does not get loaded into the grid in RestfulDataSource.

    I specify my RestDataSource as follows:-


    countryDS.setDataFormat( DSDataFormat.JSON);
    countryDS.setFetchDataURL("http://localhost:8980/TestREST2Service/rest/TestREST2Service/country");
    countryDS.setJsonRecordXPath("ttCountry");


    The following is the entry in the console window in eclipse:-

    === 2015-01-29 17:49:08,452 [2-29] DEBUG HttpProxyServlet - HttpProxy - ProxyData is: {
    xsi:"http://www.w3.org/2000/10/XMLSchema-instance",
    url:"http://localhost:8980/TestREST2Service/rest/TestREST2Service/country",
    httpMethod:"POST",
    params:{
    isc_dataFormat:"json"
    },
    contentType:"application/json",
    requestBody:"{\r \"dataSource\":\"isc_RestfulDataSourceSample_1_0\", \r \"operationType\":\"fetch\", \r \"startRow\":0, \r \"endRow\":50, \r \"sortBy\":[\r \"countryCode\"\r ], \r \"textMatchStyle\":\"substring\", \r \"componentId\":\"isc_ListGrid_0\", \r \"data\":{\r }, \r \"oldValues\":null\r}",
    callbackParam:""
    }
    === 2015-01-29 17:49:08,467 [2-29] INFO HttpProxyServlet - HttpProxy - Method succeeded: HTTP/1.1 200 OK
    === 2015-01-29 17:49:08,468 [2-29] INFO HttpProxyServlet - HttpProxy - Response:
    {"response":{"status":0,"ttCountry":{"ttCountry":[{"continent":"EU","countryName":"United Kingdom","countryCode":"UK"},{"continent":"Asia","countryName":"China","countryCode":"CN"}]}}}
    === 2015-01-29 17:49:08,468 [2-29] INFO Compression - /customds/sc/HttpProxy: 184 -> 132 bytes

    Smart GWT version : trial version SmartGWTEE 5.0.
    Browser : Internet Explorer 10

    Thanks and regards
    Kay Chee Cheung

    #2
    The best solution is the to change how your server responds to conform to RestDataSource expectations. Placing the status code at the same level as data fields is an architectural mistake since it means that "status" and other metadata fields such as startRow/endRow, invalidateCache, etc, may collide with data field names.

    If you have no choice in the matter and must conform to the bad message format your server is using, then rather than start from RestDataSource, you should start from DataSource and implement your own request and response processing logic using transformRequest and transformResponse. Documentation of this approach begins in the QuickStart Guide, Data Integration chapter. But again, this approach is far far worse than correcting your server code so that it follows the default RestDataSource protocol.

    Comment


      #3
      I am now able to load data from our PROGRESS Rest Server !

      I changed the format for JsonRecordXpath to below:-

      countryDS.setJsonRecordXPath("/response/data/ttCountry");

      I added the other parameters startRow, endRow, TotalRows as well. But the main change required is the JsonRecordXPath, which maps to the data node of json returned by the server.

      The following is the last output from my server:-

      === 2015-01-30 14:22:00,679 [2-31] DEBUG PoolableDataSourceFactory - Activated DataSource 16 of type 'Object'
      === 2015-01-30 14:22:00,680 [2-31] DEBUG HttpProxyServlet - HttpProxy - ProxyData is: {
      xsi:"http://www.w3.org/2000/10/XMLSchema-instance",
      url:"http://localhost:8980/TestREST2Service/rest/TestREST2Service/country",
      httpMethod:"POST",
      params:{
      isc_dataFormat:"json"
      },
      contentType:"application/json",
      requestBody:"{\r \"dataSource\":\"isc_RestfulDataSourceSample_1_0\", \r \"operationType\":\"fetch\", \r \"startRow\":0, \r \"endRow\":50, \r \"sortBy\":[\r \"countryCode\"\r ], \r \"textMatchStyle\":\"substring\", \r \"componentId\":\"isc_ListGrid_0\", \r \"data\":{\r }, \r \"oldValues\":null\r}",
      callbackParam:""
      }
      === 2015-01-30 14:22:00,695 [2-31] INFO HttpProxyServlet - HttpProxy - Method succeeded: HTTP/1.1 200 OK
      === 2015-01-30 14:22:00,695 [2-31] INFO HttpProxyServlet - HttpProxy - Response:
      {"response":{"status":0,"startRow":0,"endRow":1,"totalRows":2,"data":{"ttCountry":[{"continent":"EU","countryName":"United Kingdom","countryCode":"UK"},{"continent":"Asia","countryName":"China","countryCode":"CN"}]}}}
      === 2015-01-30 14:22:00,696 [2-31] INFO Compression - /customds/sc/HttpProxy: 217 -> 156 bytes


      Thanks and regards
      Kay Chee Cheung

      Comment


        #4
        Do not customize jsonRecordXPath. Just conform to the examples shown in the documentation, and then there is no need to set jsonRecordXPath.

        Comment

        Working...
        X