CNIC Information System with Pakdata Cf In Pakistan
Iasi code camp 12 october 2013 jax-rs-jee-ecosystem - catalin mihalache
1.
2. 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
3. Agenda
• What is JAX-RS ?
• The use case
• JAX-RS resources
• Providers
• Handling exceptions
• Customizing the response
• More JAX-RS annotations!
• Q&A
4. 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 ..)
6. 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!!)
7. 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
8. 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
9. 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
10. 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>
{ ... }
11. Customizing the response
Case study – caveatEmptor-rs project
javax.ws.rs.core.Response
• Redirects
• Controlling the cache headers
•
12. 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