SlideShare uma empresa Scribd logo
1 de 24
Web Services (NSWI145)
Lecture 07: RESTful Web Services

  Martin Nečaský, Ph.D.
  Faculty of Mathematics and Physics
  Charles University in Prague, Czech Republic
What is REST?
   REpresentation State Transfer
   software architectural style for building
    distributed hypermedia systems
   set of following architectural principles
       resource orientation
       unique resource identification
       stateless client/server interaction
       uniform interface
   e.g. Web architecture is based on the same
    principles as REST
       but REST principles were derived from the
        architecture of Web (Roy Fielding dissertation)
Principle 1: Resource Orientation
   resource = concrete or even abstract thing/action
    we want to publish
     everything is resource in REST
   each resource has its representation = document
    that can be sent between communicating peers
     representation format needs to be established
       • different (meta-)formats may be used (e.g. HTML, XML,
         JSON, RDF, AtomPub, ...)
       • each resource can have more representations (in different
         formats)
     representation contains links to related resources
       • representation format must support links
       • applications which consume resources navigate instead of
         calling
Principle 2: Unique Resource Identification
   each resource has unique ID (name)
     universal syntax for resource IDs is necessary, e.g. URI
     ID serves not just as a name but also as a means of
      accessing resource representation
     parametrized IDs
        http://www.company.org/customer?name=John

   distinguish resource ID from resource representation ID
    (e.g. HTML, XML, JSON, RDF documents)
     if resource = document then resource ID = resource
      representation ID
     otherwise we need strategy to allow clients to request
      specific resource representation
Principle 2: Unique Resource Identification
   direct dereferencing
     resource ID is not dereferenceable
     client has to know particular resource
      representation IDs
   e.g. Twitter uses direct dereferencing
https://api.twitter.com/1/statuses/user_timeline.json
https://api.twitter.com/1/statuses/user_timeline.xml
Principle 2: Unique Resource Identification
   303 URIs
     resource ID is dereferenceable
     HTTP mechanism
     two requests
       • client requests resource ID and specified preferred
         resource representation format
          – server sends resource representation ID
       • client requests resource representation ID
Principle 2: Unique Resource Identification


                                                         Server
                   GET /customer?name=John
                   Host: www.company.org
client             Accept: text/xml




                                                         Server
         HTTP/1.1 303 See Other
         Location:
client   http://www.company.org/customer.xml?name=John




                                                         Server
                 GET /customer.xml?name=John
                 Host: www.company.org
client           Accept: text/xml
Principle 2: Unique Resource Identification
   how to set-up 303 URIs?
   e.g. Apache HTTPD (.htaccess file)
RewriteCond %{HTTP_ACCEPT} application/rdf+xml
RewriteRule ^customer customer.rdf [R=303]




http://www.company.org/            http://www.company.org/
   customer?name=John               customer.xml?name=John
Principle 2: Unique Resource Identification
   avoid parametrized resource ID
     e.g.
             http://www.company.org/customer/John
     instead of
        http://www.company.org/customer?name=John




RewriteCond %{HTTP_ACCEPT} application/rdf+xml
RewriteRule ^customer/([a-zA-Z]+)$ customer.rdf?name=John
Principle 3: Stateless Client/Server
                 Communication
   request/response message exchange
   separation of concerns principle
     clients separated from servers by interface
     clients are not concerned with data storage and
      (most) application logic
     servers are not concerned with user interface or
      state (server simplicity and scalability)
     independent evolution of clients and server
     e.g. HTTP request/response, SOAP
      request/response message exchange pattern, etc.
Principle 3: Stateless Client/Server
                  Communication
   stateless communication
     no state in server-side applications
     move state to clients and/or resources
   resource state is the same for every client
     client changes to resource state affect all other
      clients
   client state is specific for each particular client
     communication state
Principle 4: Resource Manipulation
   uniform interface for resource manipulation
     small set of operations which apply for everything
       • e.g. CRUD operations (Create, Retrieve, Update, Delete)
     small set of verbs which apply to large set of
      nouns
       • if many applications need new verb, uniform interface
         can be extended
   do not encode verbs into resource identifiers
        http://www.company.org/addCustomer?name=John
Principle 4: Resource Manipulation
   when HTTP protocol is used, following four operations are
    usually considered
     GET = requests representation of the specified resource
        • read-only operation, should be safe = should not cause any side-
          effects
     PUT = uploads representation of the specified resource
        • write operation, should be idempotent
            – idempotent = may cause side effects but its multiple calls cause the same
              effect
            – being idempotent means being simple = identical request causes the same
              state change independently of how many times it has been called
     DELETE = deletes the specified resource
        • write operation, should be idempotent
     POST = submits data to be processed to the specified resource
        • it can update the resource
        • generally not safe and not idempotent
RESTful Web Services
   RESTful Web Service
     enables manipulation with set of resources
     typically uses XML, JSON or RDF for resource
      representation
     uses URLs as resource identifiers
     stateless
     HTTP methods GET/PUT/DELETE/POST for
      resource manipulation
   like Web application but for machines instead
    of humans
RESTful Web Services
Operation       Resource representing                   Resource representing
                collection of individuals               individual
GET             List members in collection              Retrieve individual
                •   e.g. weekly list public contracts   •   e.g. retrieve public contract
PUT             Update collection with                  Update individual
                another one                             •   e.g. update public contract
                •   e.g. replace weekly list of             representation with new
                    public contracts at the                 representation
                    beginning of new week
DELETE          Delete entire collection                Delete individual
                •   e.g. delete weekly list of          •   e.g. delete public contract
                    public contracts
POST            Create member of                        Create part of individual
                collection with auto-ID                 •   e.g. create public contract
                •   e.g. add new public contract to         tender
                    the collection and generate its
                    ID
JAX-RS (Jersey Impl.) with Tomcat + Eclipse
   download Jersey
      https://jersey.dev.java.net/
      (A zip of Jersey containing the Jersey jars, core dependencies (it does not provide
       dependencies for third party jars beyond those for JSON support) and JavaDoc)
   create new Eclipse Dynamic Web Project
    ProjectName
      Apache Tomcat as Target runtime
   ProjectName project > Properties
      Java Build Path > Libraries > Add External JARs...
          • add JARs from Jersey zip
   copy JARs from Jersey zip to WEB-INF/lib
   modify WEB-INF/web.xml
JAX-RS (Jersey Impl.) with Tomcat + Eclipse

   class PublicContracts01 in
    Procurement_REST_WS project
      http://localhost:8080/Procurement_REST_WS/res
       ources/PublicContracts01/
      provides simple representation of “List of public
       contracts” resource in plain text, HTML and XML
      basic JAX-RS annotations
        • @Path
        • @GET
        • @Produces
JAX-RS (Jersey Impl.) with Tomcat + Eclipse

   test with cURL (curl_PublicContracts01.bat)
      http://curl.haxx.se/download.html
JAX-RS (Jersey Impl.) with Tomcat + Eclipse
   class PublicContracts02 in Procurement_REST_WS project
      http://localhost:8080/Procurement_REST_WS/resources/Public
       Contracts02/
      getPublicContract method which returns XML or JSON
       representation of public contract to GET request
        • class PublicContract
        • JAXB annotation (provides XML and JSON binding)
            – @XmlRootElement
      listContracts method returns XML or JSON representation of all
       public contracts in collection to GET request
      listContractsHTML method returns HTML representation of all
       public contracts in collection to GET request
      JAX-RS annotations
        • @Path – path with path parameter
        • @PathParam – association of path parameter with method parameter
JAX-RS (Jersey Impl.) with Tomcat + Eclipse

   test
      http://localhost:8080/Procurement_REST_WS/res
       ources/PublicContracts02/
      http://localhost:8080/Procurement_REST_WS/res
       ources/PublicContracts02/4
JAX-RS (Jersey Impl.) with Tomcat + Eclipse
   class PublicContracts03 in
    Procurement_REST_WS project
      http://localhost:8080/Procurement_REST_WS/res
       ources/PublicContracts03/
      putPublicContract updates existing or creates new
       public contract
      deletePublicContract deletes existing public
       contract
      JAX-RS annotations
        • @PUT
        • @DELETE
JAX-RS (Jersey Impl.) with Tomcat + Eclipse

   test with cURL (curl_PublicContracts03.bat)
JAX-RS (Jersey Impl.) with Tomcat + Eclipse

   class PublicContracts04 in
    Procurement_REST_WS project
      http://localhost:8080/Procurement_REST_WS/res
       ources/PublicContracts04/
      createPublicContract creates new contract on
       POST request
      JAX-RS annotations
       • @POST
JAX-RS (Jersey Impl.) with Tomcat + Eclipse

   test with form_PublicContracts04.html

Mais conteúdo relacionado

Mais procurados

Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
Zoran Jeremic
 
A Conversation About REST - Extended Version
A Conversation About REST - Extended VersionA Conversation About REST - Extended Version
A Conversation About REST - Extended Version
Jeremy Brown
 

Mais procurados (20)

RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RS
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
Json to hive_schema_generator
Json to hive_schema_generatorJson to hive_schema_generator
Json to hive_schema_generator
 
Intoduction to php web services and json
Intoduction to php  web services and jsonIntoduction to php  web services and json
Intoduction to php web services and json
 
Things I wish web graduates knew
Things I wish web graduates knewThings I wish web graduates knew
Things I wish web graduates knew
 
Introduction to RESTful Webservices in JAVA
Introduction to RESTful Webservices  in JAVA Introduction to RESTful Webservices  in JAVA
Introduction to RESTful Webservices in JAVA
 
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
 
OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
A Conversation About REST
A Conversation About RESTA Conversation About REST
A Conversation About REST
 
Xcap
XcapXcap
Xcap
 
Webservice for android ppt
Webservice for android pptWebservice for android ppt
Webservice for android ppt
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
xcap
xcapxcap
xcap
 
Servlet basics
Servlet basicsServlet basics
Servlet basics
 
Virtuoso Sponger - RDFizer Middleware for creating RDF from non RDF Data Sources
Virtuoso Sponger - RDFizer Middleware for creating RDF from non RDF Data SourcesVirtuoso Sponger - RDFizer Middleware for creating RDF from non RDF Data Sources
Virtuoso Sponger - RDFizer Middleware for creating RDF from non RDF Data Sources
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
DataBearings: A semantic platform for data integration on IoT, Artem Katasonov
DataBearings: A semantic platform for data integration on IoT, Artem KatasonovDataBearings: A semantic platform for data integration on IoT, Artem Katasonov
DataBearings: A semantic platform for data integration on IoT, Artem Katasonov
 
A Conversation About REST - Extended Version
A Conversation About REST - Extended VersionA Conversation About REST - Extended Version
A Conversation About REST - Extended Version
 
Introduction to JAX-RS
Introduction to JAX-RSIntroduction to JAX-RS
Introduction to JAX-RS
 
Xcap tutorial
Xcap tutorialXcap tutorial
Xcap tutorial
 

Destaque

Web Services - Business Process Execution Language
Web Services - Business Process Execution LanguageWeb Services - Business Process Execution Language
Web Services - Business Process Execution Language
Martin Necasky
 

Destaque (10)

Web Services - Business Process Execution Language
Web Services - Business Process Execution LanguageWeb Services - Business Process Execution Language
Web Services - Business Process Execution Language
 
Tutoriál : Otevřená a propojitelná data veřejné správy
Tutoriál : Otevřená a propojitelná data veřejné správyTutoriál : Otevřená a propojitelná data veřejné správy
Tutoriál : Otevřená a propojitelná data veřejné správy
 
Linked Data for Czech Legislation - 2nd year of our project
Linked Data for Czech Legislation - 2nd year of our projectLinked Data for Czech Legislation - 2nd year of our project
Linked Data for Czech Legislation - 2nd year of our project
 
Linked Open Data for Public Contracts
Linked Open Data for Public ContractsLinked Open Data for Public Contracts
Linked Open Data for Public Contracts
 
Linked Data for Czech Legislation
Linked Data for Czech LegislationLinked Data for Czech Legislation
Linked Data for Czech Legislation
 
Linked Open Data - Masaryk University in Brno 8.11.2016
Linked Open Data - Masaryk University in Brno 8.11.2016Linked Open Data - Masaryk University in Brno 8.11.2016
Linked Open Data - Masaryk University in Brno 8.11.2016
 
WS-Addressing
WS-AddressingWS-Addressing
WS-Addressing
 
Web Services - WSDL
Web Services - WSDLWeb Services - WSDL
Web Services - WSDL
 
Web Services Tutorial
Web Services TutorialWeb Services Tutorial
Web Services Tutorial
 
Web service introduction
Web service introductionWeb service introduction
Web service introduction
 

Semelhante a RESTful Web Services

Restful web services rule financial
Restful web services   rule financialRestful web services   rule financial
Restful web services rule financial
Rule_Financial
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011
Shreedhar Ganapathy
 
Rest presentation
Rest  presentationRest  presentation
Rest presentation
srividhyau
 

Semelhante a RESTful Web Services (20)

Rest web services
Rest web servicesRest web services
Rest web services
 
RESTful for opentravel.org by HP
RESTful for opentravel.org by HPRESTful for opentravel.org by HP
RESTful for opentravel.org by HP
 
ReSTful API Final
ReSTful API FinalReSTful API Final
ReSTful API Final
 
ROA.ppt
ROA.pptROA.ppt
ROA.ppt
 
Restful webservice
Restful webserviceRestful webservice
Restful webservice
 
Restful web services rule financial
Restful web services   rule financialRestful web services   rule financial
Restful web services rule financial
 
JAX-RS. Developing RESTful APIs with Java
JAX-RS. Developing RESTful APIs with JavaJAX-RS. Developing RESTful APIs with Java
JAX-RS. Developing RESTful APIs with Java
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
Rest
RestRest
Rest
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and OData
 
A Conversation About REST
A Conversation About RESTA Conversation About REST
A Conversation About REST
 
OpenTravel Advisory Forum 2012 REST XML Resources
OpenTravel Advisory Forum 2012 REST XML ResourcesOpenTravel Advisory Forum 2012 REST XML Resources
OpenTravel Advisory Forum 2012 REST XML Resources
 
Rest presentation
Rest  presentationRest  presentation
Rest presentation
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
Introduction To REST
Introduction To RESTIntroduction To REST
Introduction To REST
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using Php
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using Jersey
 
REST Basics
REST BasicsREST Basics
REST Basics
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 

Mais de Martin Necasky (8)

Otevrena data v CR - aktualni stav (brezen 2013)
Otevrena data v CR - aktualni stav (brezen 2013)Otevrena data v CR - aktualni stav (brezen 2013)
Otevrena data v CR - aktualni stav (brezen 2013)
 
Web Services - Architecture and SOAP (part 1)
Web Services - Architecture and SOAP (part 1)Web Services - Architecture and SOAP (part 1)
Web Services - Architecture and SOAP (part 1)
 
Web Services - SOAP (part 2)
Web Services - SOAP (part 2)Web Services - SOAP (part 2)
Web Services - SOAP (part 2)
 
Otevrene problemy architektury elektronickeho zdravotnictvi
Otevrene problemy architektury elektronickeho zdravotnictviOtevrene problemy architektury elektronickeho zdravotnictvi
Otevrene problemy architektury elektronickeho zdravotnictvi
 
Vysledek souteze o navrh hospodarneho a funkcniho elektronickeho zdravotnictvi
Vysledek souteze o navrh hospodarneho a funkcniho elektronickeho zdravotnictviVysledek souteze o navrh hospodarneho a funkcniho elektronickeho zdravotnictvi
Vysledek souteze o navrh hospodarneho a funkcniho elektronickeho zdravotnictvi
 
Web Services - Introduction
Web Services - IntroductionWeb Services - Introduction
Web Services - Introduction
 
Techniky a nástroje pro propojená data (Linked Data)
Techniky a nástroje pro propojená data (Linked Data)Techniky a nástroje pro propojená data (Linked Data)
Techniky a nástroje pro propojená data (Linked Data)
 
Linked Data pro Evropský sociální fond
Linked Data pro Evropský sociální fondLinked Data pro Evropský sociální fond
Linked Data pro Evropský sociální fond
 

Último

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

RESTful Web Services

  • 1. Web Services (NSWI145) Lecture 07: RESTful Web Services Martin Nečaský, Ph.D. Faculty of Mathematics and Physics Charles University in Prague, Czech Republic
  • 2. What is REST?  REpresentation State Transfer  software architectural style for building distributed hypermedia systems  set of following architectural principles  resource orientation  unique resource identification  stateless client/server interaction  uniform interface  e.g. Web architecture is based on the same principles as REST  but REST principles were derived from the architecture of Web (Roy Fielding dissertation)
  • 3. Principle 1: Resource Orientation  resource = concrete or even abstract thing/action we want to publish  everything is resource in REST  each resource has its representation = document that can be sent between communicating peers  representation format needs to be established • different (meta-)formats may be used (e.g. HTML, XML, JSON, RDF, AtomPub, ...) • each resource can have more representations (in different formats)  representation contains links to related resources • representation format must support links • applications which consume resources navigate instead of calling
  • 4. Principle 2: Unique Resource Identification  each resource has unique ID (name)  universal syntax for resource IDs is necessary, e.g. URI  ID serves not just as a name but also as a means of accessing resource representation  parametrized IDs http://www.company.org/customer?name=John  distinguish resource ID from resource representation ID (e.g. HTML, XML, JSON, RDF documents)  if resource = document then resource ID = resource representation ID  otherwise we need strategy to allow clients to request specific resource representation
  • 5. Principle 2: Unique Resource Identification  direct dereferencing  resource ID is not dereferenceable  client has to know particular resource representation IDs  e.g. Twitter uses direct dereferencing https://api.twitter.com/1/statuses/user_timeline.json https://api.twitter.com/1/statuses/user_timeline.xml
  • 6. Principle 2: Unique Resource Identification  303 URIs  resource ID is dereferenceable  HTTP mechanism  two requests • client requests resource ID and specified preferred resource representation format – server sends resource representation ID • client requests resource representation ID
  • 7. Principle 2: Unique Resource Identification Server GET /customer?name=John Host: www.company.org client Accept: text/xml Server HTTP/1.1 303 See Other Location: client http://www.company.org/customer.xml?name=John Server GET /customer.xml?name=John Host: www.company.org client Accept: text/xml
  • 8. Principle 2: Unique Resource Identification  how to set-up 303 URIs?  e.g. Apache HTTPD (.htaccess file) RewriteCond %{HTTP_ACCEPT} application/rdf+xml RewriteRule ^customer customer.rdf [R=303] http://www.company.org/ http://www.company.org/ customer?name=John customer.xml?name=John
  • 9. Principle 2: Unique Resource Identification  avoid parametrized resource ID  e.g. http://www.company.org/customer/John  instead of http://www.company.org/customer?name=John RewriteCond %{HTTP_ACCEPT} application/rdf+xml RewriteRule ^customer/([a-zA-Z]+)$ customer.rdf?name=John
  • 10. Principle 3: Stateless Client/Server Communication  request/response message exchange  separation of concerns principle  clients separated from servers by interface  clients are not concerned with data storage and (most) application logic  servers are not concerned with user interface or state (server simplicity and scalability)  independent evolution of clients and server  e.g. HTTP request/response, SOAP request/response message exchange pattern, etc.
  • 11. Principle 3: Stateless Client/Server Communication  stateless communication  no state in server-side applications  move state to clients and/or resources  resource state is the same for every client  client changes to resource state affect all other clients  client state is specific for each particular client  communication state
  • 12. Principle 4: Resource Manipulation  uniform interface for resource manipulation  small set of operations which apply for everything • e.g. CRUD operations (Create, Retrieve, Update, Delete)  small set of verbs which apply to large set of nouns • if many applications need new verb, uniform interface can be extended  do not encode verbs into resource identifiers http://www.company.org/addCustomer?name=John
  • 13. Principle 4: Resource Manipulation  when HTTP protocol is used, following four operations are usually considered  GET = requests representation of the specified resource • read-only operation, should be safe = should not cause any side- effects  PUT = uploads representation of the specified resource • write operation, should be idempotent – idempotent = may cause side effects but its multiple calls cause the same effect – being idempotent means being simple = identical request causes the same state change independently of how many times it has been called  DELETE = deletes the specified resource • write operation, should be idempotent  POST = submits data to be processed to the specified resource • it can update the resource • generally not safe and not idempotent
  • 14. RESTful Web Services  RESTful Web Service  enables manipulation with set of resources  typically uses XML, JSON or RDF for resource representation  uses URLs as resource identifiers  stateless  HTTP methods GET/PUT/DELETE/POST for resource manipulation  like Web application but for machines instead of humans
  • 15. RESTful Web Services Operation Resource representing Resource representing collection of individuals individual GET List members in collection Retrieve individual • e.g. weekly list public contracts • e.g. retrieve public contract PUT Update collection with Update individual another one • e.g. update public contract • e.g. replace weekly list of representation with new public contracts at the representation beginning of new week DELETE Delete entire collection Delete individual • e.g. delete weekly list of • e.g. delete public contract public contracts POST Create member of Create part of individual collection with auto-ID • e.g. create public contract • e.g. add new public contract to tender the collection and generate its ID
  • 16. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  download Jersey  https://jersey.dev.java.net/  (A zip of Jersey containing the Jersey jars, core dependencies (it does not provide dependencies for third party jars beyond those for JSON support) and JavaDoc)  create new Eclipse Dynamic Web Project ProjectName  Apache Tomcat as Target runtime  ProjectName project > Properties  Java Build Path > Libraries > Add External JARs... • add JARs from Jersey zip  copy JARs from Jersey zip to WEB-INF/lib  modify WEB-INF/web.xml
  • 17. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  class PublicContracts01 in Procurement_REST_WS project  http://localhost:8080/Procurement_REST_WS/res ources/PublicContracts01/  provides simple representation of “List of public contracts” resource in plain text, HTML and XML  basic JAX-RS annotations • @Path • @GET • @Produces
  • 18. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  test with cURL (curl_PublicContracts01.bat)  http://curl.haxx.se/download.html
  • 19. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  class PublicContracts02 in Procurement_REST_WS project  http://localhost:8080/Procurement_REST_WS/resources/Public Contracts02/  getPublicContract method which returns XML or JSON representation of public contract to GET request • class PublicContract • JAXB annotation (provides XML and JSON binding) – @XmlRootElement  listContracts method returns XML or JSON representation of all public contracts in collection to GET request  listContractsHTML method returns HTML representation of all public contracts in collection to GET request  JAX-RS annotations • @Path – path with path parameter • @PathParam – association of path parameter with method parameter
  • 20. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  test  http://localhost:8080/Procurement_REST_WS/res ources/PublicContracts02/  http://localhost:8080/Procurement_REST_WS/res ources/PublicContracts02/4
  • 21. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  class PublicContracts03 in Procurement_REST_WS project  http://localhost:8080/Procurement_REST_WS/res ources/PublicContracts03/  putPublicContract updates existing or creates new public contract  deletePublicContract deletes existing public contract  JAX-RS annotations • @PUT • @DELETE
  • 22. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  test with cURL (curl_PublicContracts03.bat)
  • 23. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  class PublicContracts04 in Procurement_REST_WS project  http://localhost:8080/Procurement_REST_WS/res ources/PublicContracts04/  createPublicContract creates new contract on POST request  JAX-RS annotations • @POST
  • 24. JAX-RS (Jersey Impl.) with Tomcat + Eclipse  test with form_PublicContracts04.html