java - 按照这个简单的 GSON 示例,等效的客户端 RestyGWT 是什么样子?

标签 java gson json-rpc resty-gwt

问题

由于 GSON (GWT JSON-RPC) 遇到一些问题,我想切换到 Resty-GWT。以下示例展示了我的旧设置,然后下面是我的传输尝试。

数据

这是从我设置的代理发送的 JSON 数据:

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

ControlService.java - 如何进行调用(使用 GSON)

进行异步调用的类:

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

这是实际调用和接收数据的类:

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!" );
            }
    });
    }
}

那么我该怎么做 RestyGWT

我的尝试:

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
}

最佳答案

我认为您忘记了回调的实现(也许它在代码中的其他位置,在这种情况下,将其发布在您的问题中会很有用)。

MethodCallback是一个需要实现的接口(interface)(如果你愿意的话可以匿名)

所以你需要有类似的东西

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

    //onSuccess

    //onFailure
);

现在当你说

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

你的意思是这是你发布内容时服务器的答案还是你发布到服务器的内容?

在这两种情况下,该对象看起来都不像 String[]。在您的restService 中,您声明将 String[] 作为有效负载 Names 发送,并声明在响应返回时将检索 String[]。

您可以查看本教程以获得更多帮助:http://ronanquillevere.github.io/2014/03/16/gwt-rest-app.html

关于java - 按照这个简单的 GSON 示例,等效的客户端 RestyGWT 是什么样子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22763994/

相关文章:

java - Java 8 中是否有等效于 Scala 的 Either?

java - Java 堆栈跟踪中的 "eliminated"是什么意思?

java - 通过搜索类变量来删除 ArrayList 中的元素

java - $ 在 Java 类名中有什么影响

java - 推荐一个用于 java 和 javascript 的 JSON-RPC 库

java - 在 GWT 中将 Java 对象 (POJO) 编码为 JSON 字符串

java - 使用聚合对象时,是否使用自定义集合进行关联?

java - 为 Kotlin 创建 POJO 类

java - 当嵌套 JSON 中出现相同字段名称时,将 JSON 解析为 java 对象

python - 在服务器端为 'pyjamas' 使用哪个简单的基于 python 的 WSGI 兼容 jsonrpc 库?