Top Banner
15

RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

Nov 08, 2018

Download

Documents

dinhmien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE
Page 2: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 3: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 4: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 5: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

JSON Example{

"book":{

"isbn" : "12356789",

"title" : "Algorithm",

"author" : [

"Cormen",

"Rivest",

"Stein"

],

"price" : 45.78

}

}

REST 5

Page 6: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 7: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 8: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 9: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

REST Application Configimport javax.ws.rs.ApplicationPath;

import javax.ws.rs.core.Application;

@ApplicationPath("/rest-prefix")

public class ApplicationConfig extends Application {

}

REST 9

Page 10: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 11: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 12: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

@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

Page 13: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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

Page 14: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

@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

Page 15: RESTdonatas/PSKurimas... · 2017-04-13 · Unlike SOAP-based web services, ... Java API for XML –Restful Services JSR 339: ... Standardizes RESTful Service API for Java EE

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