2013-02-01 49 views
0

我已創建一個URL REST API:http://localhost:8888/rest/contacts與此JSON輸出:SmartGWT的ListGrid JSON問題

{ 
    "contact": { 
     "address": [ 
      { 
       "city":"Shanghai", 
       "street":"Long Hua Street" 
      }, 
      { 
       "city":"Shanghai", 
       "street":"Dong Quan Street" 
      } 
     ], 
     "id": "huangyim", 
     "name": "Huang Yi Ming" 
    } 
} 

我想在SmartGWT的ListGrid打印值只有ID。

public class ExampleEntry implements EntryPoint { 
    #Override 
    public void onModuleLoad() { 
     DataSource dataSource = new DataSource(); 
     dataSource.setDataFormat(DSDataFormat.JSON); 
     dataSource.setDataURL("http://localhost:8888/rest/contacts"); 
     dataSource.setRecordXPath("/contact"); 

     DataSourceTextField field = new DataSourceTextField("id", "id"); 
     dataSource.addField(field);   

     final ListGrid grid = new ListGrid(); 
     grid.setDataSource(dataSource); 
     grid.setAutoFetchData(true); 
     grid.draw(); 
    } 
} 

但它拋出以下異常:

15:33:12.766 [ERROR] [jerseyexample] 15:33:12.747:XRP2:WARN:RPCManager:xmlHttpRequest.getAllResponseHeaders() returned null 
com.smartgwt.client.core.JsObject$SGWT_WARN: 15:33:12.747:XRP2:WARN:RPCManager:xmlHttpRequest.getAllResponseHeaders() returned null 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293) 
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547) 
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364) 
    at java.lang.Thread.run(Unknown Source) 

我試圖尋找谷歌找到一個修復,但沒有幫助。請讓我知道是否有人知道這個問題的解決方案。

回答

1

您只需更改數據url,而不是使用絕對路徑使用相對路徑。

變化

dataSource.setDataURL("http://localhost:8888/rest/contacts"); 

dataSource.setDataURL("rest/contacts"); 

這也是neccesary時,你必須將其部署到遠程服務器。

我建議您使用RestDataSource而不是普通的DataSource,它已被專門設計用於REST服務,正如您所說的那樣。

使用RestDataSource和使用DataSource一樣簡單。

@Override 
public void onModuleLoad() { 
    RestDataSource dataSource = new RestDataSource(); 
    dataSource.setDataFormat(DSDataFormat.JSON); 
    dataSource.setDataURL("rest/contacts"); 
    dataSource.setRecordXPath("/contact"); 

    DataSourceTextField field = new DataSourceTextField("id", "id"); 
    dataSource.addField(field);   

    OperationBinding get=new OperationBinding(); 
    get.setOperationType(DSOperationType.FETCH); 
    dataSource.setOperationBindings(get); 

    final ListGrid grid = new ListGrid(); 
    grid.setDataSource(dataSource); 
    grid.setAutoFetchData(true); 
    grid.draw(); 
} 

可以映射每個HTTP方法各OperationBinding(GET,POST PUT,DELETE)或任何適合您的REST API。

乾杯!

0

您可以直接把Xpath的領域要映射

RecordXPath可以對數據源直接指定了一個簡單的只讀數據源僅能夠「讀取」操作。

field.setRecordXPath("contact")