Tuesday, July 17, 2012

GWT with JAX-RS (and JPA) Part 3

Come unto me all ye who are weary of heavy-laden clients and I will give you REST.

Continued from GWT with JAX-RS (aka RPC/REST) Part 2
OK, the cast of the technology mix is
  • JAX-RS (either Resteasy or Jersey on the server side)
  • JAX-RS + GWT = RestyGWT on the client-side
  • JPA on the server-side
  • JAXB over JAX-RS on both GWT client and server-side.
  • Jackson JSON processor on server-side.

I feel those acronyms are intimidating, but they are actually not difficult to implement. If you could understand the intricacies of GWT-RPC, you should have no problem understanding the soup mix above.

JPA remains to be the most difficult part of the mix to learn/master (most difficult but not difficult). Mastering GWT is actually the more difficult part than JPA. However, if you have no desire for your server to connect to a JDBC-enabled database, you do not need to concern with JPA.

This is your JAX-RS POJO:
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement (name="employee")
    public class Employee
    implements Serializable {
      @XmlAttribute
      private Long eid = null;
      @XmlAttribute
      private String name = null;
    }
This is the same POJO, if your app is obsessed with the order of appearance:
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement (name="employee")
    @XmlType(propOrder = {
      "eid",
      "name"
    }
    public class Employee implements Serializable {
      @XmlAttribute
      private Long eid = null;
      @XmlAttribute
      private String name = null;
    }
This is the POJO with public getters/setters (generated by Eclipse) so to enable RestyGWT to operate on it. Hoever, RestyGWT would not need the public getters/setters if the fields are public instead of private.
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement (name="employee")
    @XmlType(propOrder = {
      "eid",
      "name"
    }
    public class Employee
    implements Serializable {
      @XmlAttribute
      private Long eid = null;
      @XmlAttribute
      private String name = null;
    
      public Long getEid() {
        return eid;
      }
      public void setEid (Long  eid ) {
        this. eid = eid ;
      }
      public String getName () {
        return  name ;
      }
    
      public void setName (String  name ) {
        this. name = name ;
      }
    }
And finally, this is your Three-in-one POJO to accommodate JPA.
    @Entity
    @Table(name="Employee", schema="bombay")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement (name="employee")
    @XmlType(propOrder = {
      "eid",
      "name"
    }
    public class Employee
    implements Serializable {
      @XmlAttribute
      @Id
      private Long eid = null;
      @XmlAttribute
      private String name = null;
    
      public Long getEid() {
        return eid;
      }
      public void setEid (Long  eid ) {
        this. eid = eid ;
      }
      public String getName () {
        return  name ;
      }
      public void setName (String  name ) {
        this. name = name ;
      }
    }
Let's create another POJO:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement (name="employees")
public class EmployeeList {

  @XmlElement(name="enployee")
 private List<Employee> employees;
 
 public List<Employee> getEmployees() {
  if (employees==null)
   employees = new ArrayList<Employee>();
  
  return employees;
 }

 public void setEmployees(List<Employee> employees) {
  this.employees = employees;
 }
}
But, WHERE's THE JSON? JSON output will be borrowing the JAXB XML annotation. The following is the server side service API interface.
    @Path("/employee")
    @Produces({"application/xml", "application/json"})
    public interface EmployeeServiceAPI {
       /**
       * Find all employee ids with the given name
       */
      @GET
      @Path("/eids/name")
      LongList getEmployeeIdsWithName(@QueryParam ("name")String name) ;
      
      
      /**
       * Get employee wit Id
       */
      @GET
      @Path("/employee/{id}")
      Employee getEmployeeWithId(@PathParam ("eid")Long eid) ;
      
      /**
       * Get project list for an employee
       */
      @POST
      @Path("/projects/employee")
      ProjectList getProjects(Employee employee) ;
    }
And this is the async service API on the GWT Client.
    @Path("/employee")
    @Consumes({"application/json"}) // put in a http header to tell server to return me JSON
    public interface EmployeeServiceAPI {
      
      /**
       * Find all employee ids with the given name
       */
      @GET
      @Path("/eids/name")
      LongList getEmployeeIdsWithName(@QueryParam ("name")String name, MethodCallback  <LongList  > callback) ;
      
      /**
       * Get employee wit Id
       */
      @GET
      @Path("/employee/{id}")
      Employee getEmployeeWithId(@PathParam ("eid")Long eid, MethodCallback  <Employee  > callback) ;
      
      /**
       * Get project list for an employee
       */
      @POST
      @Path("/projects/employee")
      ProjectList getProjects(Employee employee, MethodCallback  <ProjectList  > callback) ;
    }
The server-side implementation to access the database.
    public class EmployeeServiceAPIImpl
    implements EmployeeServiceAPI {
      
      @Override
      public Employee getEmployeeIdsWithName(Long eid) {
        Employee emp = new Employee();
        
        EntityManager em = null;
        try {
          em = EMF.get().createEntityManager();
          Query q = em.createQuery(
            "SELECT n FROM Employee n WHERE n.status = 'active' AND n.eid = ?1"
          ).setParameter(1, eid);
    
          Employee emp = q.getSingleResult();
          return emp;
        }
        
        finally{
          if (em!=null)
            em.close();
        }
      }
    
      ......
    }
This is how your GWT client will call the async REST service:
  public class EmployeePresenter{

    static public EmployeeAsyncServiceAPI enpServiceAPI = GWT.create(EmployeeAsyncServiceAPI.class);
    
    public void search(Long eid) {
      enpServiceAPI.getEmployeeIdsWithName(eid, new EmployeeCallBack());
    }

    public class EmployeeCallBack
    implements MethodCallback{

      @Override
      public void onFailure(Method method, Throwable exception) {
        Window.alert("Error accessing data");
      }

      @Override
      public void onSuccess(Method method, Employee response) {
        EmployeePresenter.this.view.setEmployeeRecords(
          response.getEmployee());
        if (response.getEmployee().size()==0) {
          Window.alert("No records found");
        }
      }
    }
  }
Let's say function search(Long eid) got called as
  search(12529);
RestyGWT code generated by GWT.create() will send this request over the URL
/employee/employee/12529
RestyGWT would find the phrase in the async service API
  @Consumes({"application/json"})
and proceeds to place a header in the HTTP request
  Accepts: application/json
When the RestEasy Listener intercepts the request, it would search for the class implementing EmployeeServiceAPI and finds EmployeeServiceAPIImpl. RestEasy loads that class which proceeds to service the request.

Therefore, you could use the browser to debug your web service independent of the GWT client.

You could use python or PHP to poke the server too. May be, your QA hates Java and loves PHP - so he/she could write unit tests using PHP.

Furthermore, now, you do not have to bother with SmartGWT XML or JSON datasource and you can take full control over your POJO datasource. It's really frustrating to have to take lots of time going thro nooks and corners whenever you need to tweak your datasource a lil' bit. Now you don't have to.

Here is your web.xml to tell RestEasy to do its strut.
<web-app>
  <display-name>Employee Service</display-name>

  <!-- Auto scan REST service -->
  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
  </context-param>

  <!-- this need same with resteasy servlet url-pattern -->
  <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/CurryMuttonOilDrilling/</param-value>
  </context-param>

  <listener>
    <listener-class>
      org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
  </listener>

  <servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/CurryMuttonOilDrilling/*</url-pattern>
  </servlet-mapping>

</web-app>

Therefore, according to the web.xml, the actual URL path sent to the server is:


/CurryMuttonOilDrilling/employee/employee/12529

And notice the presence of Jackson json provider in the following maven dependency?
  <properties>
    <resteasy.version>2.3.4.Final</resteasy.version>
    <gwt.version>2.4.0</gwt.version>
  </properties>

  <repositories>
    <repository>
      <id>fusesource-snapshots</id>
      <name>Fusesource Snapshots</name>
      <url>http://repo.fusesource.com/nexus/content/repositories/snapshots</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>false</enabled></releases>
    </repository>    
  </repositories>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>1.0.0.GA</version>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
      <version>${gwt.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sencha.gxt</groupId>
        <artifactId>gxt</artifactId>
        <version>2.2.5-gwt22</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/GWT/gxt-2.2.5-gwt22.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>com.googlecode.mvp4g</groupId>
      <artifactId>mvp4g</artifactId>
      <version>1.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-jaxrs</artifactId>
      <version>${resteasy.version}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-jaxb-provider</artifactId>
      <version>${resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    <dependency>
      <groupId>org.fusesource.restygwt</groupId>
      <artifactId>restygwt</artifactId>
      <version>1.3-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.6.10.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>4.3.0.Final</version>
    </dependency>
    
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava-gwt</artifactId>
      <version>13.0-rc1</version>
    </dependency>
            <!--dependency> <groupId>com.sun.faces</groupId> <artifactId>mojarra-jsf-api</artifactId> 
      <version>2.0.0-b04</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> 
      <artifactId>mojarra-jsf-impl</artifactId> <version>2.0.0-b04</version> </dependency -->

58 comments:


  1. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai

    ReplyDelete
  2. Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

    ReplyDelete
  3. Great post!
    Thanks for sharing this list!
    It helps me a lot finding a relevant blog in my niche!
    Java Training in Chennai
    Java Training in Coimbatore
    Java Training in Bangalore

    ReplyDelete
  4. Attend The Data Analytics Courses in Bangalore From ExcelR. Practical Data Analytics Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses in Bangalore.
    ExcelR Data Analytics Courses in Bangalore

    ReplyDelete
  5. I Really appreciate, this wonderful post that you have provided for peoples. Its really good. Nice information. Keep posting!! Artificial Intelligence Course

    ReplyDelete


  6. Wow it is really wonderful and awesome thus it is veWow, it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery




    ReplyDelete
  7. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

    Data Science Institute in Bangalore

    ReplyDelete
  8. It's late finding this act. At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act.
    Data Science Course in Bangalore

    ReplyDelete
  9. 360DigiTMG, Indore is a leading solutions provider of Training and Consulting to assist students, professionals by delivering top-notch, world-class classroom and online training. It offers data science course in indore.

    ReplyDelete
  10. This is a great post, despite how much detail it contains, this style of articles keeps users interested in the website and keeps sharing more ... Good luck.data science course in malaysia

    ReplyDelete
  11. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    educational course

    ReplyDelete
  12. Truly amazing post found to be very impressive while going through this post with a complete description. Thanks for sharing and keep posting such an informative content.

    Data Science Course in Raipur

    ReplyDelete
  13. These musings just knocked my socks off. I am happy you have posted this.
    data science course in noida

    ReplyDelete
  14. I truly like only reading every one your web logs. Simply desired to in form you which you simply have persons such as me that love your own work out. Absolutely an extraordinary informative article. Hats off to you! The details which you have furnished is quite valuable. Tableau Course in Bangalore

    ReplyDelete
  15. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information...
    business analytics course

    ReplyDelete
  16. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
    business analytics course


    ReplyDelete
  17. it is often compared to Lisp, Tcl, Perl, Ruby, C#, Visual Basic, Visual Fox Pro, Scheme or Java. It can be easily interfaced with C/ObjC/Java/Fortran. It runs on all major operating systems such as Windows, Linux/Unix, OS/2, Mac, Amiga, etc. Day by day we can see a rapid growth in Python Development. data science course in india

    ReplyDelete
  18. So luck to come across your excellent blog. Your blog brings me a great deal of fun.. Good luck with the site.
    business analytics course

    ReplyDelete
  19. This is one of the best content for this topic and this is very useful for me. Thank you!
    Unix Training in Chennai
    Unix Course in Chennai
    Linux Course in Chennai

    ReplyDelete

  20. Very awesome!!! When I searched for this I found this website at the top of all blogs in search engines.


    Best Digital Marketing Courses in Hyderabad

    ReplyDelete
  21. What an incredible message this is. Truly one of the best posts I have ever seen in my life. Wow, keep it up.

    Data Science Training in Bangalore

    ReplyDelete
  22. Your work is very good and I appreciate you and hopping for some more informative posts
    data scientist certification malaysia

    ReplyDelete
  23. Wonderful blog. I delighted in perusing your articles. This is genuinely an incredible perused for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome! data analytics course in mysore

    ReplyDelete
  24. Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post .
    data analytics courses in hyderabad with placements

    ReplyDelete

  25. This post is so interactive and informative.keep update more information…
    IELTS Coaching in anna nagar
    IELTS Coaching in Chennai

    ReplyDelete
  26. I am a new user of this site so here i saw multiple articles and posts posted by this site,I curious more interest in some of them hope you will give more information on this topics in your next articles.

    ReplyDelete
  27. There are also varieties of options available to choose a career in data science, like you can become a data scientist, developer, data engineer, analyst, database administrator, and many more.

    data science training in patna

    ReplyDelete
  28. 360DigiTMG is the top-rated institute for Data Science in Bangalore and it has been awarded as the best training institute by Brand Icon. Click the link below to know about fee details.
    Data Science Course in Bangalore

    ReplyDelete
  29. Data Science handles structured and unstructured and data that is generated at an unprecedented rate every day. Anyone with a strong statistical background and an analytical mindset enjoys the challenges of big data that involves building data models and software platforms along with creating attractive visualizations and machine learning algorithms. Sign up for the Data Science courses in Bangalore with Placements and get access to resume building and mock interviews that will help you get placed with top brands in this field.
    Data Science Course in Bangalore with Placement

    ReplyDelete
  30. my blog check my blog navigate to these guys additional reading navigate to this site my site

    ReplyDelete
  31. Avast Secureline VPN 5.13.5702 Crack is a privacy and internet security software. It protects user data and operating systems from potential malware attach. Avast Secureline VPN License Key

    ReplyDelete
  32. Internet Download Manager is the world's most famous downloader in the current era. The popularity of this downloader has been increasing day by da Download Tally ERP 9 With GST Crack Version

    ReplyDelete
  33. Really a awesome blog for the freshers. Thanks for posting the information. Discover the world's best GCSE home tuition with Ziyyara Edutech! Our dedicated GCSE online tuition is designed to empower students with exceptional educational support.
    For more info visit Home tuition of GCSE

    ReplyDelete