SlideShare a Scribd company logo
Sagara	
  Gunathunga	
  
Architect	
  -­‐	
  WSO2	
  
Deep	
  dive	
  into	
  JAX-­‐RS	
  
2	
  
	
  	
  REST	
  	
  
REpresentational State Transfer
“An architecture style of networked systems & uses
HTTP Protocol for data communication ”
3	
  
	
  	
  REST	
  	
  Fundamentals	
  	
  	
  
Resources with unique IDs
Standard methods
Multiple representations
Link Resources together
HTTP Content
(JSON/POX/TXT)
HTTP URL
Hyperlinks
HTTP Methods
4	
  
	
  	
  REST	
  	
  Fundamentals	
  	
  	
  
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
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
7	
  
Richardson	
  Maturity	
  Model	
  
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.
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
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
• 
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 	
  
12	
  
RMM	
  –	
  Level	
  3	
  	
  
•  Use Hyperlinks to relate (link) resources.
•  Also known as HATEOAS (Hypertext As The Engine Of Application
State).
13	
  
JAX-­‐WS	
  AnnotaGons	
  	
  
•  @Path
•  @GET
•  @PUT
•  @POST
•  @DELETE
•  @Produces
•  @Consumes
•  @PathParam
•  @QueryParam
•  @MatrixParam
•  @HeaderParam
•  @CookieParam
•  @FormParam
•  @DefaultValue
•  @Context
JAX-­‐RS	
  
JAX-RS Annotated Service
@Path("/hello”)
public class HelloWorldService {
@GET
@Path("/{user}")
public String hello(@PathParam("user") String user) {
}
}
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) {
}
}
JAX-­‐RS	
  parameter	
  types	
  
•  @PathParam
•  @QueryParam
•  @HeaderParam
•  @CookieParam
•  @FormParam
•  @MatrixParam
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
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
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
JAX-­‐RS	
  Header	
  parameters	
  
@GET
@Path("/hello")
public String hello(@HeaderParam("Referer") String referer) {
return "Hello World ” ;
}
GET /helloworldapp/hello http/1.1
Host localhost
Referer http://facebook/…….. Header value
•  Map HTTP header values to Java method
parameters.
•  Can be used to handle resource
versioning , pagination, cache etc.
21	
  
	
  Error	
  handling	
  	
  -­‐	
  Status	
  Codes	
  	
  	
  
Do not define your own error codes instead use HTTP status codes
as much as possible.
HTTP 200 - OK
HTTP 201 - Resource created
HTTP 204 - No content found
HTTP 30 4 - Not modified
HTTP 404 - Resource not found
HTTP 410 - Resource gone
HTTP 409 - Conflicts
HTTP 403 - Forbidden
HTTP 500 - Internal Server Error
HTTP 503 - Service Unavailable
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();
}
}
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();
}
}
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
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
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
27	
  
AuthenHcaHon	
  and	
  AuthorizaHon	
  	
  
Widely used approaches.
•  HTTP BasicAuth
•  OAuth2
28	
  
References	
  	
  	
  
•  https://github.com/sagara-gunathunga/jaxrs-java-colombo/
•  http://martinfowler.com/articles/richardsonMaturityModel.html
Contact	
  us	
  !	
  

More Related Content

PDF
JavaEE and RESTful development - WSO2 Colombo Meetup
PPTX
Apache Falcon DevOps
PDF
Building RESTful applications using Spring MVC
PDF
RESTful Web services using JAX-RS
PPTX
Restful web services with java
PPTX
Introduction to RESTful Webservices in JAVA
PDF
Request-Response Cycle of Ruby on Rails App
PDF
Spring Web Service, Spring Integration and Spring Batch
JavaEE and RESTful development - WSO2 Colombo Meetup
Apache Falcon DevOps
Building RESTful applications using Spring MVC
RESTful Web services using JAX-RS
Restful web services with java
Introduction to RESTful Webservices in JAVA
Request-Response Cycle of Ruby on Rails App
Spring Web Service, Spring Integration and Spring Batch

What's hot (20)

PPT
WebLogic Deployment Plan Example
PPTX
SQLSaturday Bulgaria : HA & DR with SQL Server AlwaysOn Availability Groups
PDF
Spring Web Services: SOAP vs. REST
PDF
Weblogic performance tuning2
PDF
Restful风格ž„web服务架构
PPTX
Always on in SQL Server 2012
PPTX
Rails Request & Middlewares
PPTX
AlwaysON Basics
PDF
Andrei shakirin rest_cxf
PDF
Native REST Web Services with Oracle 11g
PDF
Rails request & middlewares
PPTX
ASP.NET Mvc 4 web api
PPTX
Weblogic
PPTX
RESTful application with JAX-RS and how to expose and test them
PDF
Weblogic introduction
PDF
Lecture 2: Servlets
PDF
Weblogic security
PDF
Weblogic plug in
PDF
Oracle Web Logic server
PPTX
Sql server 2012 AlwaysOn
WebLogic Deployment Plan Example
SQLSaturday Bulgaria : HA & DR with SQL Server AlwaysOn Availability Groups
Spring Web Services: SOAP vs. REST
Weblogic performance tuning2
Restful风格ž„web服务架构
Always on in SQL Server 2012
Rails Request & Middlewares
AlwaysON Basics
Andrei shakirin rest_cxf
Native REST Web Services with Oracle 11g
Rails request & middlewares
ASP.NET Mvc 4 web api
Weblogic
RESTful application with JAX-RS and how to expose and test them
Weblogic introduction
Lecture 2: Servlets
Weblogic security
Weblogic plug in
Oracle Web Logic server
Sql server 2012 AlwaysOn
Ad

Viewers also liked (20)

PPTX
Scalableenterpriseapplicationswith jee7andbeyond
PDF
What’s new in Java SE, EE, ME, Embedded world & new Strategy
PDF
Server-Side Programming Primer
PDF
Regla del Boy Scout y la Oxidación del Software
PPTX
JAX-RS 2.0: RESTful Java on Steroids, by Aron Gupta
PPTX
JAX-RS 2.0 and OData
PDF
Gradle vs Maven
PDF
Gradle como alternativa a maven
PPT
Using Java to implement RESTful Web Services: JAX-RS
PDF
ODP
RESTful Web Services with JAX-RS
PPTX
Maven
PPTX
Maven (EN ESPANOL)
PPTX
Maven Overview
PDF
Java desde cero maven
PDF
JAX-RS 2.0: What’s New in JSR 339 ?
PPTX
An introduction to Maven
PPT
Demystifying Maven
POTX
Adopt-a-jsr Mar 1 2017 JAX-RS update
PDF
JAX RS 2.0 - OTN Bangalore 2013
Scalableenterpriseapplicationswith jee7andbeyond
What’s new in Java SE, EE, ME, Embedded world & new Strategy
Server-Side Programming Primer
Regla del Boy Scout y la Oxidación del Software
JAX-RS 2.0: RESTful Java on Steroids, by Aron Gupta
JAX-RS 2.0 and OData
Gradle vs Maven
Gradle como alternativa a maven
Using Java to implement RESTful Web Services: JAX-RS
RESTful Web Services with JAX-RS
Maven
Maven (EN ESPANOL)
Maven Overview
Java desde cero maven
JAX-RS 2.0: What’s New in JSR 339 ?
An introduction to Maven
Demystifying Maven
Adopt-a-jsr Mar 1 2017 JAX-RS update
JAX RS 2.0 - OTN Bangalore 2013
Ad

Similar to Java colombo-deep-dive-into-jax-rs (20)

PPTX
Restful webservice
PPTX
Restful webservices
PDF
JAX-RS Creating RESTFul services
PDF
Rest And Rails
PDF
Spark IT 2011 - Developing RESTful Web services with JAX-RS
PPTX
PPTX
Rest with Java EE 6 , Security , Backbone.js
PDF
JAX-RS JavaOne Hyderabad, India 2011
PPTX
XSEDE14 SciGaP-Apache Airavata Tutorial
PDF
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
PDF
REST API with CakePHP
PPT
Developing RESTful WebServices using Jersey
PPTX
StackMate - CloudFormation for CloudStack
PDF
Webinar: What's new in CDAP 3.5?
PPT
nodejs_at_a_glance.ppt
PPTX
Ppt on web development and this has all details
PPT
nodejs_at_a_glance, understanding java script
PPTX
Spring Boot and REST API
PPTX
Java SE 8 - New Features
PPTX
Apache Ambari BOF - APIs - Hadoop Summit 2013
Restful webservice
Restful webservices
JAX-RS Creating RESTFul services
Rest And Rails
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Rest with Java EE 6 , Security , Backbone.js
JAX-RS JavaOne Hyderabad, India 2011
XSEDE14 SciGaP-Apache Airavata Tutorial
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
REST API with CakePHP
Developing RESTful WebServices using Jersey
StackMate - CloudFormation for CloudStack
Webinar: What's new in CDAP 3.5?
nodejs_at_a_glance.ppt
Ppt on web development and this has all details
nodejs_at_a_glance, understanding java script
Spring Boot and REST API
Java SE 8 - New Features
Apache Ambari BOF - APIs - Hadoop Summit 2013

More from Sagara Gunathunga (20)

PPTX
Microservices Security landscape
PPTX
Privacy by Design as a system design strategy - EIC 2019
PPTX
Consumer Identity World EU - Five pillars of consumer IAM
PPTX
kicking your enterprise security up a notch with adaptive authentication sa...
PPTX
Synergies across APIs and IAM
PPTX
GDPR impact on Consumer Identity and Access Management (CIAM)
PPTX
Introduction to the All New WSO2 Governance Centre
PPTX
Building Services with WSO2 Application Server and WSO2 Microservices Framewo...
PPTX
An Introduction to WSO2 Microservices Framework for Java
PPTX
Understanding Microservice Architecture WSO2Con Asia 2016
PDF
Introduction to the all new wso2 governance centre asia 16
PDF
Building Your Own Store with WSO2 Enterprise Store: The WSO2 Store Case Study
PDF
Introduction to the All New WSO2 Governance Centre
POTX
Application Monitoring with WSO2 App Server
PDF
WSO2 Application Server
PDF
Creating APIs with the WSO2 Platform
PDF
WSO2 AppDev platform
PDF
Apache contribution-bar camp-colombo
PDF
What is new in Axis2 1.7.0
PPTX
Web service introduction 2
Microservices Security landscape
Privacy by Design as a system design strategy - EIC 2019
Consumer Identity World EU - Five pillars of consumer IAM
kicking your enterprise security up a notch with adaptive authentication sa...
Synergies across APIs and IAM
GDPR impact on Consumer Identity and Access Management (CIAM)
Introduction to the All New WSO2 Governance Centre
Building Services with WSO2 Application Server and WSO2 Microservices Framewo...
An Introduction to WSO2 Microservices Framework for Java
Understanding Microservice Architecture WSO2Con Asia 2016
Introduction to the all new wso2 governance centre asia 16
Building Your Own Store with WSO2 Enterprise Store: The WSO2 Store Case Study
Introduction to the All New WSO2 Governance Centre
Application Monitoring with WSO2 App Server
WSO2 Application Server
Creating APIs with the WSO2 Platform
WSO2 AppDev platform
Apache contribution-bar camp-colombo
What is new in Axis2 1.7.0
Web service introduction 2

Recently uploaded (20)

DOCX
The story of the first moon landing.docx
PDF
Quality Control Management for RMG, Level- 4, Certificate
PPTX
6- Architecture design complete (1).pptx
PPTX
Special finishes, classification and types, explanation
PPT
unit 1 ppt.ppthhhhhhhhhhhhhhhhhhhhhhhhhh
PPTX
Fundamental Principles of Visual Graphic Design.pptx
PDF
Urban Design Final Project-Context
PPTX
building Planning Overview for step wise design.pptx
PDF
BRANDBOOK-Presidential Award Scheme-Kenya-2023
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PPTX
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
PPT
Machine printing techniques and plangi dyeing
PPTX
Wisp Textiles: Where Comfort Meets Everyday Style
PDF
Interior Structure and Construction A1 NGYANQI
DOCX
actividad 20% informatica microsoft project
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PDF
High-frequency high-voltage transformer outline drawing
PPTX
Implications Existing phase plan and its feasibility.pptx
PDF
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
PPT
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
The story of the first moon landing.docx
Quality Control Management for RMG, Level- 4, Certificate
6- Architecture design complete (1).pptx
Special finishes, classification and types, explanation
unit 1 ppt.ppthhhhhhhhhhhhhhhhhhhhhhhhhh
Fundamental Principles of Visual Graphic Design.pptx
Urban Design Final Project-Context
building Planning Overview for step wise design.pptx
BRANDBOOK-Presidential Award Scheme-Kenya-2023
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
Machine printing techniques and plangi dyeing
Wisp Textiles: Where Comfort Meets Everyday Style
Interior Structure and Construction A1 NGYANQI
actividad 20% informatica microsoft project
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
High-frequency high-voltage transformer outline drawing
Implications Existing phase plan and its feasibility.pptx
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt

Java colombo-deep-dive-into-jax-rs

  • 1. Sagara  Gunathunga   Architect  -­‐  WSO2   Deep  dive  into  JAX-­‐RS  
  • 2. 2      REST     REpresentational State Transfer “An architecture style of networked systems & uses HTTP Protocol for data communication ”
  • 3. 3      REST    Fundamentals       Resources with unique IDs Standard methods Multiple representations Link Resources together HTTP Content (JSON/POX/TXT) HTTP URL Hyperlinks HTTP Methods
  • 4. 4      REST    Fundamentals      
  • 5. 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
  • 6. 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
  • 8. 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.
  • 9. 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
  • 10. 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 • 
  • 11. 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  
  • 12. 12   RMM  –  Level  3     •  Use Hyperlinks to relate (link) resources. •  Also known as HATEOAS (Hypertext As The Engine Of Application State).
  • 13. 13   JAX-­‐WS  AnnotaGons     •  @Path •  @GET •  @PUT •  @POST •  @DELETE •  @Produces •  @Consumes •  @PathParam •  @QueryParam •  @MatrixParam •  @HeaderParam •  @CookieParam •  @FormParam •  @DefaultValue •  @Context
  • 14. JAX-­‐RS   JAX-RS Annotated Service @Path("/hello”) public class HelloWorldService { @GET @Path("/{user}") public String hello(@PathParam("user") String user) { } }
  • 15. 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) { } }
  • 16. JAX-­‐RS  parameter  types   •  @PathParam •  @QueryParam •  @HeaderParam •  @CookieParam •  @FormParam •  @MatrixParam
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. JAX-­‐RS  Header  parameters   @GET @Path("/hello") public String hello(@HeaderParam("Referer") String referer) { return "Hello World ” ; } GET /helloworldapp/hello http/1.1 Host localhost Referer http://facebook/…….. Header value •  Map HTTP header values to Java method parameters. •  Can be used to handle resource versioning , pagination, cache etc.
  • 21. 21    Error  handling    -­‐  Status  Codes       Do not define your own error codes instead use HTTP status codes as much as possible. HTTP 200 - OK HTTP 201 - Resource created HTTP 204 - No content found HTTP 30 4 - Not modified HTTP 404 - Resource not found HTTP 410 - Resource gone HTTP 409 - Conflicts HTTP 403 - Forbidden HTTP 500 - Internal Server Error HTTP 503 - Service Unavailable
  • 22. 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(); } }
  • 23. 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(); } }
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 27   AuthenHcaHon  and  AuthorizaHon     Widely used approaches. •  HTTP BasicAuth •  OAuth2
  • 28. 28   References       •  https://github.com/sagara-gunathunga/jaxrs-java-colombo/ •  http://martinfowler.com/articles/richardsonMaturityModel.html