开发者

GWT: How can I use JsonpRequestBuilder to handle a Json response of a list

开发者 https://www.devze.com 2022-12-22 18:28 出处:网络
My backend s开发者_运维技巧erver function returns a list of json object to the caller. I would like to use JsonRequestBuilder to interact with this backend function

My backend s开发者_运维技巧erver function returns a list of json object to the caller.

I would like to use JsonRequestBuilder to interact with this backend function

I defined a AsyncCallback this way

class MyCallBack extends AsyncCallback<List<MyObject>> {

However, JsonpRequestBuilder does not this declaration AsyncCallback because the generic type is bounded to <T extends JavaScriptObject>. List<MyObject> does not satisfy this requirement.

Do you have any suggestion to this problem?


See this example from the docs for JsonpRequestBuilder

class Feed extends JavaScriptObject {
  protected Feed() {}

  public final native JsArray<Entry> getEntries() /*-{
    return this.feed.entry;
  }-*/;
}

Instead of the response being a straight List, the response is a JavaScriptObject that contains a JS array, which is exposed via the JSNI getEntries() method.

If the JSON response doesn't name the array (like var feed = [...]) then I believe you can just do return this but you'd have to try it to be sure. Hope this helped.


Using JsonpRequestBuilder is easy after you a Java JSON parser to do the server job. Most mistakes come to the way that server response must de done.

Basically you have to code a javascript function call as response.

Code to run on client

// invoke a JSON RPC call
//DO NOT FORGET TO CONFIGURE SERVLET ENDPOINT IN web.xml
new JsonpRequestBuilder().requestObject(
       GWT.getModuleBaseURL() + "JsonRecordService?service=list"
      ,new AsyncCallback<JavaScriptObject>()
          {
            @Override
            public void onFailure( Throwable caught )
            {
              Window.alert( caught.toString() );
            }
            @Override
            public void onSuccess( JavaScriptObject result )
            { // you can use generics too send collections of objects
              Window.alert( "JSON Data available." );
            }

          }
        );

I used the gjon class on server side todo the dirty job of serialization. Now the server side code:

public class JsonRecordServiceImpl extends HttpServlet
{
  @Override
  public void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException
  {
    try {
      String serviceName = req.getParameter( "service" );
      if ( serviceName == null )
      {
        //let request die by timeout, maybe we could inspect for a failure callback param
        return;
      }
      //if this endpoint answer more then one service need map to a method name
      //may wish use Reflection to map to a method name
      if ( serviceName.equals( "list" ) ) //anwser to a list call
      {
        Gson g = new Gson();
        // serialize it with GSONParser
        //resp.setContentType( "text/javascript" );
        //resp.setCharacterEncoding( "UTF-8" );
        serviceName = req.getParameter( "callback" );
        if ( serviceName != null ) resp.getWriter().write( serviceName + "(" );
        resp.getWriter().write( g.toJson( Arrays.asList( "A", "B" ), new TypeToken<List<String>>(){}.getType() ) );
        if ( serviceName != null ) resp.getWriter().write( ");" );
        return;
      }
    }
    catch ( IOException e ) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
}
0

精彩评论

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

关注公众号