gwtrequestfactoryjson-rpc

How to call JSON-RPC services using RequestFactory


Or what is the best way to archive this on GWT.

Please post some code of how to call a simple service with parameters using RequestFactory.

UPDATE:

Thomas, i have updated my code with your suggestions.

public interface MyRequestFactory extends RequestFactory {
    MyRequestFactory INSTANCE = GWT.create(MyRequestFactory.class);

    MyRequestContext myRequestContest();
}

@JsonRpcService
public interface MyRequestContext extends RequestContext {

    UserFullFormattedName userFullFormattedName();

    @JsonRpcWireName(value = "GetUserFullFormattedName")
    public interface UserFullFormattedName extends Request<String> {
    }
}

public static EventBus EVENT_BUS = GWT.create(SimpleEventBus.class);

public void onModuleLoad() {

    DefaultRequestTransport requestTransport = new DefaultRequestTransport();
    requestTransport.setRequestUrl("../services/service.ashx");

    MyRequestFactory.INSTANCE.initialize(EVENT_BUS, requestTransport);

    MyRequestFactory.INSTANCE.myRequestContext().userFullFormattedName().fire(new Receiver<String>() {

        @Override
        public void onSuccess(String response) {
            System.out.println(response);

        }
    });

This code fails with:

11:37:49.722 [ERROR] [modules] Uncaught exception escaped
java.lang.AssertionError: java.lang.String is not an EntityProxy type
    at com.google.web.bindery.requestfactory.shared.impl.IdFactory.asEntityProxy(IdFactory.java:66)
    at com.google.web.bindery.requestfactory.shared.impl.IdFactory.createId(IdFactory.java:229)
    at com.google.web.bindery.requestfactory.shared.impl.IdFactory.allocateId(IdFactory.java:41)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$JsonRpcPayloadDialect.processPayload(AbstractRequestContext.java:251)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$5.onTransportSuccess(AbstractRequestContext.java:1108)
    at com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport$1.onResponseReceived(DefaultRequestTransport.java:136)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Unknown Source)

Solution

  • Cat should be a ValueProxy annotated with @JsonRpcProxy:

    @JsonRpcProxy
    interface Cat extends ValueProxy {
       String getName();
       String getId();
    }
    

    Also, I'm not sure the @JsonRpcWireName is required, as its value is the same as the method name.