有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有没有更简单的方法用RestyGWT发布此消息?

问题

目前,由于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.

共 (1) 个答案

  1. # 1 楼答案

    首先,您的json似乎不正确您有一个开放的方括号([)没有关闭

    然后你必须做与你的对象响应完全相同的事情。您需要创建一个表示您的名字的对象,Resty将为您序列化它

    差不多

    public class Params {
      String jsonrpc;
      String method;
      String[] params;
      String id;
    }