Upload
dinhmien
View
219
Download
0
Embed Size (px)
REST REpresentational State Transfer
Introduced and defined in 2000 by Roy Fielding in his PhD
http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
Fielding developed the REST architectural style in parallel with HTTP 1.1 protocol during 1996-1999
2REST
Resource The key abstraction of information in REST is a
resource.
Any information that can be named can be a resource:
a document or image,
a temporal service (e.g. "today's weather in Los Angeles"),
a collection of other resources,
and so on
REST 3
Resource Representation REST components perform actions on a resource by
using a representation to:
capture the current or intended state of that resource and
transferring that representation between components
A representation is:
a sequence of bytes, and
representation metadata to describe those bytes
The data format of a representation is called media type
REST 4
JSON Example{
"book":{
"isbn" : "12356789",
"title" : "Algorithm",
"author" : [
"Cormen",
"Rivest",
"Stein"
],
"price" : 45.78
}
}
REST 5
REST Constraints (not all) Stateless – No client context is being stored on the
server between requests.
Each request from any client contains all the information necessary to service the request, and session state is held in the client
Identification of resources – individual resources are identified in requests
for example using URIs
http://library.org/book/isbn/978-0596801687
Resource representation – information about resource that is enough to create, modify or delete it
REST 6
REST vs SOAP Unlike SOAP-based web services, there is no "official"
standard for RESTful web APIs
REST is an architectural style, while SOAP is a protocol.
Even though REST is not a standard per se, most RESTful implementations make use of standards such as:
HTTP, URI, JSON, and XML
REST 7
JAX-RS: REST in Java EE JAX-RS: Java API for XML – Restful Services
JSR 339:
https://jcp.org/aboutJava/communityprocess/final/jsr339/index.html
Standardizes RESTful Service API for Java EE
JSON-B: Java class <-> JSON binding specification (annotations)
JSR 367
https://www.jcp.org/en/jsr/detail?id=367
To be included in Java EE 8
REST 8
REST Application Configimport javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest-prefix")
public class ApplicationConfig extends Application {
}
REST 9
Resource: XML representation of JPA entity@Entity
… other JPA annotations …
@XmlRootElement
public class User {
@Id // and other JPA annotations
private long id;
private String name;
…
@XmlTransient // cyclic references -> skip
@ManyToMany
private List<Course> courseList;
}
REST 10
Resource: JSON representation of JPA entity@Entity
… other JPA annotations …
public class User {
@Id // and other JPA annotations
private long id;
private String name;
…
@JsonbIgnore // in Java EE 8 …
@ManyToMany
private List<Course> courseList;
}
REST 11
@ApplicationScoped
@Path("/user")
@Produces(MediaType.APPLICATION_XML)
public class UseCaseController1 {
@Inject
private EntityManager em;
@Path("/show/{userid}")
@GET
public User find(@PathParam("userid") long id) {
User user = em.find(User.class, id);
return user;
}
REST 12
GET Operation
PUT Operation@Path("/create")
@PUT
@Transactional
public User create(@QueryParam("name") String name,
@QueryParam("pwd") String pwd,
@QueryParam("mail") String mail) {
User user = new User();
user.setFullname(name);
user.setPassword(pwd);
user.setEmail(mail);
em.persist(user);
return user;
}
REST 13
@Path("/update/{id}")
@POST @Transactional
public Response update(@PathParam("id") long id,
@QueryParam("name") String name,
@QueryParam("pwd") String pwd,
@QueryParam("mail") String mail) {
User user = em.find(User.class, id);
if (user == null) {
throw new IllegalArgumentException("user id "
+ id + " not found");
}
user.setFullname(name);
user.setPassword(pwd);
user.setEmail(mail);
em.merge(user);
return Response.ok(user).build(); // low level API
} REST 14
POST Operation
Resources https://jax-rs-spec.java.net
http://www.mif.vu.lt/~donatas/PSKurimas-TechPlatformos/Library/refcards/refcard-rest.pdf
JAX-RS: Advanced Topics and an Example https://docs.oracle.com/javaee/7/tutorial/jaxrs-
advanced.htm#GJJXE
Using JAX-RS with JAXB https://docs.oracle.com/javaee/7/tutorial/jaxrs-
advanced007.htm#GKKNJ
JSON-B: Java class <-> JSON binding specification (annotations) JSR 367 – will be included in Java EE 8
https://www.jcp.org/en/jsr/detail?id=367
REST 15