What Does the Equivalent Client Side RestyGWT Look Like Following This Simple GSON Example?

StackOverflow https://stackoverflow.com/questions/22763994

  •  24-06-2023
  •  | 
  •  

Domanda

The Problem

Due to several problems encountered with GSON (GWT JSON-RPC), I'd like to switch to Resty-GWT. The following example showcases my old setup, then below is my attempt at transferring.

The Data

This is the JSON data sent from the proxy I have setup:

{"id": 1, "result": ["Planets", "Stars"], "error": null}

ControlService.java - How I Make the Call (with GSON)

The class that makes the asynchronous call:

import com.google.gwtjsonrpc.common.AsyncCallback;
import com.google.gwtjsonrpc.common.RemoteJsonService;
import com.google.gwtjsonrpc.common.RpcImpl;

@RpcImpl(version=RpcImpl.Version.V2_0,transport=RpcImpl.Transport.HTTP_POST)
public interface ControlService extends RemoteJsonService
{

    public void connectedNames( String [] Names, AsyncCallback<String[]> callback ); //FirstExample

}

createPanel.java

This is the class that actually calls and receives the data:

import com.google.gwtjsonrpc.common.AsyncCallback;

public class createPanel implements ChangeHandler{

    public mainPanel(){

        //Some code setting up the panels
        service_ = GWT.create(ControlService.class);
        ((ServiceDefTarget) service_).setServiceEntryPoint("http://localhost:3900/services/ControlProxy.py"); //Directs GWT to the proxy

        service_.connectedNames( new String[0], new AsyncCallback<String[]>() {

            public void onSuccess( String[] result) 
            {   
                    //I now play with the data
            }
            public void onFailure(Throwable why)
            {
                myList_.addItem( "Server error!" );
            }
    });
    }
}

So How Do I Do This With RestyGWT?

My attempt:

testService.java

import javax.ws.rs.Path;

import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;

@Path("http://localhost:3900/services/ControlProxy.py")
@POST
public interface testService extends RestService {

        public void connectedNames( String [] Names, MethodCallback<String[]> callback );

}

testCreatePanel.java

public class createPanel implements ChangeHandler{

    public mainPanel(){

        //Some code setting up the panels
        service_ = GWT.create(testService.class);
        testService.connectedNames(cbcNames, callback);//how to extract data from this
}
È stato utile?

Soluzione

I think you forgot the implementation of your callback (maybe it is somewhere else in your code, in this case it would be usefull to post it in your question).

MethodCallback is an interface that needs to be implemented (anonymously if you want)

So you need to have something like

testService.connectedNames(cbcNames, new MethodCallback<String[]>(){

    //onSuccess

    //onFailure
);

Now when you say

This is the JSON data sent from the proxy I have setup: {"id": 1, "result": ["Planets", "Stars"], "error": null}

Do you mean it is the answer from the server when you post something or is it what you post to your server ?

In both cases this object does not look like String[]. In your restService you declared to send a String[] as a payload Names and you declared that you will retrieve a String[] when the response returns.

You can have a look to this tutorial for more help : http://ronanquillevere.github.io/2014/03/16/gwt-rest-app.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top