http://www.flatironssolutions.fr/ header_visibility: footer_visibility:
lang: en

GWT + Jersey, Great Middle Tier Communication

Calendar July 22, 2013 | User Curtis Fleming

Here at Flatirons we are constructing a sizable authoring environment for a client.  Content gets created in the browser and the application needs to communicate with the middle tier servers in numerous ways.  Our choice of GWT (Google Web Toolkit) + Jersey for client and server technologies allows us to write communication objects once and use them anywhere throughout the system.

gwt-box-150-GWT is both a client and server technology you write in Java, but client code gets compiled to JavaScript to run in the browser.  Between those two application parts you get object serialization for free.

Jersey (JAX-RS) is a Java framework for RESTful services.  Jersey offers convenient object serialization between servelet methods and its own API on the caller side.

 

Our architecture can be viewed like this:

gwt-curtis-blog

As it turns out, the two frameworks’ object serialization policies are compatible.  By meeting a few requirements, objects of a single class can freely move between the client and any of the servlets.

 

Benefits of GWT + Jersey:

  • Write a single data definition in Java
  • Use that definition on the browser, GWT server, and Jersey middleware
  • You don’t touch JSON, XML, or parsers when communicating with the server
  • No string manipulation
  • The serialization “Just Works” with minimal code
  • An exercise of this convenient pattern is to create a framework to report server errors

gwt-curtis-blog2-

                                                                  

Have your data class meet these requirements:

  • No references to classes that GWT cannot compile to JavaScript
  • No references to native JavaScript
  • Implement Serializable
  • A couple Jersey annotations to serialize the Object to XML via JAXB
  • An argument-less constructor with any access modifier
  • You are free to create an inheritance hierarchy, just apply the above to all classes

 

Example starting code

import java.io.Serializable;  
import javax.xml.bind.annotation.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) // Serialize every field  
public class MyRequest implements Serializable {  

     private MyRequest() {  
         // parameter-less constructor needed for GWT RPC serialization 
     }  
}

 

An Exercise – Send Error Traces to the browser

We have an inheritance of data objects with a central base class for responses from the servers.  In the base response you can store server-side exception traces to easily ferry them to the client.  In every Jersey servlet we return a subtype of BaseReponse and we wrap everything in the servlet’s methods in try-catch blocks.  This way, on exception we return a successful call, packaging a server status 500, a brief message of what we were attempting to do, and a stack trace.

import java.io.Serializable; 
import javax.xml.bind.annotation.*;  

@XmlRootElement  
@XmlAccessorType(XmlAccessType.FIELD)  
public class BaseResponse implements Serializable { 

    private int httpsStatus; 

    private String briefMessage;

    private String stackTrace; 

    public BaseResponse() { 
        // parameter-less constructor needed for GWT RPC serialization 
    } 
}

With the base class and a few convenience utilities, we are able to create additional server call objects without additional effort to send the stack trace back to the client.  As our multi-year project grows, this becomes one less concern to manage.

©2019 Flatirons Solutions, Inc.|All Rights Reserved|Privacy Policy
Follow Flatirons Solutions