我在URL:http://localhost:8888/rest/contacts上创建了一个REST API,输出如下:
{
"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)我试着在谷歌上搜索,想找到一个修复方法,但是没有帮助。如果有人知道这个问题的解决方案,请让我知道。
发布于 2013-02-11 23:24:44
您只需更改数据url,而不是使用绝对路径,而是使用相对路径。
变化
dataSource.setDataURL("http://localhost:8888/rest/contacts");至
dataSource.setDataURL("rest/contacts");当您必须将其部署到远程服务器时,这也是必要的。
我建议您使用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();
}您可以将每个OperationBinding映射到每个http方法(GET、POST、PUT、DELETE)或任何适合您的REST API的方法。
干杯!
发布于 2015-03-04 01:33:33
您可以在要映射的字段中直接输入Xpath
RecordXPath可以直接在DataSource上指定,用于只能执行"fetch“操作的简单只读 DataSource。
field.setRecordXPath("contact")https://stackoverflow.com/questions/14644283
复制相似问题