SlideShare a Scribd company logo
July 29, 2017
Kumaraswamy Gowda
Software Architect, PSL
Build RESTful
application
with JAX-RS
Agenda
2
• What is REST
• REST principles
• Java API for RESTful Web Services (JAX-RS, defined in
JSR 311)
• HTTP Methods
• Annotations
• Client APIs
• SWAGGER – Expose and Test REST API
HTTP Example
3
Request
GET /music/artists/beatles/recordings HTTP/1.1
Host: media.example.com
Accept: application/xml
Response
HTTP/1.1 200 OK
Date: Sat, 08 Jul 2017 16:41:58 GMT
Content-Type: application/xml(or json); charset=UTF-8
<?xml version="1.0"?>
<recordings xmlns="…">
<recording>…</recording>
…
</recordings>
Method Resource
Representation
State
transfer
{
"recordings": {
"recording": [
{},
{}
]
}
}
What is REST
4
• REpresentational State Transfer is an architectural style
for building online applications
• First described by Roy Fielding in his doctoral dissertation
in 2000
• Lightweight, Scalable and maintainable
• Stateless by nature
• Client server protocol, almost always HTTP
• Clients can be any programming language
REST architecture for Web
5
Resources
Web Container
Resources
Web Container
REST Principles
6
• Everything is a resource
• Assign everything an ID
• Uniform Interface- fixed set of four
create, read, update, delete operations
o PUT, GET, POST, and DELETE
• Representation of the Resource
o JSON, XML, TEXT
/reports
--------------------
GET – list all reports
PUT – not supported
POST – create a report
DELETE – not supported
/reports/{id}
--------------------
GET – get report details
PUT – update report
POST – not supported
DELETE – delete a report
{
"recordings": {
"recording": [
{},
{}
]
}
}
<?xml version="1.0"?>
<recordings xmlns="…">
<recording>…</recording>
…
</recordings>
REST Principles contd…
7
• Stateless
• Link
o /reports to contain links to all reports like /reports/1,
/reports/2
• Pagination
o Limit data per request
o Enables clients to navigate forward or backward
• Caching
• Fields, sorting, filtering, versioning
JAX-RS (2.0)
8
• Java API for RESTful Web Services (JAX-RS, defined in
JSR 311)
• Designed to make it easy to develop applications that use
the REST architecture
• Implementations
o Jersey
o Apache CXF
o Apache Wink
o Restlet
• JAX-RS uses annotations available from Java SE 5
Annotations
9
• @Path
o Sets path to base URL +
@Path value
o https://server:port/eclipseapp
/api/reports
o @Path(“/reports”/{id})
o @Path("/username/{usernam
e : [a-zA-Z][a-zA-Z_0-9]}")
o @Path("{id : d+}")
@GET, @POST, @PUT, @DELETE
10
/reports – GET POST
/reports/{reportid} – GET, DELETE, PUT
/reports/{reportid}/templates
/reports/{reportid}/templates/{templateid}
@Produces and @Consumes
11
• MIME type content returned
• Equivalent to Content-type response header
@PathParam @QueryParam @HeaderParam
@FormDataParam @FormParam
12
• Binds values from request to the parameter
Deployment Descriptor (web.xml)
13
<servlet>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.abc.xyz.services</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.abc.xyz.service.MyApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Client APIs
14
• Remember URLConnection? Or apache http client
libraries?
• Fluent API available from JAX-RS 2.0
Client
Builder
Client
Web
Target
Request
Building
Response..
SWAGGER
API developer tools for the OpenAPI Specification
15
What is Swagger and Why Swagger?
16
• Simple representation of your RESTFul API
• Declarative resource specification
• JSON specification
• Developers and documentation… hahaha
• Why Jersey Client / JUNIT or Rest Client (Poster /
Postman) or API document for your client or boss?
• Swagger UI to interact with the API in a sandbox UI
• Support your existing RESTFul API without change
Swagger Specification
17
Web Services
• Resource Listing
• API Description
Create Swagger Specification
• Manually - writing the JSON by hand
• Codgen - converts annotations in your code into swagger
specification
Swagger Specifications
18
19
Swagger Tools
20
Adjacent tools also provided by the
Swagger project are
Tool Description
Swagger JS Javascript library for consuming Swagger-defined APIs from both client and node.js app
Swagger Node Swagger module for node.js
Swagger-Socket expose/invoke Swagger defined APIs over WebSockets
Swagger Parser Standalone library for parsing Swagger definitions from Java
Tools for nearly every language and
allows you to deploy server instances
to get your API up and running -
Clojure, Go, JS, Java, .Net, Node,
PHP, Python, Ruby, Scala
References
21
• http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html
• http://www.vogella.com/tutorials/REST/article.html
• https://jersey.java.net/documentation/latest/jaxrs-
resources.html
• https://github.com/koushikkothagal/messenger
• http://swagger.io/specification/
• https://github.com/swagger-api/swagger-spec
• https://github.com/swagger-api/swagger-core
• http://petstore.swagger.io/
22
Thanks!
Any questions?
You can find me at
➝https://www.linkedin.com/in/kumaraswamym/
➝kumaraswamy.m@gmail.com
RESTful application with JAX-RS and how to expose and test them

More Related Content

PDF
Java colombo-deep-dive-into-jax-rs
PDF
JavaEE and RESTful development - WSO2 Colombo Meetup
PPTX
L18 REST API Design
PDF
Request-Response Cycle of Ruby on Rails App
PPTX
RESTful Web Service using Swagger
PPTX
RestFul Web Services In Drupal 8
PDF
Globus for System Administrators (GlobusWorld Tour - UCSD)
PPTX
HBaseConEast2016: HBase and Spark, State of the Art
Java colombo-deep-dive-into-jax-rs
JavaEE and RESTful development - WSO2 Colombo Meetup
L18 REST API Design
Request-Response Cycle of Ruby on Rails App
RESTful Web Service using Swagger
RestFul Web Services In Drupal 8
Globus for System Administrators (GlobusWorld Tour - UCSD)
HBaseConEast2016: HBase and Spark, State of the Art

What's hot (20)

PDF
(ATS6-PLAT04) Query service
PDF
(ATS6-DEV06) Using Packages for Protocol, Component, and Application Delivery
PPTX
Oak, the architecture of Apache Jackrabbit 3
PPT
File System On Steroids
PPTX
Apache Falcon DevOps
PDF
Globus Command Line Interface (APS Workshop)
PDF
Globus Endpoint Setup and Configuration - XSEDE14 Tutorial
PPTX
Apache Phoenix Query Server PhoenixCon2016
PDF
Native REST Web Services with Oracle 11g
PDF
(ATS6-PLAT07) Managing AEP in an enterprise environment
PPTX
Apache phoenix
PPTX
The architecture of oak
PDF
Tutorial: Managing Protected Data with Globus Connect Server v5
PPTX
HiveWarehouseConnector
PDF
(ATS6-PLAT02) Accelrys Catalog and Protocol Validation
PPTX
Apache Flink - A Stream Processing Engine
PDF
LDAP Development Using Spring LDAP
PDF
(ATS6-PLAT03) What's behind Discngine collections
PDF
Avik_RailsTutorial
PPTX
Archive integration with RDF
(ATS6-PLAT04) Query service
(ATS6-DEV06) Using Packages for Protocol, Component, and Application Delivery
Oak, the architecture of Apache Jackrabbit 3
File System On Steroids
Apache Falcon DevOps
Globus Command Line Interface (APS Workshop)
Globus Endpoint Setup and Configuration - XSEDE14 Tutorial
Apache Phoenix Query Server PhoenixCon2016
Native REST Web Services with Oracle 11g
(ATS6-PLAT07) Managing AEP in an enterprise environment
Apache phoenix
The architecture of oak
Tutorial: Managing Protected Data with Globus Connect Server v5
HiveWarehouseConnector
(ATS6-PLAT02) Accelrys Catalog and Protocol Validation
Apache Flink - A Stream Processing Engine
LDAP Development Using Spring LDAP
(ATS6-PLAT03) What's behind Discngine collections
Avik_RailsTutorial
Archive integration with RDF
Ad

Similar to RESTful application with JAX-RS and how to expose and test them (20)

PPTX
Building Restful Web Services with Java
PPTX
Ppt on web development and this has all details
ODP
RESTing with JAX-RS
PDF
JAX-RS Creating RESTFul services
PDF
Spark IT 2011 - Developing RESTful Web services with JAX-RS
PPTX
JAX-RS 2.0 and OData
PDF
RESTful Web services using JAX-RS
PDF
RESTful Java With JAX RS 1st Edition Bill Burke
PPT
Developing RESTful WebServices using Jersey
PPTX
#6 (RESTtful Web Wervices)
PDF
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
PDF
Rest with java (jax rs) and jersey and swagger
PDF
RESTful Java With JAX RS 1st Edition Bill Burke
PDF
JAX-RS JavaOne Hyderabad, India 2011
PDF
S313265 - Advanced Java API for RESTful Web Services at JavaOne Brazil 2010
PDF
Lab swe-2013intro jax-rs
PDF
Rest services with Jax-rs
PDF
Making Java REST with JAX-RS 2.0
PPTX
JAX-RS. Developing RESTful APIs with Java
PPTX
6 Months Industrial Training in Spring Framework
Building Restful Web Services with Java
Ppt on web development and this has all details
RESTing with JAX-RS
JAX-RS Creating RESTFul services
Spark IT 2011 - Developing RESTful Web services with JAX-RS
JAX-RS 2.0 and OData
RESTful Web services using JAX-RS
RESTful Java With JAX RS 1st Edition Bill Burke
Developing RESTful WebServices using Jersey
#6 (RESTtful Web Wervices)
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Rest with java (jax rs) and jersey and swagger
RESTful Java With JAX RS 1st Edition Bill Burke
JAX-RS JavaOne Hyderabad, India 2011
S313265 - Advanced Java API for RESTful Web Services at JavaOne Brazil 2010
Lab swe-2013intro jax-rs
Rest services with Jax-rs
Making Java REST with JAX-RS 2.0
JAX-RS. Developing RESTful APIs with Java
6 Months Industrial Training in Spring Framework
Ad

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
medical staffing services at VALiNTRY
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administration Chapter 2
PPT
Introduction Database Management System for Course Database
PPTX
history of c programming in notes for students .pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Digital Strategies for Manufacturing Companies
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo Companies in India – Driving Business Transformation.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Wondershare Filmora 15 Crack With Activation Key [2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
medical staffing services at VALiNTRY
Odoo POS Development Services by CandidRoot Solutions
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administration Chapter 2
Introduction Database Management System for Course Database
history of c programming in notes for students .pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
ISO 45001 Occupational Health and Safety Management System
Digital Strategies for Manufacturing Companies

RESTful application with JAX-RS and how to expose and test them

  • 1. July 29, 2017 Kumaraswamy Gowda Software Architect, PSL Build RESTful application with JAX-RS
  • 2. Agenda 2 • What is REST • REST principles • Java API for RESTful Web Services (JAX-RS, defined in JSR 311) • HTTP Methods • Annotations • Client APIs • SWAGGER – Expose and Test REST API
  • 3. HTTP Example 3 Request GET /music/artists/beatles/recordings HTTP/1.1 Host: media.example.com Accept: application/xml Response HTTP/1.1 200 OK Date: Sat, 08 Jul 2017 16:41:58 GMT Content-Type: application/xml(or json); charset=UTF-8 <?xml version="1.0"?> <recordings xmlns="…"> <recording>…</recording> … </recordings> Method Resource Representation State transfer { "recordings": { "recording": [ {}, {} ] } }
  • 4. What is REST 4 • REpresentational State Transfer is an architectural style for building online applications • First described by Roy Fielding in his doctoral dissertation in 2000 • Lightweight, Scalable and maintainable • Stateless by nature • Client server protocol, almost always HTTP • Clients can be any programming language
  • 5. REST architecture for Web 5 Resources Web Container Resources Web Container
  • 6. REST Principles 6 • Everything is a resource • Assign everything an ID • Uniform Interface- fixed set of four create, read, update, delete operations o PUT, GET, POST, and DELETE • Representation of the Resource o JSON, XML, TEXT /reports -------------------- GET – list all reports PUT – not supported POST – create a report DELETE – not supported /reports/{id} -------------------- GET – get report details PUT – update report POST – not supported DELETE – delete a report { "recordings": { "recording": [ {}, {} ] } } <?xml version="1.0"?> <recordings xmlns="…"> <recording>…</recording> … </recordings>
  • 7. REST Principles contd… 7 • Stateless • Link o /reports to contain links to all reports like /reports/1, /reports/2 • Pagination o Limit data per request o Enables clients to navigate forward or backward • Caching • Fields, sorting, filtering, versioning
  • 8. JAX-RS (2.0) 8 • Java API for RESTful Web Services (JAX-RS, defined in JSR 311) • Designed to make it easy to develop applications that use the REST architecture • Implementations o Jersey o Apache CXF o Apache Wink o Restlet • JAX-RS uses annotations available from Java SE 5
  • 9. Annotations 9 • @Path o Sets path to base URL + @Path value o https://server:port/eclipseapp /api/reports o @Path(“/reports”/{id}) o @Path("/username/{usernam e : [a-zA-Z][a-zA-Z_0-9]}") o @Path("{id : d+}")
  • 10. @GET, @POST, @PUT, @DELETE 10 /reports – GET POST /reports/{reportid} – GET, DELETE, PUT /reports/{reportid}/templates /reports/{reportid}/templates/{templateid}
  • 11. @Produces and @Consumes 11 • MIME type content returned • Equivalent to Content-type response header
  • 12. @PathParam @QueryParam @HeaderParam @FormDataParam @FormParam 12 • Binds values from request to the parameter
  • 13. Deployment Descriptor (web.xml) 13 <servlet> <servlet-name>JAX-RS Servlet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.abc.xyz.services</param-value> </init-param> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.abc.xyz.service.MyApplication</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>JAX-RS Servlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
  • 14. Client APIs 14 • Remember URLConnection? Or apache http client libraries? • Fluent API available from JAX-RS 2.0 Client Builder Client Web Target Request Building Response..
  • 15. SWAGGER API developer tools for the OpenAPI Specification 15
  • 16. What is Swagger and Why Swagger? 16 • Simple representation of your RESTFul API • Declarative resource specification • JSON specification • Developers and documentation… hahaha • Why Jersey Client / JUNIT or Rest Client (Poster / Postman) or API document for your client or boss? • Swagger UI to interact with the API in a sandbox UI • Support your existing RESTFul API without change
  • 17. Swagger Specification 17 Web Services • Resource Listing • API Description Create Swagger Specification • Manually - writing the JSON by hand • Codgen - converts annotations in your code into swagger specification
  • 19. 19
  • 20. Swagger Tools 20 Adjacent tools also provided by the Swagger project are Tool Description Swagger JS Javascript library for consuming Swagger-defined APIs from both client and node.js app Swagger Node Swagger module for node.js Swagger-Socket expose/invoke Swagger defined APIs over WebSockets Swagger Parser Standalone library for parsing Swagger definitions from Java Tools for nearly every language and allows you to deploy server instances to get your API up and running - Clojure, Go, JS, Java, .Net, Node, PHP, Python, Ruby, Scala
  • 21. References 21 • http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html • http://www.vogella.com/tutorials/REST/article.html • https://jersey.java.net/documentation/latest/jaxrs- resources.html • https://github.com/koushikkothagal/messenger • http://swagger.io/specification/ • https://github.com/swagger-api/swagger-spec • https://github.com/swagger-api/swagger-core • http://petstore.swagger.io/
  • 22. 22 Thanks! Any questions? You can find me at ➝https://www.linkedin.com/in/kumaraswamym/ ➝kumaraswamy.m@gmail.com

Editor's Notes

  • #2: Today let us try to learn building RESTful application using JAX-RS
  • #3: We’ll understand what is REST and what are it’s principles And then know about JAX-RS Understand different HTTP methods that helps building REST services Understand annotation from JAX-RS that simplifies building REST services Look at client APIs from JAR-RS 2.0 And final on how to expose REST APIs
  • #4: Let’s look at a basic HTTP request and response Looking at the URI, the client is requesting to GET all the recordings from beatles band The URI is /music/…./recordings HTTP method is GET The clients ways, I’m expecting an XML as a response All of these forms a request Let’s take a looks at the response The HTTP request status code is 200, which means successful The content-type header says that response data is in xml format.. Or json format
  • #5: Web services are services that are exposed to the internet for programmatic access... an online API that you can call from your code it's a concept, it’s an idea, there is no specification, and no committee to tell you what is right or what is wrong it's an architectural style, set of guide lines for architecting your services… build the services as per your need following the REST principles It was first described by Roy Fielding… he is also one the principal authors of the HTTP specifications Lightweight, without unnecessary headers as in SOAP services Concepts of REST are mainly influenced by HTTP
  • #6: I'm sure many of you would have heard of Facebook and Twitter app... you must have seen games that would post on the Facebook wall, even though those games are not designed by Facebook themselves How do they do this? by calling online APIs
  • #7: Think of resources and build unique URLs for it Resources/URIs should be nouns and not verbs... the URIs should be documents, messages, reports.... and not getMessages, deleteMessages.. it should typically be nouns and not the action it's not message... it should be messages getMessages.do?id=10 deleteMessages.do?id=25 putMessages.do HTTP methods: They have specific meaning to each of these methods You are using these methods with or without your knowledge... like while post a comment or while opening a page in the web browser In the REST world, you don't make call to getReports.. you make a get request to the reports resource URI you don't call a deleteOrder, you make a delete request to the order resource URI the URI tells what entity or resource is being operated upon and the method tells what that operation is.
  • #8: Caching - Cache response on the service layer... for a short time - Stateless - Every request should be independent request - every request should send the state -
  • #9: It provide a way to map the meta-information to your methods through annotation. It’s a POJO based resource classes Injects
  • #10: Which resource you want to act upon In JAX-RS, you can use @Path to bind URI pattern to a Java method. The base URL + @Path parameter before the full URL URL matching with @Path pattern URI matching with parameter: The value within an open brace “{” and close brace “}”, is represents a parameter, and can be access with @PathParam URI matching with regular expression:
  • #11: @Path mentions that resource you want to act upon… @GET, @POST tells what action would that be… To do that, you could add annotations like… to the java method
  • #12: The @Produces annotation is used to specify the MIME media types or representations a resource can produce and send back to the client Can be applied to class level or method level The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. Can be applied to class level or method level
  • #14: In the dep desc, define servlet container handling all ur jax-rs service requests….
  • #15: Specification - human and machine readable specification -
  • #16: How many of you have used HTTP URLConnection API from java? Was the experience pleasant?
  • #17: Let me ask you all a question. How many of you develop web services? How do you test the web services that you're developing? write junits (junits are still important to test the services for regressions) java.net.URLConnection API, use libraries like HTTP Client, Jersey Client Rest Client from the broswers like Poster, POSTMAN create a simple/sample UI app to test the APIs For someone to consume your services, you have to expose the API. How do you share/expose your APIs to others? Like what are the services are available, what parameters to be passed, is it a GET or POST document the web services in a word document when an API is updated, new service is added, a service is deprecated, you need to update the document...
  • #18: What is Swagger Swagger is a simple yet powerful representation of your RESTful API. It is an open source tool and has support for nearly every language like Closure, JS, Go, Scala, .Net, PHP, Python, Ruby and not to forget, JAVA. Swagger provides a declarative resource specification, allowing users to understand and consume services without knowledge of server implementation, enabling both developers and non-developers to interact with the API, providing clear insight into how the API responds to parameters and options. Why Swagger - Developer and documentation... no offense to anyone... developers usually hate documentation... we tend to avoid documentation whenever we could - Swagger provides a sandbox UI to interact with the API... wow... how is it different form POSTER or POSTMAN... let's see
  • #19: Specification - human and machine readable specification -