问题
目前,由于GSON的各种烦恼,我正在用Resty-GWT替换GSON以进行JSON-RPC调用。它完全按照我想要的方式工作,但我无法弄清楚如何发送我的消息,而不是在String
:
String names_ = "{"jsonrpc":"2.0","method":"RegisterValues","params":[["FirstValue"],"id":2}";
我想以更聪明的方式做到这一点,所以我不必写出所有这些值。最重要的是,我想要一种简单的方法来插入这些参数。某种方法可以轻松地在我的请求有效负载中声明这些属性。
当前方法
此String
通过以下调用发送:
testService.RegisterValues(names_, new MethodCallback<testService.Response>(){
@Override
public void onFailure(Method method, Throwable exception) {
// TODO Auto-generated method stub
Window.alert(exception.getMessage().toString());
}
@Override
public void onSuccess(Method method, testService.Response response) {
// TODO Auto-generated method stub
Window.alert("Hello" + response.getResult().toString());
}
});
testService
类可在此处找到:
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;
public interface testService extends RestService {
@Produces("application/json")
@POST
public void RegisterValues(String names_, MethodCallback<Response> callback );
}
然后将回调发送到此响应类,在那里我可以轻松地反序列化数据,提取(尽管这在这里并不重要):
public class Response {
private int id;
private Map<String, Map<String, Integer>> result;
private String error;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//...etc.
首先,您的 json 似乎不对,您有一个开放的方括号 ([) 未关闭?
然后,您必须执行与对象响应完全相同的操作。您需要创建一个表示您的names_ Resty 将为您序列化它的对象。
类似的东西
public class Params {
String jsonrpc;
String method;
String[] params;
String id;
}