javagwtjson-rpcresty-gwt

Is There an Easier Way to Post this Message with RestyGWT?


The Problem

Currently I'm replacing GSON with Resty-GWT to make my JSON-RPC calls due to various annoyances with GSON. It works exactly how I want it to but I can't figure out how to send my messages other than in a String:

String names_ = "{\"jsonrpc\":\"2.0\",\"method\":\"RegisterValues\",\"params\":[[\"FirstValue\"],\"id\":2}";

I'd like to do this in a smarter way, so I don't have to write out all these values. Most importantly though, I'd like an easy way to insert those params. Some way of just declaring these properties in my request payload with ease.

Current Method

This String is sent via this call:

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());


            }
        });

The testService class is found here:

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

The callback is then sent to this response class, where I can deserialise the data with ease, extracting (though this isn't really important here):

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.

Solution

  • First your json does not seems right you have an open square bracket ([) not closed ?

    Then you must do exactly the same thing than with your object Response. You need to create an object representing your names_ Resty will serialize it for you.

    something like

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