SlideShare a Scribd company logo
1 of 29
Restful Service with Java
REST
● REST stands for Representational State Transfer
-Design pattern for developing web services.
● Resource based
● Rest Style:
● Client-server
● Uniform interface
● Stateless
● Cached
● Layered system
● HATEOAS - (Hypermedia As The Engine Of Application State)
REST - not a Standard
● But it uses several standards:
o HTTP
o URL
o XML/HTML/GIF/JPEG/etc (Resource Representations)
o text/xml, text/html, image/gif, image/jpeg, etc (Resource Types, MIME Types)
Browser Web ServerGET /index.html HTTP/1.1
Host: www.pitt.edu
HTTP/1.1 200 OK
Content-Type:
text/html
HTTP Request
• The HTTP request is sent from the client.
– Identifies the location of a resource.
– Uses nouns rather than verbs to denote simple resources.
– Specifies the verb, or HTTP method to use when accessing the resource.
– Supplies optional request headers (name-value pairs) that provide additional
information the server may need when processing the request.
– Supplies an optional request body that identifies additional data to be
uploaded to the server (e.g. form parameters, attachments, etc.)
Sample Client Requests:
GET /view?id=1 HTTP/1.1 Request Headers
User-Agent: Chrome
Accept: application/json Requested Resource (path and query
string)
(no request body)
POST /save HTTP/1.1 Requested Resource (typically no query string)
User-Agent: IE
Content-Type: application/x-www-form-urlencoded Request Headers
name=x&id=2 Request Body (e.g. form parameters)
HTTP Response
• The HTTP response is sent from the server.
– Gives the status of the processed request.
– Supplies response headers (name-value pairs) that provide additional
information about the response.
– Supplies an optional response body that identifies additional data to be
downloaded to the client (html, xml, binary data, etc.)
– -HTTP Status codes(1xx, 2xx, 3xx, 4xx, 5xx)
Sample Server Responses:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1337
[CRLF]
<html>
<!-- Some HTML Content. -->
</html>
HTTP/1.1 500 Internal Server Error
HTTP/1.1 201 Created
Location: /view/7
[CRLF]
Some message goes here.
Response Status
Response Headers
Response Body (content)
Response Status
Response Header
Response Body
Response Status
Standard Set of Methods
● GET - read data and not change it.
● PUT - update capabilities
● POST - create subordinate resources
● DELETE - delete a resource
● OPTIONS - ‘What methods are allowed’
● HEAD - HTTP header
A typical HTTP REST URL:
http://my.store.com/fruits/list?category=fruit&limit=20
• The protocol identifies the transport scheme that will be used to
process and respond to the request.
• The host name identifies the server address of the resource.
• The path and query string can be used to identify and customize
the accessed resource.
protocol host name path to a resource query string
RESTful Application Cycle
Resources are identified by URIs
Clients communicate with resources via
requests using a standard set of methods
Requests and responses contain resource
representations in formats identified by
media types.
Responses contain URIs that link to further
resources
Examples of Rest URIs
Insert new customer in a system POST
http://www.example.com/customers/1234
5
Read a customer with customer
ID
GET
http://www.example.com/customers/3324
5
Read all orders with customer ID GET
http://www.example.com/customers/3324
5/orders
JAX-RS is a Java standard API for REST services:
• Services are annotation driven
• Provides support for data binding.(JAX-B)
• Provides advanced APIs for content negotiation.(@Produces/@Consumes)
SOAP vs. REST: Overview
Both SOAP and REST are front-end technologies.
SOAP – Simple Object Access Protocol
 Supports a variety of transports (HTTP, JMS, etc.) and integrates with a variety of web service standards.
 Typically used to pass contractually structured data between applications.
 Bound to xml.
 Uses SOAP envelope and then HTTP (or FTP/SMTP) to transfer the data.
 Slower performance and scalability is a bit complex. Caching not possible.
REST - Representational State Transfer
 Architectural style
 Simple point-to-point communication using well-established HTTP verbs, protocols, and standards.
 Supports many different data formats like JSON, XML etc.
 Performance and scalability, caching.
 Lightweight, easy to consume.
 Widely and frequently used.
SOAP vs. REST: Overview
 - XML-based protocol
 - How to access the service and what operations are performed
 Broadly consists of:
 - Types
 - Operation
 - Binding
WSDL- Webservices Description Language
Connection point
Client-server response
Client-server request
WSDL Example

Name of the service
Parameter of the ws
Request-response operation
targetNamespace, default and
other namespaces
WSDL Example- contd.
 Define binding
transport
Endpoint URI
Connect port
and binding
Restful Webservices
● A RESTful Web service follows four basic design principles:
o Uses HTTP methods
o Be stateless as far as possible
o Expose directory/folder structure-like URI
o Transfer XML, JSON, or both
Java API for RESTful Web Services (JAX-RS)
vs Spring MVC
Some Guidelines for choosing your solution:
• Both JAX-RS and Spring MVC can produce REST services.
• Spring MVC is a web application framework that can be used as service framework.
– Provides better validation
– Supports internationalization
• JAX-RS is a primarily a services framework.
– Provides support for WADL generation
– Can use CXF interceptors, filters, etc.
• Match the framework to the needs and purpose of the project.
• Don’t mix both in same web application unless you need unique features from each.
– If your project needs both, consider separate web applications.
Spring mvc architecture
● Spring web MVC framework
RESTful support in Spring
● Controllers can handle requests for all HTTP methods, including the four primary REST methods: GET,
PUT, DELETE, and POST.
● The @PathVariable annotation enables controllers to handle requests for parameterized URLs (URLs
that have variable input as part of their path).
● Resources can be represented in a variety of ways using Spring views and view resolvers, including
View implementations for rendering model data as XML, JSON, Atom, and RSS.
The representation best suited for the client can be chosen using ContentNegotiatingViewResolver.
● View-based rendering can be bypassed altogether using the @ResponseBody annotation and various
HttpMethodConverter implementations.
● Similarly, the @RequestBody annotation, along with HttpMethodConverter implementations, can
convert inbound HTTP data into Java objects passed in to a controller’s handler methods.
● Spring applications can consume REST resources using RestTemplate
Web.xml configuration
<servlet>
<servlet-name>AccountService</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
URI Mappings example
Content Negotiation
Content Negotiation using views
 DEMO
Next topics
- Securing Restful Services
- Open source frameworks JERSEY, RESTEASY
References
- Restful Webservice: Leonrard Richardson and Sam
Ruby//O’Reilly
- RESTful Web Services Cookbook: Subbu Allamraju//O’Reilly
- Roy Fieldings dissertation -
http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
- Spring in Action, 4th Edition, Craig Walls//Manning
Questions

More Related Content

What's hot

So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scalab0ris_1
 
REST & RESTful Web Service
REST & RESTful Web ServiceREST & RESTful Web Service
REST & RESTful Web ServiceHoan Vu Tran
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API RecommendationsJeelani Shaik
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPAShaun Smith
 
Designing a RESTful web service
Designing a RESTful web serviceDesigning a RESTful web service
Designing a RESTful web serviceFilip Blondeel
 
Restful webservice
Restful webserviceRestful webservice
Restful webserviceDong Ngoc
 
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
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHPZoran Jeremic
 
RESTful http_patterns_antipatterns
RESTful http_patterns_antipatternsRESTful http_patterns_antipatterns
RESTful http_patterns_antipatternsJan Algermissen
 
Things I wish web graduates knew
Things I wish web graduates knewThings I wish web graduates knew
Things I wish web graduates knewLorna Mitchell
 
Web services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP libraryWeb services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP libraryFulvio Corno
 

What's hot (20)

RESTing with JAX-RS
RESTing with JAX-RSRESTing with JAX-RS
RESTing with JAX-RS
 
Jersey and JAX-RS
Jersey and JAX-RSJersey and JAX-RS
Jersey and JAX-RS
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scala
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
REST & RESTful Web Service
REST & RESTful Web ServiceREST & RESTful Web Service
REST & RESTful Web Service
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API Recommendations
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPA
 
Designing a RESTful web service
Designing a RESTful web serviceDesigning a RESTful web service
Designing a RESTful web service
 
Restful webservice
Restful webserviceRestful webservice
Restful webservice
 
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
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
Introduction to JAX-RS
Introduction to JAX-RSIntroduction to JAX-RS
Introduction to JAX-RS
 
RESTful http_patterns_antipatterns
RESTful http_patterns_antipatternsRESTful http_patterns_antipatterns
RESTful http_patterns_antipatterns
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Things I wish web graduates knew
Things I wish web graduates knewThings I wish web graduates knew
Things I wish web graduates knew
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
ASP.NET WEB API
ASP.NET WEB APIASP.NET WEB API
ASP.NET WEB API
 
Web services tutorial
Web services tutorialWeb services tutorial
Web services tutorial
 
Web services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP libraryWeb services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP library
 

Similar to Restful Service with Java - Building RESTful APIs

Similar to Restful Service with Java - Building RESTful APIs (20)

RESTful web services using java and spring
RESTful web services using java and springRESTful web services using java and spring
RESTful web services using java and spring
 
6 Months Industrial Training in Spring Framework
6 Months Industrial Training in Spring Framework6 Months Industrial Training in Spring Framework
6 Months Industrial Training in Spring Framework
 
Restful web services
Restful web servicesRestful web services
Restful web services
 
ROA.ppt
ROA.pptROA.ppt
ROA.ppt
 
Overview of java web services
Overview of java web servicesOverview of java web services
Overview of java web services
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
The introduction of RESTful
The introduction of RESTful The introduction of RESTful
The introduction of RESTful
 
Mini-Training: Let's have a rest
Mini-Training: Let's have a restMini-Training: Let's have a rest
Mini-Training: Let's have a rest
 
Learn REST API at ASIT
Learn REST API at ASITLearn REST API at ASIT
Learn REST API at ASIT
 
Ch-1_.ppt
Ch-1_.pptCh-1_.ppt
Ch-1_.ppt
 
REST Introduction.ppt
REST Introduction.pptREST Introduction.ppt
REST Introduction.ppt
 
REST - Representational State Transfer
REST - Representational State TransferREST - Representational State Transfer
REST - Representational State Transfer
 
Rest web service
Rest web serviceRest web service
Rest web service
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1
 
Efficient Spring Data REST Development
Efficient Spring Data REST DevelopmentEfficient Spring Data REST Development
Efficient Spring Data REST Development
 
Restful Fundamentals
Restful FundamentalsRestful Fundamentals
Restful Fundamentals
 
Restful Fundamentals
Restful FundamentalsRestful Fundamentals
Restful Fundamentals
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
SCDJWS 6. REST JAX-P
SCDJWS 6. REST  JAX-PSCDJWS 6. REST  JAX-P
SCDJWS 6. REST JAX-P
 
HTTP In-depth
HTTP In-depthHTTP In-depth
HTTP In-depth
 

Recently uploaded

Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 

Recently uploaded (20)

Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 

Restful Service with Java - Building RESTful APIs

  • 2. REST ● REST stands for Representational State Transfer -Design pattern for developing web services. ● Resource based ● Rest Style: ● Client-server ● Uniform interface ● Stateless ● Cached ● Layered system ● HATEOAS - (Hypermedia As The Engine Of Application State)
  • 3. REST - not a Standard ● But it uses several standards: o HTTP o URL o XML/HTML/GIF/JPEG/etc (Resource Representations) o text/xml, text/html, image/gif, image/jpeg, etc (Resource Types, MIME Types) Browser Web ServerGET /index.html HTTP/1.1 Host: www.pitt.edu HTTP/1.1 200 OK Content-Type: text/html
  • 4. HTTP Request • The HTTP request is sent from the client. – Identifies the location of a resource. – Uses nouns rather than verbs to denote simple resources. – Specifies the verb, or HTTP method to use when accessing the resource. – Supplies optional request headers (name-value pairs) that provide additional information the server may need when processing the request. – Supplies an optional request body that identifies additional data to be uploaded to the server (e.g. form parameters, attachments, etc.)
  • 5. Sample Client Requests: GET /view?id=1 HTTP/1.1 Request Headers User-Agent: Chrome Accept: application/json Requested Resource (path and query string) (no request body) POST /save HTTP/1.1 Requested Resource (typically no query string) User-Agent: IE Content-Type: application/x-www-form-urlencoded Request Headers name=x&id=2 Request Body (e.g. form parameters)
  • 6. HTTP Response • The HTTP response is sent from the server. – Gives the status of the processed request. – Supplies response headers (name-value pairs) that provide additional information about the response. – Supplies an optional response body that identifies additional data to be downloaded to the client (html, xml, binary data, etc.) – -HTTP Status codes(1xx, 2xx, 3xx, 4xx, 5xx)
  • 7. Sample Server Responses: HTTP/1.1 200 OK Content-Type: text/html Content-Length: 1337 [CRLF] <html> <!-- Some HTML Content. --> </html> HTTP/1.1 500 Internal Server Error HTTP/1.1 201 Created Location: /view/7 [CRLF] Some message goes here. Response Status Response Headers Response Body (content) Response Status Response Header Response Body Response Status
  • 8. Standard Set of Methods ● GET - read data and not change it. ● PUT - update capabilities ● POST - create subordinate resources ● DELETE - delete a resource ● OPTIONS - ‘What methods are allowed’ ● HEAD - HTTP header
  • 9. A typical HTTP REST URL: http://my.store.com/fruits/list?category=fruit&limit=20 • The protocol identifies the transport scheme that will be used to process and respond to the request. • The host name identifies the server address of the resource. • The path and query string can be used to identify and customize the accessed resource. protocol host name path to a resource query string
  • 10. RESTful Application Cycle Resources are identified by URIs Clients communicate with resources via requests using a standard set of methods Requests and responses contain resource representations in formats identified by media types. Responses contain URIs that link to further resources
  • 11. Examples of Rest URIs Insert new customer in a system POST http://www.example.com/customers/1234 5 Read a customer with customer ID GET http://www.example.com/customers/3324 5 Read all orders with customer ID GET http://www.example.com/customers/3324 5/orders
  • 12. JAX-RS is a Java standard API for REST services: • Services are annotation driven • Provides support for data binding.(JAX-B) • Provides advanced APIs for content negotiation.(@Produces/@Consumes)
  • 13. SOAP vs. REST: Overview Both SOAP and REST are front-end technologies. SOAP – Simple Object Access Protocol  Supports a variety of transports (HTTP, JMS, etc.) and integrates with a variety of web service standards.  Typically used to pass contractually structured data between applications.  Bound to xml.  Uses SOAP envelope and then HTTP (or FTP/SMTP) to transfer the data.  Slower performance and scalability is a bit complex. Caching not possible.
  • 14. REST - Representational State Transfer  Architectural style  Simple point-to-point communication using well-established HTTP verbs, protocols, and standards.  Supports many different data formats like JSON, XML etc.  Performance and scalability, caching.  Lightweight, easy to consume.  Widely and frequently used. SOAP vs. REST: Overview
  • 15.  - XML-based protocol  - How to access the service and what operations are performed  Broadly consists of:  - Types  - Operation  - Binding WSDL- Webservices Description Language
  • 16. Connection point Client-server response Client-server request WSDL Example  Name of the service Parameter of the ws Request-response operation targetNamespace, default and other namespaces
  • 17. WSDL Example- contd.  Define binding transport Endpoint URI Connect port and binding
  • 18. Restful Webservices ● A RESTful Web service follows four basic design principles: o Uses HTTP methods o Be stateless as far as possible o Expose directory/folder structure-like URI o Transfer XML, JSON, or both
  • 19. Java API for RESTful Web Services (JAX-RS) vs Spring MVC Some Guidelines for choosing your solution: • Both JAX-RS and Spring MVC can produce REST services. • Spring MVC is a web application framework that can be used as service framework. – Provides better validation – Supports internationalization • JAX-RS is a primarily a services framework. – Provides support for WADL generation – Can use CXF interceptors, filters, etc. • Match the framework to the needs and purpose of the project. • Don’t mix both in same web application unless you need unique features from each. – If your project needs both, consider separate web applications.
  • 20. Spring mvc architecture ● Spring web MVC framework
  • 21. RESTful support in Spring ● Controllers can handle requests for all HTTP methods, including the four primary REST methods: GET, PUT, DELETE, and POST. ● The @PathVariable annotation enables controllers to handle requests for parameterized URLs (URLs that have variable input as part of their path). ● Resources can be represented in a variety of ways using Spring views and view resolvers, including View implementations for rendering model data as XML, JSON, Atom, and RSS. The representation best suited for the client can be chosen using ContentNegotiatingViewResolver. ● View-based rendering can be bypassed altogether using the @ResponseBody annotation and various HttpMethodConverter implementations. ● Similarly, the @RequestBody annotation, along with HttpMethodConverter implementations, can convert inbound HTTP data into Java objects passed in to a controller’s handler methods. ● Spring applications can consume REST resources using RestTemplate
  • 27. Next topics - Securing Restful Services - Open source frameworks JERSEY, RESTEASY
  • 28. References - Restful Webservice: Leonrard Richardson and Sam Ruby//O’Reilly - RESTful Web Services Cookbook: Subbu Allamraju//O’Reilly - Roy Fieldings dissertation - http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm - Spring in Action, 4th Edition, Craig Walls//Manning

Editor's Notes

  1. Agenda: Overview of REST architecture, Basics of HTTP, Implementation with Spring and examples.
  2. CSS, Visibility, Scalability
  3. Patch method – partial update
  4. 1
  5. Traversing the response directory structure by asking specific information based on ID.
  6. Why use JAX-RS. Comparisons with spring mvc implementation.
  7. A client program connecting to a web service can read the WSDL to determine what functions are available on the server. Any special datatypes used are embedded in the WSDL file in the form of XML Schema. The client can then use SOAP to actually call one of the functions listed in the WSDL.
  8. Suffix: http://example/myapp/accounts/list.html Parameter: http://myserver/myapp/accounts/list?format=xls