SlideShare a Scribd company logo
Easy REST APIs with
Jersey and RestyGWT
David Chandler
turbomanage.wordpress.com
why RestyGWT?
Ease of GWT-RPC
Power of Command pattern
Less boilerplate
Easier testing
get started
pom.xml
<dependency>

<groupId>org.fusesource.restygwt</groupId>

<artifactId>restygwt</artifactId>

<version>1.4</version>

</dependency>

<dependency>

<groupId>javax.ws.rs</groupId>

<artifactId>jsr311-api</artifactId>

<version>1.1.1</version>

</dependency>

*.gwt.xml
<inherits name="org.fusesource.restygwt.RestyGWT"/>
https://resty-gwt.github.io/
map JSON
public class YqlResponse {

public YqlQuery query;



public static class YqlQuery {

public YqlResults results;



public static class YqlResults {

public List<Quote> quote;

}

}

}

no annotations needed
create a service API
@Path("http://query.yahooapis.com/v1/public")

public interface QuoteService extends RestService {

@GET

@Path("yql")

public void get(

@QueryParam("q") String query,

@QueryParam("env") String env,

@QueryParam("format") String format,

MethodCallback<YqlResponse> callback);

}

invoke the service
private static final QuoteService svc = GWT.create(QuoteService.class);



@Override

public void load(LoaderDemo.QuoteRequest input, final Callback<ListLoadResult<Quote>,
Throwable> callback) {

svc.get(input.getYql(), ENV, FORMAT, new MethodCallback<YqlResponse>() {

@Override

public void onFailure(Method method, Throwable throwable) {

Info.display("QuoteProxy", "failure");

callback.onFailure(throwable);

}



@Override

public void onSuccess(Method method, YqlResponse yqlResponse) {

List<Quote> quotes = yqlResponse.query.results.quote;

Info.display("QuoteProxy", "success");

callback.onSuccess(new ListLoadResultBean<Quote>(quotes));

}

});

}

simple CRUD API
public interface RestApi<T> extends RestService {



@GET

@Path("get")

public void get(@QueryParam("id")Long id, MethodCallback<T> callback);



@GET

@Path("all")

public void listAll(MethodCallback<ListResponse<T>> callback);



@POST

@Path("save")

public void save(T obj, MethodCallback<T> callback);



@POST

@Path("saveMany")

public void saveMany(List<T> obj, MethodCallback<Integer> callback);



@POST

@Path("delete")

public void delete(Long id, MethodCallback<Integer> callback);



}

not much here!
minimal boilerplate
@Path("/api/note")

public interface NoteItemRestService extends RestApi<Note> { }



private static final NoteItemRestService service = GWT.create(NoteItemRestService.class);



public void addNote(Display display, long listId, Note item)

{

NoteList noteList = App.getNoteListService().getNoteList(listId);

// All are 0-based for consistency with GWT constants

item.listId = listId;

service.save(item, new AppCallback<Note>(display) {

@Override

public void handleSuccess(Note result) {

App.getAppModel().getAllNotes().add(0, result);

App.getEventBus().fireEvent(new ShowMessageEvent("Note saved."));

App.getEventBus().fireEvent(new
NotesLoadedEvent(App.getAppModel().getAllNotes()));

App.getEventBus().fireEvent(new NoteAddedEvent(result));

}

});

}

public class ProjectEntryPoint implements EntryPoint {



private static DispatcherFactory dispatcherFactory = new DispatcherFactory();

private static FilterawareDispatcher dispatcher =
dispatcherFactory.cachingDispatcher();



@Override

public void onModuleLoad() {

// Configure RestyGWT

dispatcher.addFilter(new CORSFilter());

Defaults.setDispatcher(dispatcher);



LoaderDemo loaderDemo = new LoaderDemo();

RootPanel.get().add(loaderDemo);

}



}
ListAllNotesAction, ListAllNotesResult,

AddNoteAction, AddNoteResult, …
Command pattern
server side
@Path("api/note")

public class NoteDao extends RestServiceDao<Note>

{


private static final Logger LOG = Logger.getLogger(NoteDao.class.getName());



@Override

@Path("all")

@GET

public ListWrapper<Note> findAll() {

User user = AuthFilter.getUser();

List<Note> notes = this.listByOwner(user);

return new ListWrapper<Note>(notes);

}
. . .
}
Objectify + Jersey
@Produces(MediaType.APPLICATION_JSON)

public class RestServiceDao<T extends Owned> extends ObjectifyDao<T>

{



public T getForOwner() {

User user = AuthFilter.getUser();

T obj = null;

try {

obj = this.getByOwner(user);

return obj;

} catch (TooManyResultsException e) {

throw new WebApplicationException(e);

}

}



public ListWrapper<T> findAll() {

User user = AuthFilter.getUser();

List<T> userAll = this.listByOwner(user);

return new ListWrapper<T>(userAll);

}
. . .
}

getting Jersey
pom.xml
<dependency>

<groupId>org.glassfish.jersey.containers</groupId>

<!-- if your container implements Servlet API older than 3.0, 

use "jersey-container-servlet-core" -->

<artifactId>jersey-container-servlet-core</artifactId>

<version>2.7</version>

</dependency>

<!-- workaround for https://java.net/jira/browse/JERSEY-2459 -->

<dependency>

<groupId>org.glassfish.hk2</groupId>

<artifactId>hk2-api</artifactId>

<version>2.3.0-b09</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.jaxrs</groupId>

<artifactId>jackson-jaxrs-json-provider</artifactId>

<version>2.3.2</version>

</dependency>

Jersey config
web.xml
<servlet>

<servlet-name>jerseyServlet</servlet-name>

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>jersey.config.server.provider.packages</param-name>

<param-value>com.example.listmaker.server</param-value>

</init-param>

<init-param>

<!-- speed up initial Jersey loading by deactivating WADL -->

<param-name>jersey.config.server.wadl.disableWadl</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

<param-name>jersey.config.server.provider.classnames</param-name>

<param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>



<servlet-mapping>

<servlet-name>jerseyServlet</servlet-name>

<url-pattern>/listmaker/*</url-pattern>

</servlet-mapping>

what about auth?
AuthFilter.java
// if an API call, return JSON response

if (path.startsWith("/listmaker/api")) {

((HttpServletResponse) resp).setStatus(401);

resp.setContentType(MediaType.TEXT_PLAIN);

resp.getWriter().write("User must log in");

} else {

// otherwise redirect

httpRes.sendRedirect(LOGIN_FORM);

}
<filter>

<filter-name>AuthFilter</filter-name>

<filter-class>com.turbomanage.gwt.server.servlet.AuthFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>AuthFilter</filter-name>

<url-pattern>/listmaker/*</url-pattern>

</filter-mapping>

exception handling
@Override

public void onFailure(Method method, Throwable throwable) {

String url = method.builder.getUrl();



App.getLogger().log(Level.SEVERE, "Error calling service " + url, throwable);

try {

// Decode the exception

if (throwable instanceof FailedStatusCodeException) {

FailedStatusCodeException sce = (FailedStatusCodeException) throwable;

App.getLogger().log(Level.SEVERE, "Service returned " + sce.getStatusCode() +
sce.getMessage());

if (401 == sce.getStatusCode()) {

Window.Location.replace(LOGIN_FORM);

} else if (500 == sce.getStatusCode()) {

if ("UserNotRegisteredException".equals(sce.getMessage())) {

Window.Location.replace(SIGNUP_URL);

}

}

}

handleFailure(throwable);

} finally {

reset(null);

}

}

application error handling
service.save(item, new AppCallback<Note>(display) {

@Override

public void handleSuccess(Note result) {

. . .
}

});
public abstract class AppCallback<R> implements MethodCallback<R> {



private final Display display;



public AppCallback() {

this.display = null;

}



public AppCallback(Display display) {

this.display = display;

display.startProcessing();

}
. . .
}
finer points
Text, JSON, XML via direct API — res.get()…
CachingRetryingDispatcher
ModelChangeEvent
Objects with final fields (@JsonCreator)
Polymorphism (@JsonSubTypes)
same obj on client / server?
older versions of RestyGWT used Jackson 1.7
RestyGWT 2.0 uses Jackson 2.3
so you can now use the same annotations on client
and server
and therefore the same POJOs (yeah)!
use @JsonIgnore for server-only fields (like Ref)
Please rate this session at
gwtcreate.com/agenda
src
github.com/turbomanage/listmaker
David Chandler
turbomanage.wordpress.com
resty-gwt.github.io

More Related Content

What's hot

Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017
Andres Almiray
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code Effectively
Andres Almiray
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJava
Fabio Collini
 
Finding Clojure
Finding ClojureFinding Clojure
Finding Clojure
Matthew McCullough
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
Tiago de Freitas Lima
 
Architecture Components
Architecture ComponentsArchitecture Components
Architecture Components
Sang Eel Kim
 
The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210
Mahmoud Samir Fayed
 
yagdao-0.3.1 JPA guide
yagdao-0.3.1 JPA guideyagdao-0.3.1 JPA guide
yagdao-0.3.1 JPA guide
Mert Can Akkan
 
The uniform interface is 42
The uniform interface is 42The uniform interface is 42
The uniform interface is 42
Yevhen Bobrov
 
What's new in Android O
What's new in Android OWhat's new in Android O
What's new in Android O
Kirill Rozov
 
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Ryosuke Uchitate
 
The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184
Mahmoud Samir Fayed
 
Spring & Hibernate
Spring & HibernateSpring & Hibernate
Spring & Hibernate
Jiayun Zhou
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
Mike Nakhimovich
 
Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2
Fabio Collini
 
React lecture
React lectureReact lecture
React lecture
Christoffer Noring
 
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai UniversityT.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
Niraj Bharambe
 

What's hot (20)

Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code Effectively
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJava
 
Finding Clojure
Finding ClojureFinding Clojure
Finding Clojure
 
Unit testing with mock libs
Unit testing with mock libsUnit testing with mock libs
Unit testing with mock libs
 
XTW_Import
XTW_ImportXTW_Import
XTW_Import
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
 
Architecture Components
Architecture ComponentsArchitecture Components
Architecture Components
 
The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210
 
yagdao-0.3.1 JPA guide
yagdao-0.3.1 JPA guideyagdao-0.3.1 JPA guide
yagdao-0.3.1 JPA guide
 
The uniform interface is 42
The uniform interface is 42The uniform interface is 42
The uniform interface is 42
 
What's new in Android O
What's new in Android OWhat's new in Android O
What's new in Android O
 
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
 
The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184
 
Spring & Hibernate
Spring & HibernateSpring & Hibernate
Spring & Hibernate
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
 
Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2
 
React lecture
React lectureReact lecture
React lecture
 
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai UniversityT.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
 

Viewers also liked

GWT integration with Vaadin
GWT integration with VaadinGWT integration with Vaadin
GWT integration with Vaadin
Peter Lehto
 
RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RS
Neil Ghosh
 
JSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web ServicesJSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web Services
Daniel Cunha
 
Real world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsReal world RESTful service development problems and solutions
Real world RESTful service development problems and solutions
Bhakti Mehta
 
Artsofte облегченная веб шина данных для организации дистанционных каналов пр...
Artsofte облегченная веб шина данных для организации дистанционных каналов пр...Artsofte облегченная веб шина данных для организации дистанционных каналов пр...
Artsofte облегченная веб шина данных для организации дистанционных каналов пр...
Artsofte IT company
 
figo at FinTech Startups MeetUp in Hamburg
figo at FinTech Startups MeetUp in Hamburgfigo at FinTech Startups MeetUp in Hamburg
figo at FinTech Startups MeetUp in Hamburg
Lars Markull
 
JAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web ServicesJAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web Services
Arun Gupta
 
Управление идентификационными данными и доступом
Управление идентификационными данными и доступомУправление идентификационными данными и доступом
Управление идентификационными данными и доступом
КРОК
 
Необходимые условия качества данных: MDM, Шина, Хранилище данных
Необходимые условия качества данных: MDM, Шина, Хранилище данныхНеобходимые условия качества данных: MDM, Шина, Хранилище данных
Необходимые условия качества данных: MDM, Шина, Хранилище данных
КРОК
 
Интеграция данных и приложений: основа для единой ИТ-инфраструктуры
Интеграция данных и приложений: основа для единой ИТ-инфраструктурыИнтеграция данных и приложений: основа для единой ИТ-инфраструктуры
Интеграция данных и приложений: основа для единой ИТ-инфраструктуры
КРОК
 
Open Source Integration with WSO2 Enterprise Service Bus
Open Source Integration  with  WSO2 Enterprise Service BusOpen Source Integration  with  WSO2 Enterprise Service Bus
Open Source Integration with WSO2 Enterprise Service Bus
sumedha.r
 
Oracle Service Bus vs. Oracle Enterprise Service Bus vs. BPEL
Oracle Service Bus vs. Oracle Enterprise Service Bus vs. BPELOracle Service Bus vs. Oracle Enterprise Service Bus vs. BPEL
Oracle Service Bus vs. Oracle Enterprise Service Bus vs. BPELGuido Schmutz
 
Enterprise service bus(esb)
Enterprise service bus(esb)Enterprise service bus(esb)
Enterprise service bus(esb)prksh89
 
Produktivität - 5 Tricks, um in weniger Zeit mehr zu erledigen
Produktivität - 5 Tricks, um in weniger Zeit mehr zu erledigenProduktivität - 5 Tricks, um in weniger Zeit mehr zu erledigen
Produktivität - 5 Tricks, um in weniger Zeit mehr zu erledigen
FastBill
 
Microservices = Death of the Enterprise Service Bus (ESB)?
Microservices = Death of the Enterprise Service Bus (ESB)?Microservices = Death of the Enterprise Service Bus (ESB)?
Microservices = Death of the Enterprise Service Bus (ESB)?
Kai Wähner
 
Become a Presentation Hero: 21 tips
Become a Presentation Hero: 21 tipsBecome a Presentation Hero: 21 tips
Become a Presentation Hero: 21 tipsTom De Ruyck
 

Viewers also liked (16)

GWT integration with Vaadin
GWT integration with VaadinGWT integration with Vaadin
GWT integration with Vaadin
 
RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RS
 
JSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web ServicesJSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web Services
 
Real world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsReal world RESTful service development problems and solutions
Real world RESTful service development problems and solutions
 
Artsofte облегченная веб шина данных для организации дистанционных каналов пр...
Artsofte облегченная веб шина данных для организации дистанционных каналов пр...Artsofte облегченная веб шина данных для организации дистанционных каналов пр...
Artsofte облегченная веб шина данных для организации дистанционных каналов пр...
 
figo at FinTech Startups MeetUp in Hamburg
figo at FinTech Startups MeetUp in Hamburgfigo at FinTech Startups MeetUp in Hamburg
figo at FinTech Startups MeetUp in Hamburg
 
JAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web ServicesJAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web Services
 
Управление идентификационными данными и доступом
Управление идентификационными данными и доступомУправление идентификационными данными и доступом
Управление идентификационными данными и доступом
 
Необходимые условия качества данных: MDM, Шина, Хранилище данных
Необходимые условия качества данных: MDM, Шина, Хранилище данныхНеобходимые условия качества данных: MDM, Шина, Хранилище данных
Необходимые условия качества данных: MDM, Шина, Хранилище данных
 
Интеграция данных и приложений: основа для единой ИТ-инфраструктуры
Интеграция данных и приложений: основа для единой ИТ-инфраструктурыИнтеграция данных и приложений: основа для единой ИТ-инфраструктуры
Интеграция данных и приложений: основа для единой ИТ-инфраструктуры
 
Open Source Integration with WSO2 Enterprise Service Bus
Open Source Integration  with  WSO2 Enterprise Service BusOpen Source Integration  with  WSO2 Enterprise Service Bus
Open Source Integration with WSO2 Enterprise Service Bus
 
Oracle Service Bus vs. Oracle Enterprise Service Bus vs. BPEL
Oracle Service Bus vs. Oracle Enterprise Service Bus vs. BPELOracle Service Bus vs. Oracle Enterprise Service Bus vs. BPEL
Oracle Service Bus vs. Oracle Enterprise Service Bus vs. BPEL
 
Enterprise service bus(esb)
Enterprise service bus(esb)Enterprise service bus(esb)
Enterprise service bus(esb)
 
Produktivität - 5 Tricks, um in weniger Zeit mehr zu erledigen
Produktivität - 5 Tricks, um in weniger Zeit mehr zu erledigenProduktivität - 5 Tricks, um in weniger Zeit mehr zu erledigen
Produktivität - 5 Tricks, um in weniger Zeit mehr zu erledigen
 
Microservices = Death of the Enterprise Service Bus (ESB)?
Microservices = Death of the Enterprise Service Bus (ESB)?Microservices = Death of the Enterprise Service Bus (ESB)?
Microservices = Death of the Enterprise Service Bus (ESB)?
 
Become a Presentation Hero: 21 tips
Become a Presentation Hero: 21 tipsBecome a Presentation Hero: 21 tips
Become a Presentation Hero: 21 tips
 

Similar to Easy REST APIs with Jersey and RestyGWT

比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
javatwo2011
 
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy ClarksonMulti Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Joshua Long
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, Macoscope
Macoscope
 
Spring into rails
Spring into railsSpring into rails
Spring into rails
Hiro Asari
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss
Andres Almiray
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
Ignacio Coloma
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web APIhabib_786
 
Native REST Web Services with Oracle 11g
Native REST Web Services with Oracle 11gNative REST Web Services with Oracle 11g
Native REST Web Services with Oracle 11g
Marcelo Ochoa
 
OSGi ecosystems compared on Apache Karaf - Christian Schneider
OSGi ecosystems compared on Apache Karaf - Christian SchneiderOSGi ecosystems compared on Apache Karaf - Christian Schneider
OSGi ecosystems compared on Apache Karaf - Christian Schneider
mfrancis
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Alena Holligan
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureC.T.Co
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureAlexey Buzdin
 
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R AugeHTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
mfrancis
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
nbuddharaju
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 
Cloud nativeworkshop
Cloud nativeworkshopCloud nativeworkshop
Cloud nativeworkshop
Emily Jiang
 
Testing in android
Testing in androidTesting in android
Testing in android
jtrindade
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
JBug Italy
 

Similar to Easy REST APIs with Jersey and RestyGWT (20)

比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy ClarksonMulti Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, Macoscope
 
Brief Intro To Jax Rs
Brief Intro To Jax RsBrief Intro To Jax Rs
Brief Intro To Jax Rs
 
Spring into rails
Spring into railsSpring into rails
Spring into rails
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
 
Native REST Web Services with Oracle 11g
Native REST Web Services with Oracle 11gNative REST Web Services with Oracle 11g
Native REST Web Services with Oracle 11g
 
OSGi ecosystems compared on Apache Karaf - Christian Schneider
OSGi ecosystems compared on Apache Karaf - Christian SchneiderOSGi ecosystems compared on Apache Karaf - Christian Schneider
OSGi ecosystems compared on Apache Karaf - Christian Schneider
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R AugeHTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
HTTP Whiteboard - OSGI Compendium 6.0 - How web apps should have been! - R Auge
 
Servlets
ServletsServlets
Servlets
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Cloud nativeworkshop
Cloud nativeworkshopCloud nativeworkshop
Cloud nativeworkshop
 
Testing in android
Testing in androidTesting in android
Testing in android
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 

More from David Chandler

Taking Your GWT App to Tablets with GXT 4.0
Taking Your GWT App to Tablets with GXT 4.0Taking Your GWT App to Tablets with GXT 4.0
Taking Your GWT App to Tablets with GXT 4.0
David Chandler
 
StORM: a lightweight ORM for Android SQLite
StORM: a lightweight ORM for Android SQLiteStORM: a lightweight ORM for Android SQLite
StORM: a lightweight ORM for Android SQLite
David Chandler
 
Cómo trabajan los Googlers
Cómo trabajan los GooglersCómo trabajan los Googlers
Cómo trabajan los Googlers
David Chandler
 
Life of an engineer
Life of an engineerLife of an engineer
Life of an engineer
David Chandler
 
StORM preview
StORM previewStORM preview
StORM preview
David Chandler
 
Google App Engine Update 2012
Google App Engine Update 2012Google App Engine Update 2012
Google App Engine Update 2012
David Chandler
 
GWT Plus HTML 5
GWT Plus HTML 5GWT Plus HTML 5
GWT Plus HTML 5
David Chandler
 
Scalable Apps with Google App Engine
Scalable Apps with Google App EngineScalable Apps with Google App Engine
Scalable Apps with Google App Engine
David Chandler
 
What's New in GWT 2.2
What's New in GWT 2.2What's New in GWT 2.2
What's New in GWT 2.2
David Chandler
 
Develop and Deploy Scalable Apps with Google App Engine
Develop and Deploy Scalable Apps with Google App EngineDevelop and Deploy Scalable Apps with Google App Engine
Develop and Deploy Scalable Apps with Google App EngineDavid Chandler
 
Secrets of the GWT
Secrets of the GWTSecrets of the GWT
Secrets of the GWT
David Chandler
 
The 90-Day Startup with Google AppEngine for Java
The 90-Day Startup with Google AppEngine for JavaThe 90-Day Startup with Google AppEngine for Java
The 90-Day Startup with Google AppEngine for Java
David Chandler
 
GWT MVP Case Study
GWT MVP Case StudyGWT MVP Case Study
GWT MVP Case Study
David Chandler
 
Securing JSF Applications Against the OWASP Top Ten
Securing JSF Applications Against the OWASP Top TenSecuring JSF Applications Against the OWASP Top Ten
Securing JSF Applications Against the OWASP Top Ten
David Chandler
 

More from David Chandler (14)

Taking Your GWT App to Tablets with GXT 4.0
Taking Your GWT App to Tablets with GXT 4.0Taking Your GWT App to Tablets with GXT 4.0
Taking Your GWT App to Tablets with GXT 4.0
 
StORM: a lightweight ORM for Android SQLite
StORM: a lightweight ORM for Android SQLiteStORM: a lightweight ORM for Android SQLite
StORM: a lightweight ORM for Android SQLite
 
Cómo trabajan los Googlers
Cómo trabajan los GooglersCómo trabajan los Googlers
Cómo trabajan los Googlers
 
Life of an engineer
Life of an engineerLife of an engineer
Life of an engineer
 
StORM preview
StORM previewStORM preview
StORM preview
 
Google App Engine Update 2012
Google App Engine Update 2012Google App Engine Update 2012
Google App Engine Update 2012
 
GWT Plus HTML 5
GWT Plus HTML 5GWT Plus HTML 5
GWT Plus HTML 5
 
Scalable Apps with Google App Engine
Scalable Apps with Google App EngineScalable Apps with Google App Engine
Scalable Apps with Google App Engine
 
What's New in GWT 2.2
What's New in GWT 2.2What's New in GWT 2.2
What's New in GWT 2.2
 
Develop and Deploy Scalable Apps with Google App Engine
Develop and Deploy Scalable Apps with Google App EngineDevelop and Deploy Scalable Apps with Google App Engine
Develop and Deploy Scalable Apps with Google App Engine
 
Secrets of the GWT
Secrets of the GWTSecrets of the GWT
Secrets of the GWT
 
The 90-Day Startup with Google AppEngine for Java
The 90-Day Startup with Google AppEngine for JavaThe 90-Day Startup with Google AppEngine for Java
The 90-Day Startup with Google AppEngine for Java
 
GWT MVP Case Study
GWT MVP Case StudyGWT MVP Case Study
GWT MVP Case Study
 
Securing JSF Applications Against the OWASP Top Ten
Securing JSF Applications Against the OWASP Top TenSecuring JSF Applications Against the OWASP Top Ten
Securing JSF Applications Against the OWASP Top Ten
 

Recently uploaded

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 

Recently uploaded (20)

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 

Easy REST APIs with Jersey and RestyGWT

  • 1. Easy REST APIs with Jersey and RestyGWT David Chandler turbomanage.wordpress.com
  • 2. why RestyGWT? Ease of GWT-RPC Power of Command pattern Less boilerplate Easier testing
  • 4. map JSON public class YqlResponse {
 public YqlQuery query;
 
 public static class YqlQuery {
 public YqlResults results;
 
 public static class YqlResults {
 public List<Quote> quote;
 }
 }
 }
 no annotations needed
  • 5. create a service API @Path("http://query.yahooapis.com/v1/public")
 public interface QuoteService extends RestService {
 @GET
 @Path("yql")
 public void get(
 @QueryParam("q") String query,
 @QueryParam("env") String env,
 @QueryParam("format") String format,
 MethodCallback<YqlResponse> callback);
 }

  • 6. invoke the service private static final QuoteService svc = GWT.create(QuoteService.class);
 
 @Override
 public void load(LoaderDemo.QuoteRequest input, final Callback<ListLoadResult<Quote>, Throwable> callback) {
 svc.get(input.getYql(), ENV, FORMAT, new MethodCallback<YqlResponse>() {
 @Override
 public void onFailure(Method method, Throwable throwable) {
 Info.display("QuoteProxy", "failure");
 callback.onFailure(throwable);
 }
 
 @Override
 public void onSuccess(Method method, YqlResponse yqlResponse) {
 List<Quote> quotes = yqlResponse.query.results.quote;
 Info.display("QuoteProxy", "success");
 callback.onSuccess(new ListLoadResultBean<Quote>(quotes));
 }
 });
 }

  • 7. simple CRUD API public interface RestApi<T> extends RestService {
 
 @GET
 @Path("get")
 public void get(@QueryParam("id")Long id, MethodCallback<T> callback);
 
 @GET
 @Path("all")
 public void listAll(MethodCallback<ListResponse<T>> callback);
 
 @POST
 @Path("save")
 public void save(T obj, MethodCallback<T> callback);
 
 @POST
 @Path("saveMany")
 public void saveMany(List<T> obj, MethodCallback<Integer> callback);
 
 @POST
 @Path("delete")
 public void delete(Long id, MethodCallback<Integer> callback);
 
 }

  • 8. not much here! minimal boilerplate @Path("/api/note")
 public interface NoteItemRestService extends RestApi<Note> { }
 
 private static final NoteItemRestService service = GWT.create(NoteItemRestService.class);
 
 public void addNote(Display display, long listId, Note item)
 {
 NoteList noteList = App.getNoteListService().getNoteList(listId);
 // All are 0-based for consistency with GWT constants
 item.listId = listId;
 service.save(item, new AppCallback<Note>(display) {
 @Override
 public void handleSuccess(Note result) {
 App.getAppModel().getAllNotes().add(0, result);
 App.getEventBus().fireEvent(new ShowMessageEvent("Note saved."));
 App.getEventBus().fireEvent(new NotesLoadedEvent(App.getAppModel().getAllNotes()));
 App.getEventBus().fireEvent(new NoteAddedEvent(result));
 }
 });
 }

  • 9. public class ProjectEntryPoint implements EntryPoint {
 
 private static DispatcherFactory dispatcherFactory = new DispatcherFactory();
 private static FilterawareDispatcher dispatcher = dispatcherFactory.cachingDispatcher();
 
 @Override
 public void onModuleLoad() {
 // Configure RestyGWT
 dispatcher.addFilter(new CORSFilter());
 Defaults.setDispatcher(dispatcher);
 
 LoaderDemo loaderDemo = new LoaderDemo();
 RootPanel.get().add(loaderDemo);
 }
 
 } ListAllNotesAction, ListAllNotesResult,
 AddNoteAction, AddNoteResult, … Command pattern
  • 10. server side @Path("api/note")
 public class NoteDao extends RestServiceDao<Note>
 { 
 private static final Logger LOG = Logger.getLogger(NoteDao.class.getName());
 
 @Override
 @Path("all")
 @GET
 public ListWrapper<Note> findAll() {
 User user = AuthFilter.getUser();
 List<Note> notes = this.listByOwner(user);
 return new ListWrapper<Note>(notes);
 } . . . }
  • 11. Objectify + Jersey @Produces(MediaType.APPLICATION_JSON)
 public class RestServiceDao<T extends Owned> extends ObjectifyDao<T>
 {
 
 public T getForOwner() {
 User user = AuthFilter.getUser();
 T obj = null;
 try {
 obj = this.getByOwner(user);
 return obj;
 } catch (TooManyResultsException e) {
 throw new WebApplicationException(e);
 }
 }
 
 public ListWrapper<T> findAll() {
 User user = AuthFilter.getUser();
 List<T> userAll = this.listByOwner(user);
 return new ListWrapper<T>(userAll);
 } . . . }

  • 12. getting Jersey pom.xml <dependency>
 <groupId>org.glassfish.jersey.containers</groupId>
 <!-- if your container implements Servlet API older than 3.0, 
 use "jersey-container-servlet-core" -->
 <artifactId>jersey-container-servlet-core</artifactId>
 <version>2.7</version>
 </dependency>
 <!-- workaround for https://java.net/jira/browse/JERSEY-2459 -->
 <dependency>
 <groupId>org.glassfish.hk2</groupId>
 <artifactId>hk2-api</artifactId>
 <version>2.3.0-b09</version>
 </dependency>
 <dependency>
 <groupId>com.fasterxml.jackson.jaxrs</groupId>
 <artifactId>jackson-jaxrs-json-provider</artifactId>
 <version>2.3.2</version>
 </dependency>

  • 13. Jersey config web.xml <servlet>
 <servlet-name>jerseyServlet</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 <init-param>
 <param-name>jersey.config.server.provider.packages</param-name>
 <param-value>com.example.listmaker.server</param-value>
 </init-param>
 <init-param>
 <!-- speed up initial Jersey loading by deactivating WADL -->
 <param-name>jersey.config.server.wadl.disableWadl</param-name>
 <param-value>true</param-value>
 </init-param>
 <init-param>
 <param-name>jersey.config.server.provider.classnames</param-name>
 <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
 <servlet-name>jerseyServlet</servlet-name>
 <url-pattern>/listmaker/*</url-pattern>
 </servlet-mapping>

  • 14. what about auth? AuthFilter.java // if an API call, return JSON response
 if (path.startsWith("/listmaker/api")) {
 ((HttpServletResponse) resp).setStatus(401);
 resp.setContentType(MediaType.TEXT_PLAIN);
 resp.getWriter().write("User must log in");
 } else {
 // otherwise redirect
 httpRes.sendRedirect(LOGIN_FORM);
 } <filter>
 <filter-name>AuthFilter</filter-name>
 <filter-class>com.turbomanage.gwt.server.servlet.AuthFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>AuthFilter</filter-name>
 <url-pattern>/listmaker/*</url-pattern>
 </filter-mapping>

  • 15. exception handling @Override
 public void onFailure(Method method, Throwable throwable) {
 String url = method.builder.getUrl();
 
 App.getLogger().log(Level.SEVERE, "Error calling service " + url, throwable);
 try {
 // Decode the exception
 if (throwable instanceof FailedStatusCodeException) {
 FailedStatusCodeException sce = (FailedStatusCodeException) throwable;
 App.getLogger().log(Level.SEVERE, "Service returned " + sce.getStatusCode() + sce.getMessage());
 if (401 == sce.getStatusCode()) {
 Window.Location.replace(LOGIN_FORM);
 } else if (500 == sce.getStatusCode()) {
 if ("UserNotRegisteredException".equals(sce.getMessage())) {
 Window.Location.replace(SIGNUP_URL);
 }
 }
 }
 handleFailure(throwable);
 } finally {
 reset(null);
 }
 }

  • 16. application error handling service.save(item, new AppCallback<Note>(display) {
 @Override
 public void handleSuccess(Note result) {
 . . . }
 }); public abstract class AppCallback<R> implements MethodCallback<R> {
 
 private final Display display;
 
 public AppCallback() {
 this.display = null;
 }
 
 public AppCallback(Display display) {
 this.display = display;
 display.startProcessing();
 } . . . }
  • 17. finer points Text, JSON, XML via direct API — res.get()… CachingRetryingDispatcher ModelChangeEvent Objects with final fields (@JsonCreator) Polymorphism (@JsonSubTypes)
  • 18. same obj on client / server? older versions of RestyGWT used Jackson 1.7 RestyGWT 2.0 uses Jackson 2.3 so you can now use the same annotations on client and server and therefore the same POJOs (yeah)! use @JsonIgnore for server-only fields (like Ref)
  • 19. Please rate this session at gwtcreate.com/agenda src github.com/turbomanage/listmaker David Chandler turbomanage.wordpress.com resty-gwt.github.io