Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
RESTFul Web Services -JAX-RS         (JSR 311)                                                                   João Migu...
RESTFul Web Services   • REST (Representional State Transfer)   • Architecture Sytle introduced by Roy Fielding           ...
RESTFul Web Services   • RESTful Architectural Principles         –       Addressability         –       Uniform, Constrai...
RESTful Architectural Principles   • Addressability         – Every Object/Resource in the system is reachable           t...
RESTful Architectural Principles   • Addressability         – Using URI to identify resources makes the             resour...
RESTful Architectural Principles   • Uniform, Constrained Interface         – In SOAP world, you define your interface for...
RESTful Architectural Principles   • Uniform, Constrained Interface         – With REST, we have a well defined interface ...
RESTful Architectural Principles   • Uniform, Constrained Interface         – Examples:               GET http://ordermngt...
RESTful Architectural Principles   • Uniform, Constrained Interface         – Examples:               PUT http://ordermngt...
RESTful Architectural Principles   • Uniform, Constrained Interface         – Examples:               DELETE http://orderm...
RESTful Architectural Principles   • Uniform, Constrained Interface         – Examples:               POST http://ordermng...
RESTful Architectural Principles   • Represention-Oriented         – Resources can be represented in various formats      ...
RESTful Architectural Principles   • Represention-Oriented         – Examples of Content-Type:               •       Text/...
RESTful Architectural Principles   • Example:                        Sent Request                        Request URL: http...
RESTful Architectural Principles   • Example:                   Response Body                   {                       "e...
RESTful Architectural Principles   •       Example (using headers to versioning a service):                        Sent Re...
RESTful Architectural Principles   • Representation oriented                                                              ...
RESTful Architectural Principles   • Representation oriented                                                              ...
RESTful Architectural Principles   •       Communicate Statelessly         – No client session stored on the server       ...
RESTful Architectural Principles   • HATEOAS (Hypermedia As The Engine Of     Application State)     <product id="1233">  ...
RESTFul Web Services Designing RESTful Services
Designing RESTful Services   • Let’s start by our Object Model              Employee                                      ...
Designing RESTful Services   • Let’s start model our Services                EmployeeService                              ...
Designing RESTful Services   • Model the Employee URIs         – /employees  Represent the employees           collection...
Designing RESTful Services   •       Model the Projects URIs         –       /projects  Represent the projects collection...
Designing RESTful Services   • Model the Customer URIs         – /customers Represent the customers           collection ...
Designing RESTful Services   • Defining data format         – We will use XML and JSON to represent the           resource...
Designing RESTful Services   • Define the REST Service for Project resource                                               ...
Designing RESTful Services   •       Define the GET method for all Project resources             GET http://host:port/proj...
Designing RESTful Services     •       Define the GET method for a page of Project             resources               GET...
Designing RESTful Services   •       Define the GET method for a give Project resource             GET http://host:port/pr...
Designing RESTful Services   •       Define the POST method to create a Project resource           POST http://host:port/p...
Designing RESTful Services   •       Define the PUT method to update a Project resource           PUT http://host:port/pro...
Designing RESTful Services   •       Define the DELETE method to delete a Project           resource           DELETE http...
Upcoming SlideShare
Loading in …5
×

Quick introduction to REST and JAX-RS

3,124 views

Published on

Quick introduction to REST and JAX-RS
For more information visit http://jpereira.eu/2012/04/18/quick-introduction-to-rest-and-jax-rs/
If you want to download the presentation, please contact me (fin my contacts on my blog)

Published in: Education, Technology
  • Be the first to comment

Quick introduction to REST and JAX-RS

  1. 1. RESTFul Web Services -JAX-RS (JSR 311) João Miguel Pereira http://jpereira.eu March 2012 RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  2. 2. RESTFul Web Services • REST (Representional State Transfer) • Architecture Sytle introduced by Roy Fielding (http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.ht m) • Based on key principles of WWW • REST is protocol agnostic, but most (if not all) implementations use HTTP • Requires some good knowledge of HTTP protocolRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 2Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  3. 3. RESTFul Web Services • RESTful Architectural Principles – Addressability – Uniform, Constrained Interface – Represention-Oriented – Communicate Statelessly – HATEOAS (Hypermedia As The Engine Of Application State)RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 3Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  4. 4. RESTful Architectural Principles • Addressability – Every Object/Resource in the system is reachable through a unique identifier – This unique identifier is standarized in the form of URIs (Uniform Resource Identifier): • scheme://host:port/path?queryString#fragment – Example: • http://twitter.com/joaomrpereiraRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 4Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  5. 5. RESTful Architectural Principles • Addressability – Using URI to identify resources makes the resources linkable. Example: http://ordermngt.mystore.com/orders/1232 <order> <customer>http://customers.mycompany.com/customers/7656</customer> <order-entries> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> </order-entries> </order>RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 5Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  6. 6. RESTful Architectural Principles • Uniform, Constrained Interface – In SOAP world, you define your interface for the Web Service. Example:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body> <m:getDataSourceInfo xmlns:m="http://arcweb.esri.com/v2"> <group xsi:type="xsd:string">ArcWebForDev</group> <dataSource xsi:type="xsd:string">GDT.Streets.US</dataSource> <token xsi:type="xsd:string">MyToken</token> </m:getDataSourceInfo></SOAP-ENV:Body></SOAP-ENV:Envelope> Action to call in the server. Actually, your interfaceRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 6Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  7. 7. RESTful Architectural Principles • Uniform, Constrained Interface – With REST, we have a well defined interface – Use the Methods/Verbs available in HTTP • GET – Read-only operation that return a resource from server • PUT – Write operation that updates a resource in the server • DELETE – Delete a resource from server • POST – Create a resource in the serverRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 7Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  8. 8. RESTful Architectural Principles • Uniform, Constrained Interface – Examples: GET http://ordermngt.mystore.com/orders/999 Response: <order> <customer>http://customers.mycompany.com/customers/7656</customer> <order-entries> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> </order-entries> </order> Plus some HTTP Status CodeRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 8Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  9. 9. RESTful Architectural Principles • Uniform, Constrained Interface – Examples: PUT http://ordermngt.mystore.com/orders/999 Body: <order> <customer>http://customers.mycompany.com/customers/7656</customer> <order-entries> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> </order-entries> </order> Response: Some HTTP Status CodeRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 9Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  10. 10. RESTful Architectural Principles • Uniform, Constrained Interface – Examples: DELETE http://ordermngt.mystore.com/orders/999 Response: Some HTTP Status Code + optional contentRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 10Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  11. 11. RESTful Architectural Principles • Uniform, Constrained Interface – Examples: POST http://ordermngt.mystore.com/orders/ Body: <order> <customer>http://customers.mycompany.com/customers/7656</customer> <order-entries> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> <order-entry> <quantity>10</quantity> <product>http://products.suppliercompany.com/products/92637</product> </order-entry> </order-entries> </order> Response: Some HTTP Status Code + optional contentRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 11Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  12. 12. RESTful Architectural Principles • Represention-Oriented – Resources can be represented in various formats – With HTTP, we can use the Content-Type and Accepts headers to negotiate the data format exchanged between server and client – Content-Type is the Multipurpose Internet Mail Extension (MIME) and has the following format: • Type/subtype;name=value;name=valueRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 12Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  13. 13. RESTful Architectural Principles • Represention-Oriented – Examples of Content-Type: • Text/plain • Text/html • Application/pdf • Application/xml; charset=iso-8859-1 • image/pngRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 13Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  14. 14. RESTful Architectural Principles • Example: Sent Request Request URL: http://twitter.com/joaomrpereira Request Method: GET Sent Headers Accept: application/json Accept-Language: en Client http://twitter.comRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 14Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  15. 15. RESTful Architectural Principles • Example: Response Body { "error": "Rate limit exceeded. Clients may not make more than 150 requests per hour.", "request": "/" } Received Headers Status Code: 400 Status: 400 Bad Request Client Content-Type: application/json; charset=utf-8 http://twitter.comRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 15Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  16. 16. RESTful Architectural Principles • Example (using headers to versioning a service): Sent Request Request URL: http://twitter.com/joaomrpereira Request Method: GET Sent Headers Accept: application/json;version=1.2 Accept-Language: en Client http://twitter.comRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 16Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  17. 17. RESTful Architectural Principles • Representation oriented Representations {order: {“id”:”123”, “items”:[{item: {...}]}} JSON Order 123 GET http://host/orders/123 Accept: application/json XML Accept-Language: en PDFRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 17Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  18. 18. RESTful Architectural Principles • Representation oriented Representations JSON Order 123 GET http://host/orders/123 Accept: application/pdf XML Accept-Language: en PDF File PDFRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 18Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  19. 19. RESTful Architectural Principles • Communicate Statelessly – No client session stored on the server – Server only manages the state of resoureces it exposes – Client session data should be managed by client itself – If server needs client session data it should be sent with every request – https://developers.facebook.com/docs/authenticatio n/ – Imagine Facebook holding client session data on their servers on behalf of their users’s sessions....RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 19Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  20. 20. RESTful Architectural Principles • HATEOAS (Hypermedia As The Engine Of Application State) <product id="1233"> <name>RESTFul Web Services Books</name> <price>45.00</price> <description>The book description</description> <link rel="buy" href="http://bookstore/products/1233/order"/> <link rel="delete" href="http://bookstore/products/1233/deleted"/> <link rel="edit" href="http://bookstore/products/1233/edited"/> <link rel=“comments" href="http://bookstore/products/1233/comments"/> </product>RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 20Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  21. 21. RESTFul Web Services Designing RESTful Services
  22. 22. Designing RESTful Services • Let’s start by our Object Model Employee Project Id: Long Id: Long Name: String 0..* Name: String startDate: Date bornDate: Date endDate: Date price: Long void: addProject(Project) void: addEmployee(Employee) Void:removeProject(Project) Void:removeEmployee(Employee) Collection<Project> : getProjects() Collection<Employee> : getEmployees() Void: setCustomer(Customer) Void: addDocument(Document) Customer Void: removeDocument(Document) Collection<Document>: getDocuments() 1 Id: Long Name: String Document void: addProject(Project) Id: Long Void:removeProject(Project) title: String 1 Collection<Project> : getProjects() Byte[]: content 0..*RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 22Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  23. 23. Designing RESTful Services • Let’s start model our Services EmployeeService ProjectService CustomerService Employee: createEmployee() Project: createProject(....) Customer: createCustomer(String) Employee: updateEmployee() Project: updateProject(Project) Customer: updateCustomer(Customer) Void: removeEmployee(Long id) Void: removeProject(Long) .... Employee: findEmployee(Long id) Void: addEmployee(Employee ) Collection<Employee>: .... findEmployeesByName(String) Collection<Project>: getProjects(Long) ....RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 23Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  24. 24. Designing RESTful Services • Model the Employee URIs – /employees  Represent the employees collection in our systems – /employees/{id}  Represents a single employee in our system, identified by {id} – /employees/{id}/projects  Represents the collection of projects for the employee with id {id}RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 24Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  25. 25. Designing RESTful Services • Model the Projects URIs – /projects  Represent the projects collection in our systems – /projects/{id}  Represents a single project in our system, identified by {id} – /project/{id}/employees Represents the collection of employees for the project with id {id} – /project/{id}/customer Represents the customer for the project with id {id} – /project/{id}/documents Represents the collection of projects for the project with id {id} – /project/{id}/documents/{doc_id} Represents the documetnt identified by {doc_id} for the project with id {id}RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 25Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  26. 26. Designing RESTful Services • Model the Customer URIs – /customers Represent the customers collection in our systems – / customers /{id}  Represents a single customer in our system, identified by {id} – /customers/{id}/projects Represents the collection of projects for the customer with id {id}RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 26Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  27. 27. Designing RESTful Services • Defining data format – We will use XML and JSON to represent the resources managed by our system. – JSON stands for JavaScript Object Notation • Example of a Book Object represented in JSON: {“id”:”1223”, “name”:”book name”, “authors”: [{“name”:”Jack Welsh”}, {“name”:”Eric Smith”}]} – More about JSON @ http://www.json.org/RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 27Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  28. 28. Designing RESTful Services • Define the REST Service for Project resource Path in the URI Consumes JSON or XML @Path("/projects") @Consumes({ "application/json", "application/xml" }) @Produces({ "application/json", "application/xml" }) public class ProjectRestService { ... } Produces JSON or XMLRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 28Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  29. 29. Designing RESTful Services • Define the GET method for all Project resources GET http://host:port/projects should return a list of projects @Path("/projects") @Consumes({ "application/json", "application/xml" }) @Produces({ "application/json", "application/xml" }) public class ProjectRestService { @GET public Response getProjects() { ... } } HTTP/1.1 MethodRESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 29Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  30. 30. Designing RESTful Services • Define the GET method for a page of Project resources GET http://host:port/projects?page=10 &pageSize=10 should return a the page 10 with 10 projects@Path("/projects")@Consumes({ "application/json", "application/xml" })@Produces({ "application/json", "application/xml" })public class ProjectRestService { @GET public Response getProjects(@PathParam("page") int page, @PathParam("pageSize") int pageSize) { ... }} RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 30 Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  31. 31. Designing RESTful Services • Define the GET method for a give Project resource GET http://host:port/projects/1 should return the project identified by 1 @Path("/projects") @Consumes({ "application/json", "application/xml" }) @Produces({ "application/json", "application/xml" }) public class ProjectRestService { @GET @Path("/{id}") public Response findProject(@PathParam("id") Long id) { ... } }RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 31Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  32. 32. Designing RESTful Services • Define the POST method to create a Project resource POST http://host:port/projects with JSON or XML representation of a project in the body should create a new project @Path("/projects") @Consumes({ "application/json", "application/xml" }) @Produces({ "application/json", "application/xml" }) public class ProjectRestService { @POST public Response createProject(ProjectResource project) { ... } }RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 32Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  33. 33. Designing RESTful Services • Define the PUT method to update a Project resource PUT http://host:port/projects/1 with JSON or XML representation of a project in the body should should update the project with id 1 @Path("/projects") @Consumes({ "application/json", "application/xml" }) @Produces({ "application/json", "application/xml" }) public class ProjectRestService { @PUT @Path("/{id}") public Response updateProject(ProjectResource projectResource, @PathParam("id") Long id) { ... } }RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 33Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  34. 34. Designing RESTful Services • Define the DELETE method to delete a Project resource DELETE http://host:port/projects/1 should remove the project with id 1 @Path("/projects") @Consumes({ "application/json", "application/xml" }) @Produces({ "application/json", "application/xml" }) public class ProjectRestService { @DELETE @Path("/{id}") public Response removeProject(@PathParam("id") Long id) { ... } }RESTful Web Services by João Miguel Pereira is licensed under a Creative Commons 34Attribution-NonCommercial-ShareAlike 3.0 Unported License.

×