RESTful Services - e-Lite: Intelligent and Interactive...
Transcript of RESTful Services - e-Lite: Intelligent and Interactive...
RESTful Services
Sistemi Informativi Aziendali – A.A. 2012/2013
Summary
A.A. 2012/2013
Foundations
REST in Java
Sistemi Informativi Aziendali
Foundations
REST
A.A. 2012/2013
Representational State Transfer
Firstly defined by Roy Fielding (2000)
“Architectural Styles and the Design of Network-based
Software Architectures”
Has mostly displaced Web Services
Simpler
Standard
E.g. Will be part of the Java distribution through JSR-311
Sistemi Informativi Aziendali
Basic Principles
A.A. 2012/2013
Addressability
Use HTTP methods explicitly
GET,
PUT,
POST,
DELETE
Stateless
Atomic requests (no state preservation between different requests)
Expose directory structure-like URIs
Resources identified by URIs
Transfer XML, JavaScript Object Notation (JSON) or both
Sistemi Informativi Aziendali
Resource
A.A. 2012/2013
Resource
Thing exposed by the system to the outside world
Everything is a resource
http://elite.polito.it/people/bonino
http://elite.polito.it/people/corno
Independent from its representation
http://elite.polito.it/people/bonino bonino.html
http://elite.polito.it/people/bonino bonino.jpg
Sistemi Informativi Aziendali
Operations on Resources
A.A. 2012/2013
GET
Defines a reading access of the resource without side-effects. The
resource is never changed via a GET request (idempotent*).
PUT
Updates/changes an existing resource (idempotent*)
DELETE
Removes the resources (idempotent*).
POST
Creates a new resource, must also be idempotent*.
* Idempotent operations can get repeated without leading to different
results.
Sistemi Informativi Aziendali
Examples
A.A. 2012/2013
Accessing a resource (read)
GET /users/Robert HTTP/1.1
Creating a resource
POST /users HTTP/1.1
Host: myserver
Content-Type: application/xml
<?xml version="1.0"?>
<user>
<name>Robert</name>
</user>
Sistemi Informativi Aziendali
Examples
A.A. 2012/2013
Changing a resource
To change the resource Robert
Provide a new representation for Robert in the body of a PUT
request
The old representation will not be accessible anymore
PUT /users/Robert HTTP/1.1
Host: myserver
Content-Type: application/xml
<?xml version="1.0"?>
<user>
<name>Bob</name>
</user>
Sistemi Informativi Aziendali
Examples
A.A. 2012/2013
Delete a resource
DELETE /users/Robert HTTP/1.1
Sistemi Informativi Aziendali
REST in J2EE
REST in J2EE
A.A. 2012/2013
JSR-311
Aka JAX-RS
Reference implementation
Jersey
Client
Server
Restful resources identified through annotations
http://jersey.java.net/
Sistemi Informativi Aziendali
Jersey Annotations
A.A. 2012/2013 Sistemi Informativi Aziendali
Jersey + Eclipse – REST server
A.A. 2012/2013 Sistemi Informativi Aziendali
Jersey + Eclipse – REST server
A.A. 2012/2013
@PATH("/HelloREST")
public class HelloREST
{
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
}
Sistemi Informativi Aziendali
Jersey + Eclipse – REST server
A.A. 2012/2013
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
<display-name>it.polito.sistinfo.services.rest</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>it.polito.sistinfo.services.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Sistemi Informativi Aziendali
Jersey+Eclipse - Client
A.A. 2012/2013
Jersey provides a client
library to consume REST
services
Clients can alternatively
exploit
Apache Http Client
Example
Google Directions API
Client c = Client.create();
c.setFollowRedirects(true);
WebResource r =
c.resource("http://maps.googleapis.com/maps/api/distancematrix");
MultivaluedMap<String, String> params = new
MultivaluedMapImpl();
params.add("origins", "cuneo");
params.add("destinations", "torino");
params.add("language", "it-IT");
params.add("sensor", "false");
//send the query
JSONObject responseAsJSON =
r.path("json").queryParams(params).get(JSONObject.class);
//print out the query
out.println(r.path("json").queryParams(params).getURI());
out.println("<br/>");
//print out the response
out.println(responseAsJSON.toString());
Sistemi Informativi Aziendali
Licenza d’uso Queste diapositive sono distribuite con licenza Creative Commons
“Attribuzione - Non commerciale - Condividi allo stesso modo 2.5 Italia (CC BY-NC-SA 2.5)”
Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,
rappresentare, eseguire e recitare quest'opera
di modificare quest'opera
Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori
originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.
Non commerciale — Non puoi usare quest'opera per fini commerciali.
Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.
http://creativecommons.org/licenses/by-nc-sa/2.5/it/
A.A. 2012/2013 Sistemi Informativi Aziendali