2018

July

2017

September

May

2016

September

August

June

2015

September

August

June

January

2014

December

July

April

March

February

2013

December

August

July

March

2012

December

August

GWT and Rest : Part 2 - Client (RestyGWT) 03 Jul 2013

I have written a new article with all the needed info for Jersey 2.7, you probably should have a look here

This article is part 2 of a previous post, see part1 GWT and Rest : Part 1 - Server Side (Jersey JAX-RS) 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 client part, using RestyGWT

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 and 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 !

comments powered by Disqus
This site was designed using jekyll, bootstrap, fontawesome, google analytics, disqus and is hosted on github. All for free so thank you!