SlideShare a Scribd company logo
1 of 55
Download to read offline
IT Dan 2008
                             Java conference
                            Belgrade, Serbia
                           October 30th, 2008




      Have some REST
    Building Web 2.0
applications and services
         Nenad V. Nikolic
Senior Software Engineer, Mogul Balkan
       Software Consultant, NVN
Author background check

    Qualifications, experience?
    −   Web portals, CMS, applications
    −   Graphical apps: GIS, anti-spam
    −   networking drivers
    −   Integration/middleware
    −   Java, C#, C++, Python, …
   http://www.xing.com/profile/Nenad_Nikolic
   http://www.linkedin.com/in/shonzilla


    Oct 30 2008       Have some REST     2/55
Web background check
•
    Web 1.0: terminal programming model
•
    Web 1.00001: traces of JavaScript appear
•
    Web 1.1: IE showing JavaScript errors
•
    Web 1.2: web gets the 3rd dimension... alas, buttons only
•
    Web 1.3: built for IE or Netscape
•
    Web 1.4: basic client-side validation, 3D smileys
•
    Web 1.5: Flash says “Please wait...”
•
    Web 1.6: Popup calendar... not visible in IE4
•
    Web 1.7: Back not working - shows porno instead (whoops!)
•
    Web 1.8: Win Server 2003 trusted zone hell
•
    Web 1.9: You play Flash games until Web 2.0 comes
•
    Web 2.0: full-blown email client inside the browser, etc.




     Oct 30 2008               Have some REST                   3/55
What is Web 2.0?




Oct 30 2008        Have some REST   4/55
What REST is not?


              NOT a protocol
              NOT a library


               NOT HYPE ;-)




Oct 30 2008       Have some REST   5/55
What is REST?


 REpresentational State Transfer
              Architectural style
  Roy T. Fielding's PhD thesis
   (don't worry, REST is simple)
Web [2.0] – an application domain


Oct 30 2008         Have some REST   6/55
What is REST? (cont'd)

  Software architecture for building
           hypermedia systems
          (web is one of them)

     Network architecture principles

Way to design data oriented interfaces

     We focus on the Web: HTTP + URI

Alternative to traditional web services
 RPC, SOA, SOAP - bad to better&complex

 Oct 30 2008     Have some REST   7/55
REST for the web
• REST over HTTP
• Sound foundations
• Standards with proven track record:
  • URI - RFC 2396, 08/1998; de jure
  • HTTP 1.1 - RFC 2616, 08/1999; de jure
  • REST PhD dissertation, 2000; de facto
• Standards mash-up -> synergy


  Oct 30 2008        Have some REST   8/55
Principles of REST

1a. Application state -> Resource(s)

1b. Functionality -> Resource

2. Each resource: uniquely addressable

3. Universal addressing scheme

4. Uniform interface to xfer representation

5. Underlying protocol (HTTP) requirements



  Oct 30 2008      Have some REST        9/55
Protocol requirements
                 
                     Client-server
                request-response,
         client wants data, server has it
                     
                         Stateless
        request = independent transaction
                     
                         Cacheable
                         ++performance
                         
                             Layered
              proxies + caches + firewalls

Oct 30 2008                  Have some REST   10/55
Resource


  Source of information
for clients (for caches/proxies too)


Multiple representations
 HTML, JSON, XML, image, PDF, ...


  Resources hyperlinked
 “Web things” point one to another
REST “syntax”

    Each resource is a noun

    Noun - person, account... anything

    Action over a resource is a verb

    Verb - HTTP method

    Representation is an adverb

    Adverb - content type


    Oct 30 2008       Have some REST   12/55
REST “syntax” (cont’d)
                     Nouns
                  Unconstrained
                    Resources




                    REST
       Verbs                             Adverbs
    Constrained                        Constrained
   HTTP methods                       Content types




Oct 30 2008          Have some REST                   13/55
Techy visualization


                   Instances




      Operations                      Data types


              Simple, isn’t it?

Oct 30 2008          Have some REST                14/55
REST principles

    Adressability

    Uniform interface

    Representation oriented

    Stateless communication




    Oct 30 2008        Have some REST   15/55
Object: addressability

    Resource addresses

    Each address is unique

    Infinite URI naming space
    −   http://shonzilla.com/customers/123
    −   http://shonzilla.com/customers/politika
    −   http://shonzilla.com/orders/08/165
    −   http://shonzilla.com/products/53
    −   http://shonzilla.com/orders/7865/items/2,5
    −   http://stanford.edu/students/08/123
    −   http://stanford.edu/students?name=Sergey&graduated=true

    Bookmark, biz card, even tattoo?!

    Oct 30 2008              Have some REST              16/55
Operations: uniform
              interface
• Constrained by HTTP

   HTTP           GET      POST           PUT   DELETE

  Operation
                  Find   Create Update Delete

  Database
                 SELECT INSERT UPDATE DELETE



   Oct 30 2008           Have some REST          17/55
Data types:
                  representations

    Request metadata - HTTP headers

    Content-Type: HTTP header

    Internet media types aka MIME types
    −   text/html, application/pdf
    −   application/xml
    −   application/json
    −   <type>/<subtype>

    Content negotiation – flexibility

    Oct 30 2008        Have some REST   18/55
Benefits

    Ease of use

    Simple and self-descriptive
    interface

    Decoupling

    Leveraging existing infrastructure

    Caches, proxies



    Oct 30 2008    Have some REST   19/55
Benefits (cont’d)

    Multiple representations

    Accessibility (web clients&servers)

    Same API for servers, web browsers

    ... (many more)

    Web 2.0 minus GUI
   GUI: any AJAX library (incl. GWT)


    Oct 30 2008    Have some REST   20/55
Applications of REST

    Want to develop simple interfaces?

    Have some rest!

    Web services, APIs

    You have publicly available data?
    You need API!

    Accessible data
    can be viral!

    Submitting data can
    make it grow faster!

    “What is an API?
    I call it Biz Dev 2.0!”
    - Caterina Fake, Flickr


    Oct 30 2008            Have some REST   21/55
Example: XML
                      representation

    Client request:
    GET http://shonzilla.com/orders/06/81 HTTP/1.1
    Content-Type: application/xml


    Server response:
    Content-Type: application/xml
    Content-Length: 202
    <?xml version=quot;1.0quot;?>
    <order self='http://shonzilla.com/customers/06/81' >
      <amount currency=“RSD”>17641.00</amount>
      <customer ref='http://shonzilla.com/customers/123' />
      <order-entries>
        <order-entry>
          <product ref='http://shonzilla.com/products/53'/>
          <quantity>4</quantity>
        </order-entry>
        <order-entry>
          <product ref='http://shonzilla.com/products/29‘/>
          <quantity>2</quantity>
        </order-entry>
        <order-entry>
          <product ref='http://shonzilla.com/products/81‘ />
          <quantity>2</quantity>
        <order-entry>
      </order-entries>
    </order>




     Oct 30 2008                        Have some REST         22/55
Example: PDF
                  representation

    Client request:
    GET http://shonzilla.com/orders/06/81 HTTP/1.1
    Accept: application/pdf


    Server response:
    Content-Length: 53248
    Content-Type: application/pdf




    Oct 30 2008         Have some REST        23/55
Example: GET error

    Client request:
    GET http://shonzilla.com/orders/09/999 HTTP/1.1
    Accept: application/xml


    Server response:
    HTTP/1.1 404 Not Found
    Content-Type: text/html
    Date: Thu, 30 Oct 2008 00:52:09 GMT
    Connection: close
    Content-Length: 26
    <h1>Order not found.</h1>




    Oct 30 2008         Have some REST         24/55
Example: create

    Client request:
    POST http://shonzilla.com/orders HTTP/1.1
    Accept: application/xml
    Content-Length: 309
    <?xml version=quot;1.0quot;?>
    <order self='http://shonzilla.com/customers/08/165' >
      <amount>23523</amount>
      <customer ref='http://shonzilla.com/customers/123' />
      <order-entries>
        <order-entry>
          <product ref='http://shonzilla.com/products/53’/>
          <quantity>2</quantity>
        </order-entry>
        <order-entry>
          <product ref='http://shonzilla.com/products/86‘ />
        </order-entry>
      </order-entries>
    </order>



     Oct 30 2008            Have some REST              25/55
Example: created
Server response:
 HTTP/1.1 201 Created
 Location: /orders/08/165
 Content-Type: text/html
 Date: Thu, 30 Oct 2008 01:18:09 GMT
 Connection: close
 Content-Length: 36
 <h1>Order created successfully.</h1>




  Oct 30 2008        Have some REST     26/55
HTTP on steroids

    HTTP status codes – less is more

    Headers instruct the web
    −   Representation – Accept, Content-Type
    −   Where next - Location
    −   Everyone obey this - Cache-Control
    −   Verify local copy w/ crypto checksum - ETag
    −   Localization - Content-Language
    −   Age, Date, Expires, Last-Modified, ...
    −   Link: <mailto:shone@europe.com>; rev=“Madequot;

    Oct 30 2008         Have some REST          27/55
RESTful vs. RESTless

    RESTful, RESTlike, RESTless

    HTTP API is NOT REST

    RESTful:
    −   Hypertext -   resources pointing at each other

    −   All HTTP methods, HTTP status codes
    −   Obey content types
    −   URI design ~= API design


    Oct 30 2008         Have some REST         28/55
RESTful vs. RESTless #2

    Overloading POST for create&update

    XHR - some web browsers have issues
    −   http://www.mnot.net/javascript/xmlhttprequest/

    HTTP   method tunneling (XHR,             misconfigured nets?)

    −   Anti-pattern: operation in URI, etc.
    −   Header (GData): X-HTTP-Method-Override
    −   Query parameter (set from hidden form field or
        via JavaScript; Rails uses POST tunneling):
        GET http://shonzilla.com/orders/13&_method=delete HTTP/1.1
        POST http://shonzilla.com/orders/&_method=delete HTTP/1.1



    Oct 30 2008              Have some REST               29/55
REST API by the REST man

    URI is enough. Everything else should be
    figured out by the client.

    “A REST API should be entered with no prior knowledge beyond the initial URI
    (bookmark) and set of standardized media types that are appropriate for the
    intended audience (i.e., expected to be understood by any client that might use
    the API). From that point on, all application state transitions must be driven
    by client selection of server-provided choices that are present in the received
    representations or implied by the user’s manipulation of those representations.
    The transitions may be determined (or limited by) the client’s knowledge of
    media types and resource communication mechanisms, both of which may be improved
    on-the-fly (e.g., code-on-demand).“
    - Roy T. Fielding, REST APIs must be hypertext-driven


    This requires quite a lot from a client.

    Such a generic REST client would be universal




     Oct 30 2008                   Have some REST                      30/55
REST vs. RPC & SOAP

    SOAP, WSDL, UDDI

    RPC tight coupling

    WS-* over-designed; “WS Death Star”

    Insufficient tooling (until soapUI)

    XML schema obsoletes SOAP encoding

    REST: flexibility + faster processing


    Oct 30 2008    Have some REST   31/55
Java API for REST
   JAX-RS a.k.a. JSR 311, now final
   Will be included in Java EE 6 (JSR 316)

    Java API for RESTful web services

    Closely related alphabet soup:
    −   XML, JSON (JavaScript Object Notation)
    −   JAXB 2 (Java Architecture for XML Binding)
    −   RSS (Really Simple Sindication)
    −   APP (Atom Publishing Protocol)

    URI mapping using UriTemplate

    Resource life-cycles: 1/request, 1/application

    Oct 30 2008             Have some REST           32/55
REST libs & frameworks

    Jersey, reference implementation

    Restlet, lightweight framework

    Apache CXF, implements WS-* too

    JBoss RESTEasy

    NetKernel (RESTful app server)

    Axis2, don’t even bother

    Spring 3.0 (in Jan 2009)

    ...

    Oct 30 2008      Have some REST    33/55
Java code, finally
   Use JAX-RS: import javax.ws.rs.*

    Define a resource class

    Pass parameters to resource

    Implement GET method(s)

    MIME typed custom representation

    Implement POST method

    Support multiple representations w/ JAXB

    Jersey life-cycle support: singleton and
    session resources

    Oct 30 2008    Have some REST      34/55
Resource class that GETs
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
 @GET public String getOrders() {…}
}




    Oct 30 2008   Have some REST   35/55
Resource class that GETs...
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
 @GET public String getOrders() {…}
    // GET /orders/523
    @GET @Path(quot;/{id}quot;)
    public String getOrder(@PathParam(quot;idquot;) int orderId) {
      // get order object,
      // extract attributes, build response
    }
}
// Representation behind String: YAML



        Oct 30 2008       Have some REST           36/55
Resource class that GETs it
// JAX-RS resource skeleton
@Path(”/ordersquot;)
public class OrderResource {
 // ...
 @GET @Path(quot;/{id}quot;)
 @Produces(quot;text/xmlquot;)
  public String getOrder(@PathParam(quot;idquot;) int orderId) {
    // get order object,
    // extract attributes, prepare response
    return response; // XML-formatted
    }
}



        Oct 30 2008     Have some REST           37/55
Resource class that searches
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
 // ...
 @GET @Path(quot;/{search}quot;)
 @Produces(quot;text/xmlquot;)
   public String getOrder(@PathParam(quot;searchquot;) String query) {
     // search searchable order attributes for query text,
     // extract attributes for each result, prepare response
     return response; // XML-formatted
    }
}




        Oct 30 2008           Have some REST           38/55
Support representations

    Method overriding problem?

    No problem

    Resource method names - anything

    How about repeating code?

    Hold on... we handle this elegantly




    Oct 30 2008   Have some REST   39/55
Resource class that GETs
               it
// JAX-RS resource skeleton

@Path(quot;/ordersquot;)

public class OrderResource {

 // ...

 @GET @Path(quot;/{id}quot;) @Produces(quot;text/xmlquot;)
   public String getOrder(@PathParam(quot;idquot;) int orderId) {…}

 @GET @Path(quot;/{id}quot;) @Produces(quot;text/htmlquot;)
   public String getOrderHtml(@PathParam(quot;idquot;) int orderId) {…}

 @GET @Path(quot;/{id}quot;) @Produces(quot;application/jsonquot;)
   public String getOrderJson(@PathParam(quot;idquot;) int orderId) {…}


}

// Imagine having many resources like this.
// We need to get rid of formatting boiler-plate code.



    Oct 30 2008                Have some REST                 40/55
Custom representation
         using a MIME type

    Building different representations

    Again and again? No, thanks!

    java.ws.rs.ext.* to the rescue

    response body builder (marshaller)

    Built-in support for XML and JSON
    using JAXB

    JAXB annotations are needed

    Oct 30 2008   Have some REST   41/55
Annotated
      resource entity classes
@XmlRootElement(name=quot;orderquot;)  
public class ApiOrder {  
   @XmlAttribute(name=quot;idquot;) int id;  
   @XmlElement(name=quot;customer-idquot;) 
  int customerId;  
   @XmlElement(quot;order-entriesquot;) 
  List<ApiOrderEntry> entries;
     @XmlElement(“amountquot;) 
    ApiNumber amount;
// ...  
}  




      Oct 30 2008         Have some REST   42/55
Return “any”
                  represenattion
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
 // ...
 // Marshaller will take care
 @GET @Path(quot;/{id}quot;)
  @Produces({quot;application/xml”,
  quot;application/jsonquot;})
  public ApiOrder getOrder(@PathParam(quot;idquot;) int
    orderId) {…}
}

    Oct 30 2008       Have some REST    43/55
Resource class that POSTs
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
    // ...
    // POST /orders
    // (with order XML data in request body)
 @POST @Consumes(quot;application/xmlquot;) public
  Response createOrder(ApiOrder newOrder) {…}
}


     Oct 30 2008     Have some REST     44/55
Updating an order with PUT
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
    // ...
    // PUT /orders/512
    // (w/ data in request body)
 @PUT @Path(quot;/{id}quot;)
  @Consumes(quot;application/xmlquot;)
  public Response createOrder(
    @PathParam(quot;idquot;) int orderId,
    ApiOrder newOrder) {…}
}

     Oct 30 2008     Have some REST   45/55
HTTP status codes
// JAX-RS resource skeleton

@Path(quot;/ordersquot;)

public class OrderResource {

 // ...

@GET @Path(quot;/{id}quot;) @Produces({ quot;application/xmlquot;, quot;application/jsonquot;})
   public Response getOrder(@PathParam(quot;idquot;) int orderId) {
     Order order = OrderService.getOrder(orderId);
     ApiOrder apiOrder = ApiOrderFactory.getOrder(order);
     Response.ResponseBuilder response = Response.ok(apiOrder);
     Date now = new Date();
     Date paymentDeadline = OrderService.calcPaymentDeadline(now);
     resp.expires(paymentDeadline);
     return response;

    }

}




        Oct 30 2008            Have some REST                 46/55
Error handling
// JAX-RS resource skeleton
@Path(quot;/ordersquot;)
public class OrderResource {
 // ...
@GET @Path(quot;/{id}quot;)
   @Produces({ quot;application/xmlquot;, quot;application/jsonquot;})
   public ApiOrder getOrder(@PathParam(quot;idquot;) int orderId) {
     Order order = OrderService.getOrder(orderId);
     if (order == null) {
       throw new NotFoundException(“order “ + orderId);
     }
     ApiOrder apiOrder = ApiOrderFactory.getOrder(order);
     return apiOrder;
    }
}


        Oct 30 2008           Have some REST           47/55
Custom exceptions
// Custom exception
public class NotFoundException extends
  WebApplicationException {
    public NotFoundException() {
      super(Response.Status.GONE);
    }
    public NotFoundException(String msg) {
      super(Response.Status.GONE, msg);
    }
}
// WebApplicationException is a run-time exc.

    Oct 30 2008       Have some REST   48/55
More JAX-RS annotations

    @Singleton -   singleton resource instance

    @QueryParam, @HeaderParam, @MatrixParam

    @DefaultValue - default param                value
    (if one is ommitted from URI)

    @Context - inject [UriInfo, Request,
    HttpHeaders, SecurityContext] into a
    class field, property or method param

    @CacheControl - resource controls cache

    @Cookie, @CookieParam - NOTE:
    propagating server-side session state is an
    anti-pattern
    Oct 30 2008       Have some REST             49/55
Other topics

    AAA = authentication + authorization + accounting
    (per resource|method)

    Accounting = request logging

    Request throttling (req/min, etc.) - to reduce server load

    In-container API unit testing

    API/URI design and versioning

    application.wadl file shows the available resources “menu”

    RIA using REST:
     −   GWT (Google Web Toolkit)
     −   Adobe AIR and Flex
     −   JavaFX

    Stay tuned...



     Oct 30 2008                    Have some REST      50/55
REST protocols

    REST application protocols

    Interacting with the web
    −   Publish and edit Web resources
   APP (Atom     Publishing Protocol),    RFC 5023
    −   Implemented under Apache Abdera project
   GData (Google data APIs)
    −   APP + queries + auth + optimist.lock.
    −   Contacts, Calendar, Docs, ...


    Oct 30 2008           Have some REST          51/55
REST Clients

    web browsers (Web 2.0 clients)

    Web services clients

    Integration systems (Mule, ServiceMix)
   Mobile devices (Google Android)

    Web apps (Google Services:
    Calendar, Docs, Contacts, etc.)



    Oct 30 2008      Have some REST   52/55
Famous APIs

    Web API == HTTP API != REST API

    Flickr - RESTless: delete is POSTed

    Amazon S3 - RESTlike and SOAPy storage

    Digg - social news, read-only and clean API

    Twitter - RESTful microblogging

    Yelp - RESTful yellow pages

    Delicious - RESTless social bookmarking
   New York Times (!)
   and many more (see Programmable web)

    Oct 30 2008      Have some REST        53/55
Beyond API – API mashups

    DIY - learn REST, JAX-RS, etc.

    Commercial outsourcing:
       Mashery (on-demand API infrastructure)
       email me (custom API R&D)
   GNIP - data portability & filtering

    More to come...
   Stay tuned: Shonzilla, JavaSvet

    Oct 30 2008      Have some REST    54/55
GET /applause HTTP/1.1

                200 OK Thank you!



ttp://shonzilla.com/docs/it-dan-2008_have-some-rest.pdf

More Related Content

What's hot

ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
Rest api design by george reese
Rest api design by george reeseRest api design by george reese
Rest api design by george reesebuildacloud
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & DevelopmentAshok Pundit
 
Creating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandCreating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandMatthew Turland
 
Rest api standards and best practices
Rest api standards and best practicesRest api standards and best practices
Rest api standards and best practicesAnkita Mahajan
 
REST: The Most Misunderstood Software Architecture Ever
REST: The Most Misunderstood Software Architecture EverREST: The Most Misunderstood Software Architecture Ever
REST: The Most Misunderstood Software Architecture EverMartín Soto
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Mario Cardinal
 
API Testing. Streamline your testing process.
API Testing. Streamline your testing process.API Testing. Streamline your testing process.
API Testing. Streamline your testing process.Andrey Oleynik
 
ITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul ServicesITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul ServicesOrtus Solutions, Corp
 
Building ColdFusion And AngularJS Applications
Building ColdFusion And AngularJS ApplicationsBuilding ColdFusion And AngularJS Applications
Building ColdFusion And AngularJS ApplicationsColdFusionConference
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web APIBrad Genereaux
 
ASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVCASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVCBilal Amjad
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonAdnan Masood
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUDPrem Sanil
 

What's hot (20)

ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
Rest api design by george reese
Rest api design by george reeseRest api design by george reese
Rest api design by george reese
 
REST full API Design
REST full API DesignREST full API Design
REST full API Design
 
Web API Basics
Web API BasicsWeb API Basics
Web API Basics
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
Creating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandCreating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew Turland
 
Rest api standards and best practices
Rest api standards and best practicesRest api standards and best practices
Rest api standards and best practices
 
REST: The Most Misunderstood Software Architecture Ever
REST: The Most Misunderstood Software Architecture EverREST: The Most Misunderstood Software Architecture Ever
REST: The Most Misunderstood Software Architecture Ever
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
API Testing. Streamline your testing process.
API Testing. Streamline your testing process.API Testing. Streamline your testing process.
API Testing. Streamline your testing process.
 
ITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul ServicesITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul Services
 
Building ColdFusion And AngularJS Applications
Building ColdFusion And AngularJS ApplicationsBuilding ColdFusion And AngularJS Applications
Building ColdFusion And AngularJS Applications
 
A Bit of REST
A Bit of RESTA Bit of REST
A Bit of REST
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
ASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVCASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVC
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
 
Web api
Web apiWeb api
Web api
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
 
What is an API?
What is an API?What is an API?
What is an API?
 
Hidden Gems in ColdFusion 2016
Hidden Gems in ColdFusion 2016Hidden Gems in ColdFusion 2016
Hidden Gems in ColdFusion 2016
 

Similar to Have Some Rest Building Web2.0 Apps And Services

[DSBW Spring 2009] Unit 02: Web Technologies (1/2)
[DSBW Spring 2009] Unit 02: Web Technologies (1/2)[DSBW Spring 2009] Unit 02: Web Technologies (1/2)
[DSBW Spring 2009] Unit 02: Web Technologies (1/2)Carles Farré
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web ServicesBradley Holt
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座Li Yi
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest apiTony Nguyen
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptxJames Wong
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptxHarry Potter
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptxLuis Goldster
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptxFraboni Ec
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptxYoung Alista
 
Applications of the REST Principle
Applications of the REST PrincipleApplications of the REST Principle
Applications of the REST Principleelliando dias
 
Architecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APISHAKIL AKHTAR
 
REST Introduction (PHP London)
REST Introduction (PHP London)REST Introduction (PHP London)
REST Introduction (PHP London)Paul James
 
A RESTful introduction
A RESTful introductionA RESTful introduction
A RESTful introductionDaniel Toader
 
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)Carles Farré
 
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...CloudIDSummit
 
Google G Data Reading And Writing Data On The Web
Google G Data Reading And Writing Data On The WebGoogle G Data Reading And Writing Data On The Web
Google G Data Reading And Writing Data On The WebQConLondon2008
 
Google G Data Reading And Writing Data On The Web 1
Google G Data Reading And Writing Data On The Web 1Google G Data Reading And Writing Data On The Web 1
Google G Data Reading And Writing Data On The Web 1QConLondon2008
 

Similar to Have Some Rest Building Web2.0 Apps And Services (20)

[DSBW Spring 2009] Unit 02: Web Technologies (1/2)
[DSBW Spring 2009] Unit 02: Web Technologies (1/2)[DSBW Spring 2009] Unit 02: Web Technologies (1/2)
[DSBW Spring 2009] Unit 02: Web Technologies (1/2)
 
WWW and HTTP
WWW and HTTPWWW and HTTP
WWW and HTTP
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web Services
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座
 
RESTful design
RESTful designRESTful design
RESTful design
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest api
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 
Speed = $$$
Speed = $$$Speed = $$$
Speed = $$$
 
Applications of the REST Principle
Applications of the REST PrincipleApplications of the REST Principle
Applications of the REST Principle
 
Architecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web API
 
REST Introduction (PHP London)
REST Introduction (PHP London)REST Introduction (PHP London)
REST Introduction (PHP London)
 
A RESTful introduction
A RESTful introductionA RESTful introduction
A RESTful introduction
 
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
 
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
 
Google G Data Reading And Writing Data On The Web
Google G Data Reading And Writing Data On The WebGoogle G Data Reading And Writing Data On The Web
Google G Data Reading And Writing Data On The Web
 
Google G Data Reading And Writing Data On The Web 1
Google G Data Reading And Writing Data On The Web 1Google G Data Reading And Writing Data On The Web 1
Google G Data Reading And Writing Data On The Web 1
 

Recently uploaded

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Have Some Rest Building Web2.0 Apps And Services

  • 1. IT Dan 2008 Java conference Belgrade, Serbia October 30th, 2008 Have some REST Building Web 2.0 applications and services Nenad V. Nikolic Senior Software Engineer, Mogul Balkan Software Consultant, NVN
  • 2. Author background check  Qualifications, experience? − Web portals, CMS, applications − Graphical apps: GIS, anti-spam − networking drivers − Integration/middleware − Java, C#, C++, Python, …  http://www.xing.com/profile/Nenad_Nikolic  http://www.linkedin.com/in/shonzilla Oct 30 2008 Have some REST 2/55
  • 3. Web background check • Web 1.0: terminal programming model • Web 1.00001: traces of JavaScript appear • Web 1.1: IE showing JavaScript errors • Web 1.2: web gets the 3rd dimension... alas, buttons only • Web 1.3: built for IE or Netscape • Web 1.4: basic client-side validation, 3D smileys • Web 1.5: Flash says “Please wait...” • Web 1.6: Popup calendar... not visible in IE4 • Web 1.7: Back not working - shows porno instead (whoops!) • Web 1.8: Win Server 2003 trusted zone hell • Web 1.9: You play Flash games until Web 2.0 comes • Web 2.0: full-blown email client inside the browser, etc. Oct 30 2008 Have some REST 3/55
  • 4. What is Web 2.0? Oct 30 2008 Have some REST 4/55
  • 5. What REST is not? NOT a protocol NOT a library NOT HYPE ;-) Oct 30 2008 Have some REST 5/55
  • 6. What is REST? REpresentational State Transfer Architectural style Roy T. Fielding's PhD thesis (don't worry, REST is simple) Web [2.0] – an application domain Oct 30 2008 Have some REST 6/55
  • 7. What is REST? (cont'd) Software architecture for building hypermedia systems (web is one of them) Network architecture principles Way to design data oriented interfaces We focus on the Web: HTTP + URI Alternative to traditional web services RPC, SOA, SOAP - bad to better&complex Oct 30 2008 Have some REST 7/55
  • 8. REST for the web • REST over HTTP • Sound foundations • Standards with proven track record: • URI - RFC 2396, 08/1998; de jure • HTTP 1.1 - RFC 2616, 08/1999; de jure • REST PhD dissertation, 2000; de facto • Standards mash-up -> synergy Oct 30 2008 Have some REST 8/55
  • 9. Principles of REST 1a. Application state -> Resource(s) 1b. Functionality -> Resource 2. Each resource: uniquely addressable 3. Universal addressing scheme 4. Uniform interface to xfer representation 5. Underlying protocol (HTTP) requirements Oct 30 2008 Have some REST 9/55
  • 10. Protocol requirements  Client-server request-response, client wants data, server has it  Stateless request = independent transaction  Cacheable ++performance  Layered proxies + caches + firewalls Oct 30 2008 Have some REST 10/55
  • 11. Resource Source of information for clients (for caches/proxies too) Multiple representations HTML, JSON, XML, image, PDF, ... Resources hyperlinked “Web things” point one to another
  • 12. REST “syntax”  Each resource is a noun  Noun - person, account... anything  Action over a resource is a verb  Verb - HTTP method  Representation is an adverb  Adverb - content type Oct 30 2008 Have some REST 12/55
  • 13. REST “syntax” (cont’d) Nouns Unconstrained Resources REST Verbs Adverbs Constrained Constrained HTTP methods Content types Oct 30 2008 Have some REST 13/55
  • 14. Techy visualization Instances Operations Data types Simple, isn’t it? Oct 30 2008 Have some REST 14/55
  • 15. REST principles  Adressability  Uniform interface  Representation oriented  Stateless communication Oct 30 2008 Have some REST 15/55
  • 16. Object: addressability  Resource addresses  Each address is unique  Infinite URI naming space − http://shonzilla.com/customers/123 − http://shonzilla.com/customers/politika − http://shonzilla.com/orders/08/165 − http://shonzilla.com/products/53 − http://shonzilla.com/orders/7865/items/2,5 − http://stanford.edu/students/08/123 − http://stanford.edu/students?name=Sergey&graduated=true  Bookmark, biz card, even tattoo?! Oct 30 2008 Have some REST 16/55
  • 17. Operations: uniform interface • Constrained by HTTP HTTP GET POST PUT DELETE Operation Find Create Update Delete Database SELECT INSERT UPDATE DELETE Oct 30 2008 Have some REST 17/55
  • 18. Data types: representations  Request metadata - HTTP headers  Content-Type: HTTP header  Internet media types aka MIME types − text/html, application/pdf − application/xml − application/json − <type>/<subtype>  Content negotiation – flexibility Oct 30 2008 Have some REST 18/55
  • 19. Benefits  Ease of use  Simple and self-descriptive interface  Decoupling  Leveraging existing infrastructure  Caches, proxies Oct 30 2008 Have some REST 19/55
  • 20. Benefits (cont’d)  Multiple representations  Accessibility (web clients&servers)  Same API for servers, web browsers  ... (many more)  Web 2.0 minus GUI  GUI: any AJAX library (incl. GWT) Oct 30 2008 Have some REST 20/55
  • 21. Applications of REST  Want to develop simple interfaces?  Have some rest!  Web services, APIs  You have publicly available data? You need API!  Accessible data can be viral!  Submitting data can make it grow faster!  “What is an API? I call it Biz Dev 2.0!” - Caterina Fake, Flickr Oct 30 2008 Have some REST 21/55
  • 22. Example: XML representation  Client request: GET http://shonzilla.com/orders/06/81 HTTP/1.1 Content-Type: application/xml  Server response: Content-Type: application/xml Content-Length: 202 <?xml version=quot;1.0quot;?> <order self='http://shonzilla.com/customers/06/81' > <amount currency=“RSD”>17641.00</amount> <customer ref='http://shonzilla.com/customers/123' /> <order-entries> <order-entry> <product ref='http://shonzilla.com/products/53'/> <quantity>4</quantity> </order-entry> <order-entry> <product ref='http://shonzilla.com/products/29‘/> <quantity>2</quantity> </order-entry> <order-entry> <product ref='http://shonzilla.com/products/81‘ /> <quantity>2</quantity> <order-entry> </order-entries> </order> Oct 30 2008 Have some REST 22/55
  • 23. Example: PDF representation  Client request: GET http://shonzilla.com/orders/06/81 HTTP/1.1 Accept: application/pdf  Server response: Content-Length: 53248 Content-Type: application/pdf Oct 30 2008 Have some REST 23/55
  • 24. Example: GET error  Client request: GET http://shonzilla.com/orders/09/999 HTTP/1.1 Accept: application/xml  Server response: HTTP/1.1 404 Not Found Content-Type: text/html Date: Thu, 30 Oct 2008 00:52:09 GMT Connection: close Content-Length: 26 <h1>Order not found.</h1> Oct 30 2008 Have some REST 24/55
  • 25. Example: create  Client request: POST http://shonzilla.com/orders HTTP/1.1 Accept: application/xml Content-Length: 309 <?xml version=quot;1.0quot;?> <order self='http://shonzilla.com/customers/08/165' > <amount>23523</amount> <customer ref='http://shonzilla.com/customers/123' /> <order-entries> <order-entry> <product ref='http://shonzilla.com/products/53’/> <quantity>2</quantity> </order-entry> <order-entry> <product ref='http://shonzilla.com/products/86‘ /> </order-entry> </order-entries> </order> Oct 30 2008 Have some REST 25/55
  • 26. Example: created Server response: HTTP/1.1 201 Created Location: /orders/08/165 Content-Type: text/html Date: Thu, 30 Oct 2008 01:18:09 GMT Connection: close Content-Length: 36 <h1>Order created successfully.</h1> Oct 30 2008 Have some REST 26/55
  • 27. HTTP on steroids  HTTP status codes – less is more  Headers instruct the web − Representation – Accept, Content-Type − Where next - Location − Everyone obey this - Cache-Control − Verify local copy w/ crypto checksum - ETag − Localization - Content-Language − Age, Date, Expires, Last-Modified, ... − Link: <mailto:shone@europe.com>; rev=“Madequot; Oct 30 2008 Have some REST 27/55
  • 28. RESTful vs. RESTless  RESTful, RESTlike, RESTless  HTTP API is NOT REST  RESTful: − Hypertext - resources pointing at each other − All HTTP methods, HTTP status codes − Obey content types − URI design ~= API design Oct 30 2008 Have some REST 28/55
  • 29. RESTful vs. RESTless #2  Overloading POST for create&update  XHR - some web browsers have issues − http://www.mnot.net/javascript/xmlhttprequest/  HTTP method tunneling (XHR, misconfigured nets?) − Anti-pattern: operation in URI, etc. − Header (GData): X-HTTP-Method-Override − Query parameter (set from hidden form field or via JavaScript; Rails uses POST tunneling): GET http://shonzilla.com/orders/13&_method=delete HTTP/1.1 POST http://shonzilla.com/orders/&_method=delete HTTP/1.1 Oct 30 2008 Have some REST 29/55
  • 30. REST API by the REST man  URI is enough. Everything else should be figured out by the client.  “A REST API should be entered with no prior knowledge beyond the initial URI (bookmark) and set of standardized media types that are appropriate for the intended audience (i.e., expected to be understood by any client that might use the API). From that point on, all application state transitions must be driven by client selection of server-provided choices that are present in the received representations or implied by the user’s manipulation of those representations. The transitions may be determined (or limited by) the client’s knowledge of media types and resource communication mechanisms, both of which may be improved on-the-fly (e.g., code-on-demand).“ - Roy T. Fielding, REST APIs must be hypertext-driven  This requires quite a lot from a client.  Such a generic REST client would be universal Oct 30 2008 Have some REST 30/55
  • 31. REST vs. RPC & SOAP  SOAP, WSDL, UDDI  RPC tight coupling  WS-* over-designed; “WS Death Star”  Insufficient tooling (until soapUI)  XML schema obsoletes SOAP encoding  REST: flexibility + faster processing Oct 30 2008 Have some REST 31/55
  • 32. Java API for REST  JAX-RS a.k.a. JSR 311, now final  Will be included in Java EE 6 (JSR 316)  Java API for RESTful web services  Closely related alphabet soup: − XML, JSON (JavaScript Object Notation) − JAXB 2 (Java Architecture for XML Binding) − RSS (Really Simple Sindication) − APP (Atom Publishing Protocol)  URI mapping using UriTemplate  Resource life-cycles: 1/request, 1/application Oct 30 2008 Have some REST 32/55
  • 33. REST libs & frameworks  Jersey, reference implementation  Restlet, lightweight framework  Apache CXF, implements WS-* too  JBoss RESTEasy  NetKernel (RESTful app server)  Axis2, don’t even bother  Spring 3.0 (in Jan 2009)  ... Oct 30 2008 Have some REST 33/55
  • 34. Java code, finally  Use JAX-RS: import javax.ws.rs.*  Define a resource class  Pass parameters to resource  Implement GET method(s)  MIME typed custom representation  Implement POST method  Support multiple representations w/ JAXB  Jersey life-cycle support: singleton and session resources Oct 30 2008 Have some REST 34/55
  • 35. Resource class that GETs // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { @GET public String getOrders() {…} } Oct 30 2008 Have some REST 35/55
  • 36. Resource class that GETs... // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { @GET public String getOrders() {…} // GET /orders/523 @GET @Path(quot;/{id}quot;) public String getOrder(@PathParam(quot;idquot;) int orderId) { // get order object, // extract attributes, build response } } // Representation behind String: YAML Oct 30 2008 Have some REST 36/55
  • 37. Resource class that GETs it // JAX-RS resource skeleton @Path(”/ordersquot;) public class OrderResource { // ... @GET @Path(quot;/{id}quot;) @Produces(quot;text/xmlquot;) public String getOrder(@PathParam(quot;idquot;) int orderId) { // get order object, // extract attributes, prepare response return response; // XML-formatted } } Oct 30 2008 Have some REST 37/55
  • 38. Resource class that searches // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... @GET @Path(quot;/{search}quot;) @Produces(quot;text/xmlquot;) public String getOrder(@PathParam(quot;searchquot;) String query) { // search searchable order attributes for query text, // extract attributes for each result, prepare response return response; // XML-formatted } } Oct 30 2008 Have some REST 38/55
  • 39. Support representations  Method overriding problem?  No problem  Resource method names - anything  How about repeating code?  Hold on... we handle this elegantly Oct 30 2008 Have some REST 39/55
  • 40. Resource class that GETs it // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... @GET @Path(quot;/{id}quot;) @Produces(quot;text/xmlquot;) public String getOrder(@PathParam(quot;idquot;) int orderId) {…} @GET @Path(quot;/{id}quot;) @Produces(quot;text/htmlquot;) public String getOrderHtml(@PathParam(quot;idquot;) int orderId) {…} @GET @Path(quot;/{id}quot;) @Produces(quot;application/jsonquot;) public String getOrderJson(@PathParam(quot;idquot;) int orderId) {…} } // Imagine having many resources like this. // We need to get rid of formatting boiler-plate code. Oct 30 2008 Have some REST 40/55
  • 41. Custom representation using a MIME type  Building different representations  Again and again? No, thanks!  java.ws.rs.ext.* to the rescue  response body builder (marshaller)  Built-in support for XML and JSON using JAXB  JAXB annotations are needed Oct 30 2008 Have some REST 41/55
  • 42. Annotated resource entity classes @XmlRootElement(name=quot;orderquot;)   public class ApiOrder {      @XmlAttribute(name=quot;idquot;) int id;      @XmlElement(name=quot;customer-idquot;)  int customerId;      @XmlElement(quot;order-entriesquot;)  List<ApiOrderEntry> entries;   @XmlElement(“amountquot;)  ApiNumber amount; // ...   }   Oct 30 2008 Have some REST 42/55
  • 43. Return “any” represenattion // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... // Marshaller will take care @GET @Path(quot;/{id}quot;) @Produces({quot;application/xml”, quot;application/jsonquot;}) public ApiOrder getOrder(@PathParam(quot;idquot;) int orderId) {…} } Oct 30 2008 Have some REST 43/55
  • 44. Resource class that POSTs // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... // POST /orders // (with order XML data in request body) @POST @Consumes(quot;application/xmlquot;) public Response createOrder(ApiOrder newOrder) {…} } Oct 30 2008 Have some REST 44/55
  • 45. Updating an order with PUT // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... // PUT /orders/512 // (w/ data in request body) @PUT @Path(quot;/{id}quot;) @Consumes(quot;application/xmlquot;) public Response createOrder( @PathParam(quot;idquot;) int orderId, ApiOrder newOrder) {…} } Oct 30 2008 Have some REST 45/55
  • 46. HTTP status codes // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... @GET @Path(quot;/{id}quot;) @Produces({ quot;application/xmlquot;, quot;application/jsonquot;}) public Response getOrder(@PathParam(quot;idquot;) int orderId) { Order order = OrderService.getOrder(orderId); ApiOrder apiOrder = ApiOrderFactory.getOrder(order); Response.ResponseBuilder response = Response.ok(apiOrder); Date now = new Date(); Date paymentDeadline = OrderService.calcPaymentDeadline(now); resp.expires(paymentDeadline); return response; } } Oct 30 2008 Have some REST 46/55
  • 47. Error handling // JAX-RS resource skeleton @Path(quot;/ordersquot;) public class OrderResource { // ... @GET @Path(quot;/{id}quot;) @Produces({ quot;application/xmlquot;, quot;application/jsonquot;}) public ApiOrder getOrder(@PathParam(quot;idquot;) int orderId) { Order order = OrderService.getOrder(orderId); if (order == null) { throw new NotFoundException(“order “ + orderId); } ApiOrder apiOrder = ApiOrderFactory.getOrder(order); return apiOrder; } } Oct 30 2008 Have some REST 47/55
  • 48. Custom exceptions // Custom exception public class NotFoundException extends WebApplicationException { public NotFoundException() { super(Response.Status.GONE); } public NotFoundException(String msg) { super(Response.Status.GONE, msg); } } // WebApplicationException is a run-time exc. Oct 30 2008 Have some REST 48/55
  • 49. More JAX-RS annotations  @Singleton - singleton resource instance  @QueryParam, @HeaderParam, @MatrixParam  @DefaultValue - default param value (if one is ommitted from URI)  @Context - inject [UriInfo, Request, HttpHeaders, SecurityContext] into a class field, property or method param  @CacheControl - resource controls cache  @Cookie, @CookieParam - NOTE: propagating server-side session state is an anti-pattern Oct 30 2008 Have some REST 49/55
  • 50. Other topics  AAA = authentication + authorization + accounting (per resource|method)  Accounting = request logging  Request throttling (req/min, etc.) - to reduce server load  In-container API unit testing  API/URI design and versioning  application.wadl file shows the available resources “menu”  RIA using REST: − GWT (Google Web Toolkit) − Adobe AIR and Flex − JavaFX  Stay tuned... Oct 30 2008 Have some REST 50/55
  • 51. REST protocols  REST application protocols  Interacting with the web − Publish and edit Web resources  APP (Atom Publishing Protocol), RFC 5023 − Implemented under Apache Abdera project  GData (Google data APIs) − APP + queries + auth + optimist.lock. − Contacts, Calendar, Docs, ... Oct 30 2008 Have some REST 51/55
  • 52. REST Clients  web browsers (Web 2.0 clients)  Web services clients  Integration systems (Mule, ServiceMix)  Mobile devices (Google Android)  Web apps (Google Services: Calendar, Docs, Contacts, etc.) Oct 30 2008 Have some REST 52/55
  • 53. Famous APIs  Web API == HTTP API != REST API  Flickr - RESTless: delete is POSTed  Amazon S3 - RESTlike and SOAPy storage  Digg - social news, read-only and clean API  Twitter - RESTful microblogging  Yelp - RESTful yellow pages  Delicious - RESTless social bookmarking  New York Times (!)  and many more (see Programmable web) Oct 30 2008 Have some REST 53/55
  • 54. Beyond API – API mashups  DIY - learn REST, JAX-RS, etc.  Commercial outsourcing:  Mashery (on-demand API infrastructure)  email me (custom API R&D)  GNIP - data portability & filtering  More to come...  Stay tuned: Shonzilla, JavaSvet Oct 30 2008 Have some REST 54/55
  • 55. GET /applause HTTP/1.1 200 OK Thank you! ttp://shonzilla.com/docs/it-dan-2008_have-some-rest.pdf