Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2013
Contenido
Actualizar....
REST. Concepto
Representational State Transfer
Estilo arquitectnico para exponer servicios sobre
HTTP/HTTPS.
Restricciones REST
Principios REST
Principios fundamentales:
Mtodo HTTP
Mtodo
Operacin CRUD
Seguro
Idempotente
GET
Leer recurso
Si
Si
POST
No
No
PUT
Si
DELETE
Borrar recurso
Si
No
GET http://{host:port}/library/v1/books/0201709066
PUT http://{host:port}/library/v1/books/0201709066
POST http://{host:port}/library/v1/books/0201709066/reviews
DELETE http://{host:port}/library/v1/books/0201709066
Sustantivos en plural.
Traer todos los libros:
GET http://{host:port}/library/v1/books
Traer un libro:
GET http://{host:port}/library/v1/books/0201709066
Representar modelo jerrquico de datos
GET http://{host:port}/library/v1/books/0201709066/reviews/1
Rango
Descripcin
1xx (Meta)
2xx (xito)
Operacin exitosa
3xx (Redireccin)
Modelado de recursos
Reflejar relaciones y estructura jerrquica
http://api.nfl.com/v1/divisions/nfc-west/teams/arizona/players
http://api.nfl.com/v1/divisions/nfc-west/teams/arizona
http://api.nfl.com/v1/divisions/nfc-west/teams
http://api.nfl.com/v1/divisions/nfc-west
http://api.nfl.com/v1/divisions
Patrones de recursos
Document
Collection
Store
Controller
Document
Un documento representa un nico recurso (una
entidad)
Ejemplos:
http://rest.service.com/library/v1/books/0201709066
http://rest.service.com/library/v1/books/0201709066/r
eviews/1
Collection
Representa un grupo de documentos
Ejemplo:
http://rest.service.com/library/v1/books/0201709066/reviews
Store
Similar a un collection. Desde el store se pueden obtener
documentos, modificarlos, insertarlos y editarlos.
Diferencias:
El cliente asigna las URIs para los nuevos elementos.
No soporta POST, siempre se crea con PUT.
Controller
No es estrctamente RESTful.
Permite agregar nuevo verbo a un recurso.
Mapear operaciones que no necesariamente se mapean a un
mtodo HTTP.
Siempre deben usar POST.
Ejemplo:
POST http://rest.com/library/v1/books/66/overdue-alerts/1/resend
El mtodo GET
@GET
@Path("/books")
@ProduceMime("application/xml")
public String getBooks()
{
//implementation here...
}
@GET
@Path("/books/{isbn}")
@ProduceMime("application/xml")
public String getBook(@PathParam("isbn")String id)
{
//implementation here...
}
QueryParam
@GET
@Path("/users")
public String getUser(@QueryParam("id")String userId)
{
//implementation here...
}
Para la URL:
/users?id=1234
El mtodo PUT
@PUT
@Path("/books/{isbn}")
@ConsumeMime("application/xml")
public Response addUpdateBook(@PathParam("isbn") String isbn, String body)
{
if (books.containsKey(isbn))
response = Response.status(200).build(); //updated
else
response = Response.status(201).build(); //created
//....
return response;
}
El mtodo DELETE
@DELETE
@Path("/books/{isbn}")
public Response removeBook(@PathParam("isbn")String isbn)
{
//implementation here...
}
Cliente de ejemplo
Obtener un libro:
<form name=datos>
<input type=text name=idlibro onblur=traerDesc()/>
<div id=descLibro></div>
</form>
<script>
function traerDesc() {
Ajax(/library/books/ + $('idlibro').value, GET,
function (var response) {
var libro = jQuery.parseJSON(response);
$('descLibro').innerHTML = libro.descripcion;
}
}
</script>
Cliente de ejemplo
Guardar un libro:
<form name=datos>
<input type=text name=id />
<input type=text name=desc />
<input type=button onclick=guardar() />
</form>
<script>
function guardar() {
Ajax(/library/books/ + $('id').value, PUT,
{ desc :' + $('desc').value + '} ,
function (var response) {
var libro = jQuery.parseJSON(response);
$('descLibro').innerHTML = libro.descripcion;
}
}
</script>
Referencias
RESTful Web Services CookBook. Subbu Allamaraju.
http://www.sourcestream.com/programming-stuff/guide-to-rest-services
http://www.sourcestream.com/programming-stuff/how-to-create-a-restfulweb-service