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

What's hot (20)

PPT
WebLogic Deployment Plan Example
James Bayer
 
PPTX
SQLSaturday Bulgaria : HA & DR with SQL Server AlwaysOn Availability Groups
turgaysahtiyan
 
PDF
Spring Web Services: SOAP vs. REST
Sam Brannen
 
PDF
Weblogic performance tuning2
Aditya Bhuyan
 
PDF
Restful风格ž„web服务架构
Benjamin Tan
 
PPTX
Always on in SQL Server 2012
Fadi Abdulwahab
 
PPTX
Rails Request & Middlewares
Santosh Wadghule
 
PPTX
AlwaysON Basics
Harsh Chawla
 
PDF
Andrei shakirin rest_cxf
Aravindharamanan S
 
PDF
Native REST Web Services with Oracle 11g
Marcelo Ochoa
 
PDF
Rails request & middlewares
Santosh Wadghule
 
PPTX
ASP.NET Mvc 4 web api
Tiago Knoch
 
PPTX
Weblogic
sudeeporcl
 
PPTX
RESTful application with JAX-RS and how to expose and test them
Kumaraswamy M
 
PDF
Weblogic introduction
Aditya Bhuyan
 
PDF
Lecture 2: Servlets
Fahad Golra
 
PDF
Weblogic security
Aditya Bhuyan
 
PDF
Weblogic plug in
Aditya Bhuyan
 
PDF
Oracle Web Logic server
Rakesh Gujjarlapudi
 
PPTX
Sql server 2012 AlwaysOn
Warwick Rudd
 
WebLogic Deployment Plan Example
James Bayer
 
SQLSaturday Bulgaria : HA & DR with SQL Server AlwaysOn Availability Groups
turgaysahtiyan
 
Spring Web Services: SOAP vs. REST
Sam Brannen
 
Weblogic performance tuning2
Aditya Bhuyan
 
Restful风格ž„web服务架构
Benjamin Tan
 
Always on in SQL Server 2012
Fadi Abdulwahab
 
Rails Request & Middlewares
Santosh Wadghule
 
AlwaysON Basics
Harsh Chawla
 
Andrei shakirin rest_cxf
Aravindharamanan S
 
Native REST Web Services with Oracle 11g
Marcelo Ochoa
 
Rails request & middlewares
Santosh Wadghule
 
ASP.NET Mvc 4 web api
Tiago Knoch
 
Weblogic
sudeeporcl
 
RESTful application with JAX-RS and how to expose and test them
Kumaraswamy M
 
Weblogic introduction
Aditya Bhuyan
 
Lecture 2: Servlets
Fahad Golra
 
Weblogic security
Aditya Bhuyan
 
Weblogic plug in
Aditya Bhuyan
 
Oracle Web Logic server
Rakesh Gujjarlapudi
 
Sql server 2012 AlwaysOn
Warwick Rudd
 

Viewers also liked (20)

PPTX
Scalableenterpriseapplicationswith jee7andbeyond
Andy Moncsek
 
PDF
What’s new in Java SE, EE, ME, Embedded world & new Strategy
Mohamed Taman
 
PDF
Server-Side Programming Primer
Ivano Malavolta
 
PDF
Regla del Boy Scout y la Oxidación del Software
Alejandro Pérez García
 
PPTX
JAX-RS 2.0: RESTful Java on Steroids, by Aron Gupta
Codemotion
 
PPTX
JAX-RS 2.0 and OData
Anil Allewar
 
PDF
Gradle vs Maven
Mario García
 
PDF
Gradle como alternativa a maven
David Gómez García
 
PPT
Using Java to implement RESTful Web Services: JAX-RS
Katrien Verbert
 
PDF
Maven
Luis Bertel
 
ODP
RESTful Web Services with JAX-RS
Carol McDonald
 
PPTX
Maven
Khan625
 
PPTX
Maven (EN ESPANOL)
Rodrigo Branas
 
PPTX
Maven Overview
zerovirus23
 
PDF
Java desde cero maven
www.mentoringit.com.mx
 
PDF
JAX-RS 2.0: What’s New in JSR 339 ?
Arun Gupta
 
PPTX
An introduction to Maven
Joao Pereira
 
PPT
Demystifying Maven
Mike Desjardins
 
POTX
Adopt-a-jsr Mar 1 2017 JAX-RS update
Pavel Bucek
 
PDF
JAX RS 2.0 - OTN Bangalore 2013
Jagadish Prasath
 
Scalableenterpriseapplicationswith jee7andbeyond
Andy Moncsek
 
What’s new in Java SE, EE, ME, Embedded world & new Strategy
Mohamed Taman
 
Server-Side Programming Primer
Ivano Malavolta
 
Regla del Boy Scout y la Oxidación del Software
Alejandro Pérez García
 
JAX-RS 2.0: RESTful Java on Steroids, by Aron Gupta
Codemotion
 
JAX-RS 2.0 and OData
Anil Allewar
 
Gradle vs Maven
Mario García
 
Gradle como alternativa a maven
David Gómez García
 
Using Java to implement RESTful Web Services: JAX-RS
Katrien Verbert
 
RESTful Web Services with JAX-RS
Carol McDonald
 
Maven
Khan625
 
Maven (EN ESPANOL)
Rodrigo Branas
 
Maven Overview
zerovirus23
 
Java desde cero maven
www.mentoringit.com.mx
 
JAX-RS 2.0: What’s New in JSR 339 ?
Arun Gupta
 
An introduction to Maven
Joao Pereira
 
Demystifying Maven
Mike Desjardins
 
Adopt-a-jsr Mar 1 2017 JAX-RS update
Pavel Bucek
 
JAX RS 2.0 - OTN Bangalore 2013
Jagadish Prasath
 
Ad

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

PPTX
6 Months Industrial Training in Spring Framework
Arcadian Learning
 
PPTX
Building Restful Web Services with Java
Vassil Popovski
 
PPTX
Ppt on web development and this has all details
gogijoshiajmer
 
PDF
RESTful Java With JAX RS 1st Edition Bill Burke
jolokmertah
 
PPTX
RESTful Web Services
Martin Necasky
 
PDF
RESTful Java With JAX RS 1st Edition Bill Burke
rohismhmob88
 
PDF
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Arun Gupta
 
PPTX
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
PPTX
Rest
Carol McDonald
 
PPTX
Overview of RESTful web services
nbuddharaju
 
PDF
JAX-RS JavaOne Hyderabad, India 2011
Shreedhar Ganapathy
 
PPTX
Restful webservice
Dong Ngoc
 
PPTX
Real world RESTful service development problems and solutions
Bhakti Mehta
 
PPTX
RESTful Web Services
Gordon Dickens
 
PDF
Rest web services
Paulo Gandra de Sousa
 
PPTX
JAX-RS. Developing RESTful APIs with Java
Jerry Kurian
 
PPTX
Rest in practice
Gabor Torok
 
PPTX
Restful webservices
Luqman Shareef
 
PDF
Rest web service
Hamid Ghorbani
 
PDF
S313265 - Advanced Java API for RESTful Web Services at JavaOne Brazil 2010
Arun Gupta
 
6 Months Industrial Training in Spring Framework
Arcadian Learning
 
Building Restful Web Services with Java
Vassil Popovski
 
Ppt on web development and this has all details
gogijoshiajmer
 
RESTful Java With JAX RS 1st Edition Bill Burke
jolokmertah
 
RESTful Web Services
Martin Necasky
 
RESTful Java With JAX RS 1st Edition Bill Burke
rohismhmob88
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Arun Gupta
 
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
Overview of RESTful web services
nbuddharaju
 
JAX-RS JavaOne Hyderabad, India 2011
Shreedhar Ganapathy
 
Restful webservice
Dong Ngoc
 
Real world RESTful service development problems and solutions
Bhakti Mehta
 
RESTful Web Services
Gordon Dickens
 
Rest web services
Paulo Gandra de Sousa
 
JAX-RS. Developing RESTful APIs with Java
Jerry Kurian
 
Rest in practice
Gabor Torok
 
Restful webservices
Luqman Shareef
 
Rest web service
Hamid Ghorbani
 
S313265 - Advanced Java API for RESTful Web Services at JavaOne Brazil 2010
Arun Gupta
 
Ad

More from Sagara Gunathunga (20)

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

Recently uploaded (20)

PPTX
Colorful Bold Safari Animals Presentation.pptx
HNgcTrAnh
 
PDF
Presentation - Interior Design Concepts (2).pdf
vrindagrawal456
 
DOCX
CERT HERNANDEZ CHURCH PHILIPPIBNES .docx
michael patino
 
PPTX
一比一原版(UOIT毕业证)安省理工大学毕业证如何办理
Taqyea
 
PPTX
Great_Discoverers_Nohzyxyxyzyzyzyzzyz_Imagptx
kevadiyahem07
 
PDF
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
PDF
S2 Associates brings museum exhibits to life with innovative design.pdf
S2 Associates
 
PPTX
sistem teknologi yang di desain untuk mahasiswa dan dosen agar memudahkan mer...
gamesonlya2rj
 
PDF
Uber Driver Hackday Sprint Solving Ride Cancellations
YellowSlice1
 
PPTX
condylar pptx.in relation to dental seurgery
abishekgowtham586
 
PDF
Empowering Artisans Through Technology Karmakar App Concept
yellowslice2
 
PPTX
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
PPTX
Chapter 1-1.pptx hwhahaiaiautsfzjakaiwueysuua
hibaaqabdirisaaq331
 
PPTX
hall ppt 1 it for basic tamolet .pptx
ashishbehera64
 
PPTX
the very teaching plan extra ordinary.pptx
PamelaOdibeli1
 
PDF
Plastic Foam as eco-friendly product in interiors
Disha Agrawal
 
PDF
SS27 Men's Fashion Trend Book Peclers Paris
Peclers Paris
 
PDF
ARC-101-B-4.pdfxxxxxxxxxxxxxxxxxxxxxxxxx
IzzyBaniquedBusto
 
PPTX
Pitch_template_ppt_for_generation volunteer2024 .pptx
rinjanithiara99
 
DOCX
Redefining Master Plans for creating sustainable cities-Jharkhand Conference...
JIT KUMAR GUPTA
 
Colorful Bold Safari Animals Presentation.pptx
HNgcTrAnh
 
Presentation - Interior Design Concepts (2).pdf
vrindagrawal456
 
CERT HERNANDEZ CHURCH PHILIPPIBNES .docx
michael patino
 
一比一原版(UOIT毕业证)安省理工大学毕业证如何办理
Taqyea
 
Great_Discoverers_Nohzyxyxyzyzyzyzzyz_Imagptx
kevadiyahem07
 
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
S2 Associates brings museum exhibits to life with innovative design.pdf
S2 Associates
 
sistem teknologi yang di desain untuk mahasiswa dan dosen agar memudahkan mer...
gamesonlya2rj
 
Uber Driver Hackday Sprint Solving Ride Cancellations
YellowSlice1
 
condylar pptx.in relation to dental seurgery
abishekgowtham586
 
Empowering Artisans Through Technology Karmakar App Concept
yellowslice2
 
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
Chapter 1-1.pptx hwhahaiaiautsfzjakaiwueysuua
hibaaqabdirisaaq331
 
hall ppt 1 it for basic tamolet .pptx
ashishbehera64
 
the very teaching plan extra ordinary.pptx
PamelaOdibeli1
 
Plastic Foam as eco-friendly product in interiors
Disha Agrawal
 
SS27 Men's Fashion Trend Book Peclers Paris
Peclers Paris
 
ARC-101-B-4.pdfxxxxxxxxxxxxxxxxxxxxxxxxx
IzzyBaniquedBusto
 
Pitch_template_ppt_for_generation volunteer2024 .pptx
rinjanithiara99
 
Redefining Master Plans for creating sustainable cities-Jharkhand Conference...
JIT KUMAR GUPTA
 

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