Iasi code camp 12 october 2013 jax-rs-jee-ecosystem - catalin mihalache

713 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
713
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Iasi code camp 12 october 2013 jax-rs-jee-ecosystem - catalin mihalache

  1. 1. JAX-RS - REST web services in the JEE ecosystem Catalin Mihalache Java Architect / Project Manager at Pentalog Oracle Certified Master, Java EE 5 Enterprise Architect the examples: https://www.dropbox.com/s/z1cj30b9y7pznao/samples.zip
  2. 2. Agenda • What is JAX-RS ? • The use case • JAX-RS resources • Providers • Handling exceptions • Customizing the response • More JAX-RS annotations! • Q&A
  3. 3. What is JAX-RS ? RESTful web services in JEE • JSR 311 – a JEE6 specification Available in JEE6 Application Servers: like JBoss6.x, JBoss7.x, Glassfish 3.x, IBM WebSphere 8.x, Apache TomEE Plus • JSR 339 – a JEE7 specification Available in JEE7 Application Servers: Glassfish 4.x (waiting for JBoss8 & WebSphere9 ..) • Open source implementations JBoss RestEasy Glassfish Jersey – the Reference Implementation Apache CXF Apache Wink • Spring MVC (the dark side of the Force ..)
  4. 4. Used samples https://www.dropbox.com/s/z1cj30b9y7pznao/samples.zip
  5. 5. The use case - caveatEmptor • TODO: an HTTP-based API able to manage the domain model and to be easily consumed by JavaScript-based applications (yeah, hire a professional JS developer and let me do my job: JAVA!!)
  6. 6. JAX-RS resources (1) • The URLs: – http://www.caveatemptor.com/rest/retrieveAllCategories.do http://www.caveatemptor.com/rest/retrieveCategory.do?id=13 http://www.caveatemptor.com/rest/retrieveItems.do?categId=13 – unique & pretty URLs for each object / collection from the domain model: http://www.caveatemptor.com/rest/categories http://www.caveatemptor.com/rest/categories/13 http://www.caveatemptor.com/rest/categories/13/items • The HTTP methods: • HTTP error codes - GET, POST, PUT, DELETE http://en.wikipedia.org/wiki/List_of_HTTP_status_codes – 2xx Success – 3xx Redirection – 4xx Client Error (401 Unauthorized, 404 Not found, 400 Bad request) – 5xx Server Error (well ..) • XML &&|| JSON
  7. 7. JAX-RS resources (2) Case study – the simpleJaxRs project • @Path, @PathParam, @ApplicationPath and javax.ws.rs.core.Application http://localhost:8080/simpleJaxRs/rs/categories http://localhost:8080/simpleJaxRs/rs/categories/1 /simpleJaxRs /rs /categories /categories/1 – – – – servlet context name JAX-RS front-controller instance of CategoriesResource instance of CategoryResource with id = 1 (no XML configuration, just tons of Java annotations ..) • XML & JSON; JAXB – – ● (Java annotations everywhere ..) Accept → @Produces Content-Type → @Consumes CRUD operations on same URL @POST, @GET, @PUT, @DETELE
  8. 8. Providers Case study – simpleJaxRs2 project producing / consuming JSON without JAXB • JEE6: No standard JSON – JAX-RS integration! available in JEE7 • Converting Java objects into “something” (like JSON) & vice-versa: javax.ws.rs.ext.MessageBodyWriter javax.ws.rs.ext.MessageBodyReader • Jackson - JAX-RS integration – producing / consuming JSON @Provider @Consumes({MediaType.APPLICATION_JSON, "text/json"}) @Produces({MediaType.APPLICATION_JSON, "text/json"}) public class JacksonJsonProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object> { ... } • @Provider - “provides” automatic registration & usage
  9. 9. Handling exceptions Case study – caveatEmptor-rs project • Managing Checked Exceptions (business exception) javax.ws.rs.WebApplicationException HTTP error code: 4xx • Managing Unchecked Exceptions javax.ws.rs.ext.ExceptionMapper HTTP error code: 5xx (internal server error) @Provider public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> { ... }
  10. 10. Customizing the response Case study – caveatEmptor-rs project javax.ws.rs.core.Response • Redirects • Controlling the cache headers •
  11. 11. More JAX-RS annotations! • @javax.ws.rs.core.Context – javax.ws.rs.core.UriInfo ● – javax.ws.rs.core.HttpHeaders ● – An injectable interface that provides access to application and request URI information. An injectable interface that provides access to HTTP header information. javax.ws.rs.core.SecurityContext ● An injectable interface that provides access to security related information. • @QueryParam – injects a query string parameters, just as @PathParam does • @FormParam – injects a HTML form input's value, just as @PathParam does
  12. 12. A Q&A
  13. 13. Please fill in your evaluation form [JAX-RS - REST web services in the JEE ecosystem] Catalin Mihalache [12th of October 2013

×