Wednesday, November 20, 2013



RESTWebservices and GUI Framework PinPoints:


1. Based on my experience with GWT/Vaadin and REST Webservice dont ever treat JAXB Beans as a Model in the UI layer.


2. Create a separate model objects in the UI Layer and have 'Has-A' relationship with the JAXB Model.

3. Having separate model objects in UI Layer separates the logic from the JAXB Beans and the UML Model generated.

4. If the UML Model generated beans implemented default version of equals()/hashcode() methods this will have impact on the UI layer as well.

5. Maintaining/Versioning of the UML will impact the JAXB beans which will break the UI layer.


Conclusion:

In the UI layer, have a separate model which will allow to have greater flexibility in terms of UI handling with respect to data formats, rendering, etc...

Saturday, November 9, 2013

REST Webservice example





Steps to create REST Webservice with Jersey example:

1. Create Dynamic Web Project in Eclipse


2. Below is the project structure.






3. Download and copy Jersey [jersey-bundle-1.17.1.jar] and asm [asm-3.3.1.jar] to /RESTfulExample/WebContent/WEB-INF/lib


4. Create a REST Servlet service in the below folder structure.

5. Jersey high-level definition of some of the overview:








Below is the HelloWorldService.java content copy


@Path  ->  Defines the Servlet name through the "Path" annotation
@GET   ->  Defines the HTTP Method
@Path("/{param}") - Defines the Path Param in the URI
@Produces(MediaType.TEXT_PLAIN) - In simple terms output type


package main.java.com.rest;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getMsg(@PathParam("param") String msg) {
         String output = "Jersey say : " + msg;
         System.out.println("Welcome to Jersey Apache tomcat---->"+output);
         return Response.status(200).entity(output).build();
     }
   
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response getMsg() {
         String output = "Jersey say : " + "Hanuman";
         System.out.println("Welcome to Jersey Apache tomcat---->");
         return Response.status(200).entity(output).build();
     }

    @POST
    public void postMsg(@FormParam("name") String name) {
        System.out.println("At post method---> " + name);
    }
   
}


6. Build the project from Eclipse through Project -> Build Project options


7. Right click on Project Name and select "Export".

8. Click on 'Next' button


9. Select 'Web Project' and specify the 'Destination' where the WAR file to be saved.

For simplicity, I have copied to tomcat/webapps location


10. Test the REST webservice through chrome browser - POSTMAN plugin and click on SEND button.

Method: GET 




11. To test the URI through Jersey REST client or programmatically below are steps :

a) Copy the libs to jar files below project structure and add to java build





b) Below is the Java file for testing the URI

package com.rest.junit;
import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class JTest {
   
  public static void main(String[] args) {
  
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());
    // Get plain text
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class));
    // Get XML
    System.out.println( service.path("rest").path("hello").path("RamaSita_Hanuman").accept(MediaType.TEXT_PLAIN).get(String.class));

  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/RESTfulExample").build();
  }

}



12.Select the Java file and click on 'Run As' and select 'Java Application.