Friday, 11 March 2016

REST webservice - Representational State Transfer(by Roy Fielding)

Initial Setup - jaxrs 1.9
  • Download Jersey 1.x jars from https://jersey.java.net/download.html
  • Create Dynamic web project in Eclipse EE version
  • Copy Jersey 1.x jars to eclipse lib folder in  
  • Add the Jersey sevelet & init param details to web.xml
        <servlet>
            <servlet-name>ServletAdaptor</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.venkat.rest</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>ServletAdaptor</servlet-name>
            <url-pattern>/resources/*</url-pattern>
        </servlet-mapping>
  • Make sure that the init Param name value is the package where the resources are placed. Which means if you want to make something a resource it should belong to that PACKAGE. (jersey.config.server.provider.packages is the param name for jax rs 2?)
  • Sample resource code
    package com.venkat.rest;

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


    @Path("/weatherService")
    public class WeatherService {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String findWeather() {
            return "Hyderabad 25 40";
        }


        @GET
        @Path("/findWetherBy")
        public String findWeatherByZip() {
            return "Bangalore 30 40";
        }

        @GET
        @Path(value = "findWeatherByZip/{zip}")
        public String findWeatherByZip(@PathParam("zip") int zip) {
            return "Hyd 30,40";
        }
  • index.jsp

    <html>
    <head>
    <title>Rest Services</title>
    </head>
    <body>
    <a href="resources/weatherService">Weather Service</a><br/>
    <a href="resources/weatherService/findWetherBy">findWetherBy</a><br/>
    <a href="resources/weatherService/findWeatherByZip/3">findWeatherByZip3</a>
    </body>
    </html>

Jersey Rest Client:

public class MyRestClient{
    public static void main (String[] args){
        Client client = ClientBuilder.newClient();
        String result = client.target("....restapi/myresource").request().get(String.class);
        Sysout(result)
}
}


What are webservices:
  Services that are exposed to internet for programatic access and can be called by your code. They are online apis.

What is difference between website and webservice
  website are for human consumption. HTML data (twitter.com)
  xml/json bare bone data. No fancy html/css required(api.twitter.com)

Restful webservices:
  Restful apis are type of webservices that are modern, light weight and use lot of concepts behind HTTP(technology that drives the web).

Characteristics of Web Services
   Exchange of data happens over the web over http. Client sends HTTP request, and server responds again with HTTP response.

Protocol(format in which data moves from server and client):  None for REST services.

Method: None, use any http methods

Service Definition(WSDL): Document which defines what the web service does. NONE for REST service

Restful webservice do not have any SPECIFICATION. Its an idea / an architectural style.