3 Server RPC - Reference Documentation
Authors: Peter Ledbrook, David Dawson, Rob Purcell, Predrag Knežević
Version: 1.0.3
3 Server RPC
Introduction
Almost any non-trivial GWT application will require the use of its RPC support at some stage. GWT normally requires you to write a servlet for each service interface that you define, but the plugin simplifies this step by allowing you to implement the server-side RPC with normal Grails services. Any service can be configured for GWT by adding an expose property, as is done with the Remoting plugin and others:class MyService {
static expose = [ 'gwt:example.client' ]
List listUsers() {
…
}
…
}
grails generate-gwt-rpc [--force] [--non-interactive]
- src/java/example/client/MyService.java , and
src/java/example/client/MyServiceAsync.java
MyServiceAsync myService = (MyServiceAsync) GWT.create(MyService.class); ServiceDefTarget endpoint = (ServiceDefTarget) myService;// Note the URL where the RPC service is located! String moduleRelativeURL = GWT.getModuleBaseURL() + "rpc"; endpoint.setServiceEntryPoint(moduleRelativeURL);// Call a method on the service! List users = myService.listUsers() ...
Collection and map types
GWT supports both collections (list, sets, etc.) and maps as method arguments and return types. However, it does not support generics to determine what types are collection or map holds; instead, the user must add a custom javadoc annotation to the RPC interface definition, '@gwt.typeArgs'. The plugin simplifies this process slightly if you are running on a 1.5+ JVM by supplying two annotation classes: CollectionTypeArg and MapTypeArg . These can be used to specify both argument types and return types like so:import org.codehaus.groovy.grails.plugins.gwt.annotation.CollectionTypeArg import org.codehaus.groovy.grails.plugins.gwt.annotation.MapTypeArg class MyService { static expose = [ 'gwt:example.client' ] // The return list contains objects of type java.lang.String. @CollectionTypeArg(String) List listUsers() { … } // The 'arg' parameter is a map whose keys are java.lang.Integers, // and values are java.lang.Strings. void processMap(@MapTypeArg(key=Integer, value=String) Map arg) { … } … }
Including inherited modules
Create a sub-directory called "gwt" under the Grails application "lib". Drop your GWT module jar files in this directory and compile-gwt-modules will automatically pick them up and add them. See 5. ConfigurationNote for GWT 2+
If you are using gwt 2+, you can put the following to the service interface MyService.java:import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; @RemoteServiceRelativePath("rpc")
MyServiceAsync myService = (MyServiceAsync) GWT.create(MyService.class); ServiceDefTarget endpoint = (ServiceDefTarget) myService;// Note the URL where the RPC service is located! String moduleRelativeURL = GWT.getModuleBaseURL() + "rpc"; endpoint.setServiceEntryPoint(moduleRelativeURL);
MyServiceAsync myService = GWT.create(MyService.class);