java colombo-deep-dive-into-jax-rs
TRANSCRIPT
![Page 1: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/1.jpg)
Sagara Gunathunga Architect -‐ WSO2
Deep dive into JAX-‐RS
![Page 2: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/2.jpg)
2
REST
REpresentational State Transfer
“An architecture style of networked systems & uses HTTP Protocol for data communication ”
![Page 3: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/3.jpg)
3
REST Fundamentals
Resources with unique IDs
Standard methods
Multiple representations
Link Resources together
HTTP Content (JSON/POX/TXT)
HTTP URL
Hyperlinks
HTTP Methods
![Page 4: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/4.jpg)
4
REST Fundamentals
![Page 5: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/5.jpg)
5
REST Fundamentals • Client-Server
• Separation principle • Components Independent
• Stateless • Session state on the client • Visibility, reliability and scalability • Trade off (network performance, etc.)
• Cacheable • A response can be cacheable • Efficiency but reduce reliability
• Layered system • System scalability
![Page 6: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/6.jpg)
6
JAX-‐RS – Java API for RESTfull WebServices
• Annotation driven JavaEE API
• Expose annotated Java bean as HTTP based services • Implementation may support service descriptions. • Swagger • RAML • WADL
• Define JAVA ó HTTP mapping • Number of implementations • Apache CXF • Apache Wink • Jersey
• Also used by Microservices frameworks to define services • Dropwizard • Spring Boot
![Page 7: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/7.jpg)
7
Richardson Maturity Model
![Page 8: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/8.jpg)
8
RMM – Level 0
• HTTP as a transport system for remote interactions.
• Same as SOAP or XML-RPC it's basically the same mechanism.
• Most of the cases only one endpoint is exposed for whole service/application.
![Page 9: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/9.jpg)
9
RMM – Level 1
• For each web resource there is a unique identifiable Id. • http://school.com/api/students => All student collection • Http://scholl.com/api/student/2345 => A unique student
![Page 10: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/10.jpg)
10
RMM – Level 2
• Map HTTP Methods to operations (state transition) on resources. • GET Http://scholl.com/api/student/2345 • PUT Http://scholl.com/api/student/2345 • DELETE Http://scholl.com/api/student/2345
• Do not overload HTTP POST/GET and define WS like operations.
• Do not use “verbs/actions” as a part of resource URL. • E.g – createStudent, editStudent
•
![Page 11: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/11.jpg)
11
HTTP Method CRUD Desc.
POST CREATE Create -
GET RETRIEVE Retrieve Safe,Idempotent,Cacheable
PUT UPDATE Update Idempotent
DELETE DELETE Delete Idempotent
REST HTTP Methods
![Page 12: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/12.jpg)
12
RMM – Level 3
• Use Hyperlinks to relate (link) resources.
• Also known as HATEOAS (Hypertext As The Engine Of Application State).
![Page 13: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/13.jpg)
13
JAX-‐WS AnnotaGons • @Path
• @GET
• @PUT
• @POST
• @DELETE
• @Produces
• @Consumes
• @PathParam
• @QueryParam
• @MatrixParam
• @HeaderParam
• @CookieParam
• @FormParam
• @DefaultValue
• @Context
![Page 14: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/14.jpg)
JAX-‐RS
JAX-RS Annotated Service
@Path("/hello”)public class HelloWorldService {
@GET @Path("/{user}") public String hello(@PathParam("user") String user) {
} }
![Page 15: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/15.jpg)
JAX-‐RS
JAX-RS Annotated Service
@Path("/hello”)public class HelloWorldService {
@GET @Consumes("text/plain") @Produces("text/xml") @Path("/{user}") public String hello(@PathParam("user") String user) {
} }
![Page 16: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/16.jpg)
JAX-‐RS parameter types • @PathParam
• @QueryParam • @HeaderParam
• @CookieParam
• @FormParam
• @MatrixParam
![Page 17: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/17.jpg)
JAX-‐RS Path parameters @GET @Path("/hello/{user}") public String hello(@PathParam("user") String user) { return "Hello " + user; }
http://localhost:8080/helloworldapp/hello/sagara
PathParam “user” value
• URL template style • Generally used to represent compulsory
user inputs
![Page 18: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/18.jpg)
JAX-‐RS OpGonal Path parameters @GET @Path("/hello{user: (/user)?}") public String hello(@PathParam("user") String user) { return "Hello " + user; }
http://localhost:8080/helloworldapp/hello/sagara
PathParam “user” value
• Important in backward compatible API designs.
• Generally incorporate with @DefaultValue annotation
http://localhost:8080/helloworldapp/hello/
No PathParam “user” value
![Page 19: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/19.jpg)
http://localhost:8080/helloworldapp/hello
JAX-‐RS Query parameters @GET @Path("/hello") public String hello(@QueryParam (”name") String user) { return "Hello " + user; }
http://localhost:8080/helloworldapp/hello?name=sagara
PathParam “user” value• Query string parameter style
• Generally used to represent optional user inputs or controller operations.
• Generally incorporate with @DefaultValue annotation
![Page 20: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/20.jpg)
JAX-‐RS Header parameters @GET @Path("/hello") public String hello(@HeaderParam("Referer") String referer) { return "Hello World ” ; }
GET /helloworldapp/hello http/1.1Host localhost Referer http://facebook/…….. Header value
• Map HTTP header values to Java method parameters.
• Can be used to handle resource versioning , pagination, cache etc.
![Page 21: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/21.jpg)
21
Error handling -‐ Status Codes Do not define your own error codes instead use HTTP status codes as much as possible.
HTTP 200 - OKHTTP 201 - Resource createdHTTP 204 - No content foundHTTP 30 4 - Not modified
HTTP 404 - Resource not found HTTP 410 - Resource goneHTTP 409 - Conflicts HTTP 403 - Forbidden
HTTP 500 - Internal Server Error HTTP 503 - Service Unavailable
![Page 22: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/22.jpg)
22
Error handling -‐ Status Codes Do not define your own error codes instead use HTTP status codes as much as possible.
public Response findAll() { try { return Response.status(Response.Status.ACCEPTED)
.entity(users.findAll()).build(); } catch (UserDAOException e) { return Response.
status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }
![Page 23: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/23.jpg)
23
Error handling -‐ ExcepGonMapper Instead of scatter error handling code in resource code possible to define .
class UserExceptionMapper implements ExceptionMapper<UserDAOException> {
public Response toResponse(UserDAOException exception) {
if (exception instanceof UserNotFoundException) { return Response.status(Response.Status.NOT_FOUND).build();
} else { return Response .status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }
![Page 24: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/24.jpg)
24
Resource Linking Use hyper links properly to link related resources.
Account can be in many
groups
Link to related group
Links to group members
![Page 25: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/25.jpg)
25
JAX-‐RS PaginaHon Widely used approaches. • LimitOffset based pagination
• Limit – Define the size of the page • Offset – Define the starting point of each page.
• Curser based pagination
• Limit – Define the size of the page • Before –
Cursor that points to the start of the page of data that has been returned
• After - cursor that points to the end of the page of data that has been returned
• Time based pagination • Limit – Define the size of the page • Until – points to the end of the range of time-based data • Since – points to the startof the range of time-based data
![Page 26: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/26.jpg)
26
JAX-‐RS LimitOffset based PaginaHon @GET@Path("/users")@Produces(MediaType.APPLICATION_JSON)public Response findAll(
@QueryParam("offset") @DefaultValue("0") int offset, @QueryParam("limit") @DefaultValue("10") int limit,
@Context UriInfo uriInfo) { }
URI currentLink = UriBuilder.fromUri(uriInfo.getBaseUri()).path("/page/users")
.queryParam("offset", offset) .queryParam("limit", limit) .build();
Creating Links
![Page 27: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/27.jpg)
27
AuthenHcaHon and AuthorizaHon Widely used approaches. • HTTP BasicAuth • OAuth2
![Page 28: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/28.jpg)
28
References
• https://github.com/sagara-gunathunga/jaxrs-java-colombo/
• http://martinfowler.com/articles/richardsonMaturityModel.html
![Page 29: Java colombo-deep-dive-into-jax-rs](https://reader031.vdocument.in/reader031/viewer/2022022413/58ed9a9b1a28ab75278b4667/html5/thumbnails/29.jpg)
Contact us !