开发者

Consuming REST services with SmartGWT

While creating a simple client for a REST service which I have stubbed out, I noticed that smartGWT开发者_运维技巧's RestDataSource class is limited in the type of xml it can understand. All REST resources must respond with XML in the following format..

<response>
    <status>0</status>
    <startRow>0</startRow>
    <endRow>10</endRow>
    <totalRows>50</totalRows>
    <data>
        <record>
            <someField>value</someField>
            <someOtherField>value</someOtherField>
        </record>
        <record>
            <someField>value</someField>
            <someOtherField>value</someOtherField> 
        </record>
        ...
    </data>
</response>

.. where the only variant is the someField/someOtherField tags.

This structure, which is little more than name/value pairs, is not going to work for us.

I then saw this demo on the SmartGWT showcase...

http://www.smartclient.com/smartgwtee/showcase/#data_integration_server_rss

Which shows how to consume xml in an arbitrary format for display like so...

package com.smartgwt.sample.showcase.client.webservice;  

import com.smartgwt.client.data.DataSource;  
import com.smartgwt.client.data.fields.DataSourceTextField;  
import com.smartgwt.client.data.fields.DataSourceLinkField;  
import com.smartgwt.client.widgets.Canvas;  
import com.smartgwt.client.widgets.grid.ListGrid;  
import com.smartgwt.sample.showcase.client.PanelFactory;  
import com.smartgwt.sample.showcase.client.ShowcasePanel;  

public class RssSample implements EntryPoint {  

    public void onModuleLoad() {  
        DataSource dataSource = new DataSource();  
        dataSource.setDataURL("http://rss.slashdot.org/Slashdot/slashdot");  
        dataSource.setRecordXPath("//default:item");  

        DataSourceTextField titleField = new DataSourceTextField("title", "Title");  
        DataSourceLinkField linkField = new DataSourceLinkField("link", "Link");  

        dataSource.setFields(titleField, linkField);  

        ListGrid grid = new ListGrid();  
        grid.setAutoFetchData(true);  
        grid.setHeight(200);  
        grid.setWidth100();  
        grid.setDataSource(dataSource);  

        grid.draw();  
    }  

}  

This works well for GETs but how about PUTs, POSTs and DELETEs?

Can anyone share some code or point me to a resource which demonstrates how to do other RESTful operations from a SmartGWT client?

Thanks


Use OperationBindings:

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/DataSource.html#setOperationBindings%28com.smartgwt.client.data.OperationBinding...%29

You can control the url to contact, HTTP method, and many other things on a per-CRUD-operation basis.

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/DataSource.html#setDataURL(java.lang.String)

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/DataSource.html#setDataProtocol%28com.smartgwt.client.types.DSProtocol%29

Note that to use those particular HTTP verbs (PUT and DELETE), you'd use setRequestProperties and setHTTPMethod:

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/DataSource.html#setRequestProperties%28com.smartgwt.client.data.DSRequest%29

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/rpc/RPCRequest.html#setHttpMethod%28java.lang.String%29

But be aware, some older browsers do not support those verbs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜