Wednesday, July 31, 2013

GWT and Rest : Part 2 - Client (RestyGWT)

This article has been moved here

http://ronanquillevere.github.io/2013/07/03/gwt-rest-jersey-client.html





-- old version --







This article is part 2 of a previous post, see part1 http://wpamm.blogspot.fr/2013/03/gwt-and-rest-restygwt-jersey-part-1.html for the server side information. I know it has taken me a long time to write this part. I am really sorry if you were waiting.

This part will be focused on the GWT http://www.gwtproject.org/ client part, using RestyGWT http://restygwt.fusesource.org/index.html

This part will be really short because there is not much say as RestyGWT make things really easy ! By the way thanks for the people who have written and contributed to this library : chirino https://github.com/chirino and mkristian https://github.com/mkristian and all others.

The first thing you have to do is the inherits the resty stuff in your *.gwt.xml file

<inherits name='org.fusesource.restygwt.RestyGWT'/>

Thanks to this line, when extending the RestService Interface, the restyGWT generator will be able to generate the Java code corresponding to your interface.

Generators allow the GWT compiler to generate Java code at compile time and have it then be compiled along with the rest of the project into JavaScript. GWT generators behave a little bit like annotation preprocessors in Java.

To generate the client Java code, restyGWT is using the JAX-RS annotations to define the url of the request, the parameters to set etc. The code generated by RestyGWT will use com.google.gwt.http.client.Request class to query the server.

So all you have to do is extending the RestService interface and using the right JAX-RS annotations

The following code is an example taken from here : https://github.com/ronanquillevere/GWT-Resty-Template (code not really working right sorry need to fix it)

package rq.restygwt.template.client;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;

import rq.restygwt.template.shared.Person;

@Path("api/v1/persons")
public interface PersonService extends RestService
{
    @GET
    void getPersons(MethodCallback<List<Person>> callback);
 
    @GET
    @Path("{id}")
    void getPerson(@PathParam("id") String id, MethodCallback<Person> callback);
}
Now to use your service


package rq.restygwt.template.client;

import java.util.List;

import org.fusesource.restygwt.client.Defaults;
import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;

import rq.restygwt.template.shared.Person;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class Template implements EntryPoint
{

    public void onModuleLoad()
    {
        Defaults.setServiceRoot(GWT.getHostPageBaseURL()); // (avoid Template in the url)

        final HTML html = new HTML();
        RootPanel.get().add(html);

        // Uses resty generator (set up in gwt.xml)
        PersonService s = GWT.create(PersonService.class);

     
        s.getPersons(new MethodCallback<List<Person>>()
        {
            public void onSuccess(Method method, List<Person> response)
            {
                for (Person person : response)
                {
                    html.getElement().setInnerHTML(html.getElement().getInnerHTML() + "Hello " + person.getFirstName() + " " + person.getLastName() + "");
                }
            }

            public void onFailure(Method method, Throwable exception)
            {
                html.setText("Rest call failed");
            }
        });

        s.getPerson("1", new MethodCallback<Person>()
        {
            public void onSuccess(Method method, Person response)
            {
                html.getElement().setInnerHTML(html.getElement().getInnerHTML() + "The first one is " + response.getFirstName() + " " + response.getLastName() + "");

            }

            public void onFailure(Method method, Throwable exception)
            {
                html.setText("Rest call failed");
            }
        });
    }
}


Hope it helped !