Pages

Sunday, November 08, 2009

Transferring JAVA objects over RPC in GWT

This is a very simple implementation of JAVA object transfer via RPC in GWT. You just have to write the JAVA class in the client side(Package) of your GWT project. And the class should implement IsSerializable interface. Let the JAVA class be like this.

public class Person implements IsSerializable{
    private String name;
    private String mobile;

    public Person() {

    }

    public Person(String name, String mobile) {
        this.name = name;
        this.mobile = mobile;
    }

   /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}


Now lets make the synchronous and asynchronous version of the RPC function

Synchronous:
public interface GWTService extends RemoteService{
    public Person getPerson();
}


Asynchronus:
public interface GWTServiceAsync {
    public void getPerson(AsyncCallback asyncCallback);
}


Implementation:
public class GWTServiceImpl extends RemoteServiceServlet implements
        GWTService {
    public Person getPerson()
    {
        return new Person("X""8801X11*******");
    }
}

And here is the main module of our GWT application.
public void onModuleLoad() {


        getService().getPerson(new AsyncCallback() {

            public void onFailure(Throwable caught) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            public void onSuccess(Object result) {
                Person p = (Person)result;
                Window.alert(p.getName() + " : " + p.getMobile());
            }
        });
    }

    public static GWTServiceAsync getService(){
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        GWTServiceAsync service = (GWTServiceAsync) GWT.create(GWTService.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "gwtservice";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }



So when ever you call this getPerson() function. You will get a person from the server. Now in the on success method cast your result to Person and get all the features of your Person class in client side. Asynchronous callbacks can be generic. In that case you can provide the Type Person in your Asynchronous Callback. And then you need not to cast.

No comments:

Post a Comment