SlideShare ist ein Scribd-Unternehmen logo
1 von 36
RESTing with JAX-RS
OKAFOR EZEWUZIE
@dawuzi
About Me
1. Software Engineer at Seamfix Nigeria (www.seamfix.com)
2. JavaEE Enthusiast
What is REST (Representational State
Transfer)
1. Web standards based architecture targeted at distributed hypermedia
systems such as the World Wide Web
2. Introduced in the doctoral dissertation of Roy Fielding in 2000 (One of
the principal authors of the HTTP specification)
3. Describes how resources are defined and accessed
4. Makes use of several web standards (HTTP, URI, Resource
Representations etc)
Some REST Principles
1. All identifiable resources should have an ID
2. Use links to refer to those resources
3. Resources should be accessible via standard (HTTP) methods eg
POST, GET, PUT,
4. Resources should have multiple representations
5. Stateless communication (Err not really. Just that state should be at
the client side)
Brief Intro to HTTP Methods
1. GET : Used for requesting for resources (Safe to call i.e should not
modify any resource)
2. POST: Used for creating resource
3. PUT: Used for updating resources (Idempotent )
4. DELETE: Used for deleting resources (Idempotent)
Top REST Java frameworks
1. Dropwizard
2. Play Framework
3. RESTEasy
4. Restlet
5. Spark Framework
6. Spring Boot
JAX-RS Introduction
1. Java API for RESTful Web Services (JAX-RS)
2. Java spec that aids creating Web services based on the REST
architecture
3. Java annotations based
JAX-RS Brief History
1. JAX-RS 1.0 in 2008
2. JAX-RS 1.1 in 2009. Added to the JavaEE 6.
3. JAX-RS 2.0 in 2014. JavaEE 7. (Client API, Bean validation, Async
Processing)
4. JAX-RS 2.1 in Sept 2017. JavaEE 8. (Non-blocking IO, Reactive
clients, Server Sent Events)
Common JAX-RS Annotations
● @ApplicationPath : Identifies the application path that serves as the
base URI for all resource URIs.
● @Path
● @GET, @POST, @PUT, @DELETE. This maps to the
corresponding HTTP methods
● @Produces (used to specify the MIME media types or
representations a resource)
Common JAX-RS Annotations (Continued)
● @Consumes (used to specify which MIME media types of
representations a resource can accept)
● @*Param eg @PathParam, @HeaderParam, @CookieParam,
@QueryParam @BeanParam (used for retrieving request
parameters)
● @Context : Used for injecting helper classes and informational
Objects e.g URI information, SecurityContext, HTTPHeaders
● @Provider: An injectable interface providing runtime lookup of
provider instances
Sample JAX-RS application for JUG
Attendees
Simple Maven Project with two dependencies. JavaEE API and Lombok
(to avoid a lot of boilerplate code)
1. A subclass of Application annotated with @ApplicationPath
2. A resource class annotated with @Path
3. A mock database class
https://github.com/dawuzi/jax-rs-attendant-sample
The javax.ws.rs.core.Response Class
1. Response class used for building more complex responses
2. Instances can be obtained from
javax.ws.rs.core.Response.ResponseBuilder
3. Instances of ResponseBuilder can be obtained from static helper
methods in the Response class itself
Exception Handling
1. Applications can throw javax.ws.rs.WebApplicationException to have
the container return specific HTTP codes to the client
2. The response can be customised for other exceptions by
implementing the ExceptionMapper. The implementing class can then
be annotated with @Provider or added to the list of classes in the
Application sub class
ExceptionMapper Interface
public interface ExceptionMapper<E extends Throwable> {
Response toResponse(Exception e);
}
JAX-RS Client API
1. Introduced in JAX-RS 2.0
2. Enabled writing portable client REST call with a fluent API
Client API Sample
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://www.google.com");
Response response = target.queryParam("start", "10").request().cookie("test-cookie", "test")
.header("test-header", "test").accept(MediaType.APPLICATION_JSON)
.get();
client.close();
Client API Sample (Continued)
Client client = ClientBuilder.newClient();
WebTarget target = client.target("");
Attendant attendantResponse = target.queryParam("start", "10")
.request()
.post(Entity.json(new Attendant()), Attendant.class);
client.close();
Client API Async Sample (JAX-RS 2.0)
Client client = ClientBuilder.newClient();
WebTarget target = client.target("");
AsyncInvoker asyncInvoker = target.request().async();
Future<Response> future = asyncInvoker.get();
while(future.isDone()){ // or do some other task
Response response = future.get();
}
Filters
1. Intercepts request/response to carry out special functions e.g
authentication
2. Can be classified broadly into request and response filter
3. Request filters can be annotated to execute before matching is done
to a JAX-RS method (i.e @PreMatching)
4. Can be ordered (using @Priority)
5. Client and Server side filter available
ContainerRequestFilter Interface
All server request filter implement the ContainerRequestFilter interface
defined below
public interface ContainerRequestFilter {
public void filter(ContainerRequestContext requestContext)
throws IOException;
}
ContainerResponseFilter Interface
All server response filter must implement the ContainerResponseFilter
public interface ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext)
throws IOException;
}
ClientRequestFilter Interface
All client request filter should implement the following interface
public interface ClientRequestFilter {
public void filter(ClientRequestContext requestContext) throws
IOException;
}
ClientResponseFilter
All client response filter must implement the ClientResponseFilter
public interface ClientResponseFilter {
public void filter(ClientRequestContext requestContext,
ClientResponseContext responseContext)
throws IOException;
}
Bean Validation
1. Java specification of the Java API for JavaBean validation in Java EE
and Java SE
2. Integrated since Java EE 6
3. Constraints on object model can be expressed via annotations
4. Bean validation 2.0 is the current version
5. Easily extensible
Bean Validation. Examples
1. @Min @Max (Numeric Constraints )
2. @Past @Future (Date Contraints )
3. @Size (String and Collections)
4. @Email
5. @Pattern
6. @Null @NotNull
Java EE 8. What’s new ?
1. New REST Reactive CLient API
2. Server-Sent Events support (Client & Server side)
3. Enhanced JSON support with a new JSON Binding API
4. Asynchronous CDI events
5. New portable Security API
6. Servlet 4.0 API with HTTP/2 support
7. Support for Java SE 8 capabilities
New Client REST Reactive API
1. Introduction of the rx() method to the
javax.ws.rs.client.Invocation.Builder class which returns a
CompletionStageRxInvoker
2. CompletionStageRxInvoker returns CompletionStage that can be
used to access the results asynchronously
3. CompletionStage aids chaining various async calls and also
expressing their dependencies
Reactive Client Sample Code
CompletionStageRxInvoker rxInvoker = ClientBuilder.newClient()
.target("url")
.request()
.rx(); // .rx()
CompletionStage<Response> completionStage = rxInvoker.get();
Server Sent Events
1. Browser subscribes to a stream
2. Server sends messages (called event-streams)to the client
3. One communication channel (server to client)
4. Sent over HTTP
5. Supported by many browsers (Errr except Microsoft Edge & IE)
SSE Javascript API
if (typeof(EventSource) !== "undefined") {
var source = new EventSource('url');
Source.onmessage = function(event){
console.log(event.data);
}
} else {
// Your browser does not support sse :’(
}
JAX-RS 2.1 SSE sample
@Produces(MediaType.SERVER_SENT_EVENTS)
public void eventStream(@Context SseEventSink eventSink, @Context Sse sse){
if(!eventSink.isClosed()){
eventSink.send(OutboundSseEvent)
}
eventSink.close();
}
JAX-RS for backend Services
The services for the following app was written using JAX-RS
1. BioRegistra.com
2. AutoTopup.ng
Conclusion
1. JAX-RS is a good fit for building REST services
2. Explore and Explore. JavaEE projects are now on github
THANK YOU
References
● https://www.infoq.com/articles/rest-introduction
● https://www.tutorialspoint.com/restful/restful_introduction.htm
● https://people.cs.pitt.edu/~chang/265/seminar08/emilio.ppt
● https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-
edition/en/part1/chapter1/introduction_to_rest.html
● http://beanvalidation.org/
● https://docs.oracle.com/javaee/7/tutorial/bean-validation001.htm
● https://www.html5rocks.com/en/tutorials/eventsource/basics/
References continued
● https://docs.oracle.com/javaee/7/tutorial/
● https://www.youtube.com/watch?v=qLXJTrBcAmM
● https://www.youtube.com/watch?v=1b2tO3gfboE
● https://blogs.oracle.com/theaquarium/java-ee-8-is-final-and-glassfish-
50-is-released

Weitere ähnliche Inhalte

Was ist angesagt?

Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...Trayan Iliev
 
Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicIMC Institute
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedIMC Institute
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scalab0ris_1
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 
JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013Jagadish Prasath
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicIMC Institute
 
Configure Your Projects with Apache Tamaya
Configure Your Projects with Apache TamayaConfigure Your Projects with Apache Tamaya
Configure Your Projects with Apache TamayaAnatole Tresch
 
Java Web Programming [5/9] : EL, JSTL and Custom Tags
Java Web Programming [5/9] : EL, JSTL and Custom TagsJava Web Programming [5/9] : EL, JSTL and Custom Tags
Java Web Programming [5/9] : EL, JSTL and Custom TagsIMC Institute
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsCarol McDonald
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and ODataAnil Allewar
 
Lecture 7 Web Services JAX-WS & JAX-RS
Lecture 7   Web Services JAX-WS & JAX-RSLecture 7   Web Services JAX-WS & JAX-RS
Lecture 7 Web Services JAX-WS & JAX-RSFahad Golra
 
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun GuptaJAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun GuptaJAX London
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection apiMatthieu Aubry
 

Was ist angesagt? (19)

Websocket 1.0
Websocket 1.0Websocket 1.0
Websocket 1.0
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
Novelties in Java EE 7: JAX-RS 2.0 + IPT REST HATEOAS Polling Demo @ BGOUG Co...
 
Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet Basic
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scala
 
Jersey and JAX-RS
Jersey and JAX-RSJersey and JAX-RS
Jersey and JAX-RS
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 
JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013
 
OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
Configure Your Projects with Apache Tamaya
Configure Your Projects with Apache TamayaConfigure Your Projects with Apache Tamaya
Configure Your Projects with Apache Tamaya
 
Java Web Programming [5/9] : EL, JSTL and Custom Tags
Java Web Programming [5/9] : EL, JSTL and Custom TagsJava Web Programming [5/9] : EL, JSTL and Custom Tags
Java Web Programming [5/9] : EL, JSTL and Custom Tags
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and OData
 
Lecture 7 Web Services JAX-WS & JAX-RS
Lecture 7   Web Services JAX-WS & JAX-RSLecture 7   Web Services JAX-WS & JAX-RS
Lecture 7 Web Services JAX-WS & JAX-RS
 
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun GuptaJAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection api
 

Ähnlich wie RESTing with JAX-RS

Ppt on web development and this has all details
Ppt on web development and this has all detailsPpt on web development and this has all details
Ppt on web development and this has all detailsgogijoshiajmer
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyPayal Jain
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVCIndicThreads
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swiftTim Burks
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasyJBug Italy
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface pptTaha Malampatti
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing Techglyphs
 
Cloud native programming model comparison
Cloud native programming model comparisonCloud native programming model comparison
Cloud native programming model comparisonEmily Jiang
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011Shreedhar Ganapathy
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSSpark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSArun Gupta
 
Javaone 2010
Javaone 2010Javaone 2010
Javaone 2010Hien Luu
 
Automated testing web services - part 1
Automated testing web services - part 1Automated testing web services - part 1
Automated testing web services - part 1Aleh Struneuski
 

Ähnlich wie RESTing with JAX-RS (20)

Ppt on web development and this has all details
Ppt on web development and this has all detailsPpt on web development and this has all details
Ppt on web development and this has all details
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using Jersey
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swift
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Cloud native programming model comparison
Cloud native programming model comparisonCloud native programming model comparison
Cloud native programming model comparison
 
Andrei shakirin rest_cxf
Andrei shakirin rest_cxfAndrei shakirin rest_cxf
Andrei shakirin rest_cxf
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Android and REST
Android and RESTAndroid and REST
Android and REST
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSSpark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RS
 
Switch to Backend 2023
Switch to Backend 2023Switch to Backend 2023
Switch to Backend 2023
 
Servlets
ServletsServlets
Servlets
 
Javaone 2010
Javaone 2010Javaone 2010
Javaone 2010
 
Automated testing web services - part 1
Automated testing web services - part 1Automated testing web services - part 1
Automated testing web services - part 1
 
Services Stanford 2012
Services Stanford 2012Services Stanford 2012
Services Stanford 2012
 

Kürzlich hochgeladen

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 

Kürzlich hochgeladen (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 

RESTing with JAX-RS

  • 1. RESTing with JAX-RS OKAFOR EZEWUZIE @dawuzi
  • 2. About Me 1. Software Engineer at Seamfix Nigeria (www.seamfix.com) 2. JavaEE Enthusiast
  • 3. What is REST (Representational State Transfer) 1. Web standards based architecture targeted at distributed hypermedia systems such as the World Wide Web 2. Introduced in the doctoral dissertation of Roy Fielding in 2000 (One of the principal authors of the HTTP specification) 3. Describes how resources are defined and accessed 4. Makes use of several web standards (HTTP, URI, Resource Representations etc)
  • 4. Some REST Principles 1. All identifiable resources should have an ID 2. Use links to refer to those resources 3. Resources should be accessible via standard (HTTP) methods eg POST, GET, PUT, 4. Resources should have multiple representations 5. Stateless communication (Err not really. Just that state should be at the client side)
  • 5. Brief Intro to HTTP Methods 1. GET : Used for requesting for resources (Safe to call i.e should not modify any resource) 2. POST: Used for creating resource 3. PUT: Used for updating resources (Idempotent ) 4. DELETE: Used for deleting resources (Idempotent)
  • 6. Top REST Java frameworks 1. Dropwizard 2. Play Framework 3. RESTEasy 4. Restlet 5. Spark Framework 6. Spring Boot
  • 7. JAX-RS Introduction 1. Java API for RESTful Web Services (JAX-RS) 2. Java spec that aids creating Web services based on the REST architecture 3. Java annotations based
  • 8. JAX-RS Brief History 1. JAX-RS 1.0 in 2008 2. JAX-RS 1.1 in 2009. Added to the JavaEE 6. 3. JAX-RS 2.0 in 2014. JavaEE 7. (Client API, Bean validation, Async Processing) 4. JAX-RS 2.1 in Sept 2017. JavaEE 8. (Non-blocking IO, Reactive clients, Server Sent Events)
  • 9. Common JAX-RS Annotations ● @ApplicationPath : Identifies the application path that serves as the base URI for all resource URIs. ● @Path ● @GET, @POST, @PUT, @DELETE. This maps to the corresponding HTTP methods ● @Produces (used to specify the MIME media types or representations a resource)
  • 10. Common JAX-RS Annotations (Continued) ● @Consumes (used to specify which MIME media types of representations a resource can accept) ● @*Param eg @PathParam, @HeaderParam, @CookieParam, @QueryParam @BeanParam (used for retrieving request parameters) ● @Context : Used for injecting helper classes and informational Objects e.g URI information, SecurityContext, HTTPHeaders ● @Provider: An injectable interface providing runtime lookup of provider instances
  • 11. Sample JAX-RS application for JUG Attendees Simple Maven Project with two dependencies. JavaEE API and Lombok (to avoid a lot of boilerplate code) 1. A subclass of Application annotated with @ApplicationPath 2. A resource class annotated with @Path 3. A mock database class https://github.com/dawuzi/jax-rs-attendant-sample
  • 12. The javax.ws.rs.core.Response Class 1. Response class used for building more complex responses 2. Instances can be obtained from javax.ws.rs.core.Response.ResponseBuilder 3. Instances of ResponseBuilder can be obtained from static helper methods in the Response class itself
  • 13. Exception Handling 1. Applications can throw javax.ws.rs.WebApplicationException to have the container return specific HTTP codes to the client 2. The response can be customised for other exceptions by implementing the ExceptionMapper. The implementing class can then be annotated with @Provider or added to the list of classes in the Application sub class
  • 14. ExceptionMapper Interface public interface ExceptionMapper<E extends Throwable> { Response toResponse(Exception e); }
  • 15. JAX-RS Client API 1. Introduced in JAX-RS 2.0 2. Enabled writing portable client REST call with a fluent API
  • 16. Client API Sample Client client = ClientBuilder.newClient(); WebTarget target = client.target("https://www.google.com"); Response response = target.queryParam("start", "10").request().cookie("test-cookie", "test") .header("test-header", "test").accept(MediaType.APPLICATION_JSON) .get(); client.close();
  • 17. Client API Sample (Continued) Client client = ClientBuilder.newClient(); WebTarget target = client.target(""); Attendant attendantResponse = target.queryParam("start", "10") .request() .post(Entity.json(new Attendant()), Attendant.class); client.close();
  • 18. Client API Async Sample (JAX-RS 2.0) Client client = ClientBuilder.newClient(); WebTarget target = client.target(""); AsyncInvoker asyncInvoker = target.request().async(); Future<Response> future = asyncInvoker.get(); while(future.isDone()){ // or do some other task Response response = future.get(); }
  • 19. Filters 1. Intercepts request/response to carry out special functions e.g authentication 2. Can be classified broadly into request and response filter 3. Request filters can be annotated to execute before matching is done to a JAX-RS method (i.e @PreMatching) 4. Can be ordered (using @Priority) 5. Client and Server side filter available
  • 20. ContainerRequestFilter Interface All server request filter implement the ContainerRequestFilter interface defined below public interface ContainerRequestFilter { public void filter(ContainerRequestContext requestContext) throws IOException; }
  • 21. ContainerResponseFilter Interface All server response filter must implement the ContainerResponseFilter public interface ContainerResponseFilter { public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException; }
  • 22. ClientRequestFilter Interface All client request filter should implement the following interface public interface ClientRequestFilter { public void filter(ClientRequestContext requestContext) throws IOException; }
  • 23. ClientResponseFilter All client response filter must implement the ClientResponseFilter public interface ClientResponseFilter { public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException; }
  • 24. Bean Validation 1. Java specification of the Java API for JavaBean validation in Java EE and Java SE 2. Integrated since Java EE 6 3. Constraints on object model can be expressed via annotations 4. Bean validation 2.0 is the current version 5. Easily extensible
  • 25. Bean Validation. Examples 1. @Min @Max (Numeric Constraints ) 2. @Past @Future (Date Contraints ) 3. @Size (String and Collections) 4. @Email 5. @Pattern 6. @Null @NotNull
  • 26. Java EE 8. What’s new ? 1. New REST Reactive CLient API 2. Server-Sent Events support (Client & Server side) 3. Enhanced JSON support with a new JSON Binding API 4. Asynchronous CDI events 5. New portable Security API 6. Servlet 4.0 API with HTTP/2 support 7. Support for Java SE 8 capabilities
  • 27. New Client REST Reactive API 1. Introduction of the rx() method to the javax.ws.rs.client.Invocation.Builder class which returns a CompletionStageRxInvoker 2. CompletionStageRxInvoker returns CompletionStage that can be used to access the results asynchronously 3. CompletionStage aids chaining various async calls and also expressing their dependencies
  • 28. Reactive Client Sample Code CompletionStageRxInvoker rxInvoker = ClientBuilder.newClient() .target("url") .request() .rx(); // .rx() CompletionStage<Response> completionStage = rxInvoker.get();
  • 29. Server Sent Events 1. Browser subscribes to a stream 2. Server sends messages (called event-streams)to the client 3. One communication channel (server to client) 4. Sent over HTTP 5. Supported by many browsers (Errr except Microsoft Edge & IE)
  • 30. SSE Javascript API if (typeof(EventSource) !== "undefined") { var source = new EventSource('url'); Source.onmessage = function(event){ console.log(event.data); } } else { // Your browser does not support sse :’( }
  • 31. JAX-RS 2.1 SSE sample @Produces(MediaType.SERVER_SENT_EVENTS) public void eventStream(@Context SseEventSink eventSink, @Context Sse sse){ if(!eventSink.isClosed()){ eventSink.send(OutboundSseEvent) } eventSink.close(); }
  • 32. JAX-RS for backend Services The services for the following app was written using JAX-RS 1. BioRegistra.com 2. AutoTopup.ng
  • 33. Conclusion 1. JAX-RS is a good fit for building REST services 2. Explore and Explore. JavaEE projects are now on github
  • 35. References ● https://www.infoq.com/articles/rest-introduction ● https://www.tutorialspoint.com/restful/restful_introduction.htm ● https://people.cs.pitt.edu/~chang/265/seminar08/emilio.ppt ● https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd- edition/en/part1/chapter1/introduction_to_rest.html ● http://beanvalidation.org/ ● https://docs.oracle.com/javaee/7/tutorial/bean-validation001.htm ● https://www.html5rocks.com/en/tutorials/eventsource/basics/
  • 36. References continued ● https://docs.oracle.com/javaee/7/tutorial/ ● https://www.youtube.com/watch?v=qLXJTrBcAmM ● https://www.youtube.com/watch?v=1b2tO3gfboE ● https://blogs.oracle.com/theaquarium/java-ee-8-is-final-and-glassfish- 50-is-released