Friday, September 19, 2014

Notes on REST

It is based on plain HTTP, statelessness. It is a architectural style instead of a standard.

http://docs.oracle.com/javaee/7/tutorial/doc/jaxrs.htm

-- resource identification though URI
--uniformed interface: put, get, delete, post
--self-descriptive message
--stateful interaction through hyperlink  link(every interaction itself is  stateless)

Safe method: read only
idempotent: same input same output. delete is idempotent because subsequent calling wonèt change the outcome. POST is not idempotent because calling it multiple times with same parameters can have  different outcomes.


components of RESTful web service:
- base uri for web service
- media type supported by ws
- methods or verbs used such as get, put, post, and delete.

JAX-RS
java api for representationalstate transfer is a specification defines a set of java apis for building web services conforming to REST style. it defines how to expose POJOs as web resources using HTTP as network protocal.

Practice:
-- create a RESTful root resourse
A root resource is a POJO annotated with @Path
(this function is my answer in Amazons phone interview :))

import java.io.*;
import java.util.*;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
 * root resource, exposed at algorithm
 * @author ama
 *
 */
@Path("/test")
public class Algorithm {
/*@GET
static String simpleTest() {
   
    return "I am on service";
}
*/
@GET
@Path("/{statement}")
//I forgot to make this method as public at first place, then it mademe struggled for more than one hour to figure out why it was not working
public String isValid(@PathParam("statement") String statement) {
    if (isValidStatement(statement))
    return "balanced";
    else
    return "imbalanced";
}

   static boolean isValidStatement(@PathParam("statement") String statement) {
       Stack sHelper = new Stack();
       Character tmp;
       Character leftP = new Character('(');
       Character rightP = new Character(')');
       Character leftB = new Character('[');
       Character rightB = new Character(']');
       if (statement == null) return false;
       for (int i=0;i            tmp = statement.charAt(i);
           if (tmp.equals(leftB) || tmp.equals(leftP)){
               sHelper.push(tmp);
           }else if (tmp.equals(rightB) ){
               if (!sHelper.isEmpty() && leftB.equals(sHelper.peek()))
                   sHelper.pop();
               else
               return false;
             
           }else if (tmp.equals(rightP) ){
               if (!sHelper.isEmpty() && leftP.equals(sHelper.peek()))
                   sHelper.pop();
               else
               return false;
           //need a } here
           }//thank you
       }
       return sHelper.empty();
   }

}


--make a web,xml to describe the web app. you do not  need  to define any servlet and servlet mapping in server let 3.0. in version prior to 3.0, I tried to use jersey to make it work.  the commented definination in the web.xml are under the help of jersey.


  my REST Example



--test it
http://localhost:8080/algorithm/test/abcdefg(fdfd)kll(ll

No comments: