REST (Representational State Transfer) is a design pattern for developing web services. It uses several standards like HTTP, URL, XML, and JSON. Resources are identified by URIs and clients communicate with resources via standard HTTP methods like GET, POST, PUT, and DELETE. Responses contain representations of resources in formats like XML and JSON. REST services aim to be stateless, cacheable, have a uniform interface, and use hypermedia as the engine of application state.
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
RESTful web services using java and spring
1.
2. By Muhammad Junaid
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. By Muhammad Junaid
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. By Muhammad Junaid
Rest API Concepts
Services exposed to internet for programmatic
access
Users can be either producers or consumers or
both
Eg : api.twitter.com
Data can be returned in the form of XML /JSON
/ etc
Helps developers to parse data.
Messages can be exchanged in any kind of
HTTP method
5. By Muhammad Junaid
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.)
6. By Muhammad Junaid
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)
7. By Muhammad Junaid
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)
8. By Muhammad Junaid
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
9. By Muhammad Junaid
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
10. By Muhammad Junaid
Action/Verb
/orders
GET - list all orders
POST - submit a new order
/orders/{order-id}
GET - get an order representation
PUT - update an order
DELETE - cancel an order
/orders/average-sale
GET - calculate average sale
/customers
GET - list all customers
POST - create a new customer
/customers/{cust-id}
GET - get a customer representation
DELETE- remove a customer
/customers/{cust-id}/orders
GET - get the orders of a customer
11. By Muhammad Junaid
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
12. By Muhammad Junaid
Representations (MediaType)
XML
<COURSE>
<ID>CS2650</ID>
<NAME>Distributed Multimedia Software</NAME>
</COURSE>
JSON
{
“course”: {
“id”: “CS2650”
“name”: “Distributed Multimedia Software”
}
}
13. By Muhammad Junaid
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
14. By Muhammad Junaid
Examples of Rest URIs
Insert new customer in a
system
POST
http://www.example.com/customers/12345
Read a customer with
customer ID
GET
http://www.example.com/customers/33245
Read all orders with
customer ID
GET
http://www.example.com/customers/33245/orde
rs
15. By Muhammad Junaid
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)
16. By Muhammad Junaid
Jersey
One of the libraries that implements JAX-RS
, from Oracle.
Other libraries in the market -* Apache CXF ,
an open source Web service framework .
* RESTeasy , JBoss 's implementation .
* Restlet .
* Apache Wink , Apache Software Foundation Incubator .
* WebSphere Application Server from IBM.
Choice of library doesn’t matter .
18. By Muhammad Junaid
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.
19. By Muhammad Junaid
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
20. By Muhammad Junaid
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
21. By Muhammad Junaid
Web.xml configuration
<servlet>
<servlet-name>AccountService</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AccountService</servlet-name>
<url-pattern>services/*</url-pattern>
</servlet-mapping>
22. By Muhammad Junaid
@Path
Sets the path to base URL + /your_path. The
base URL is based on your application
name, the servlet and the URL pattern from
the web.xml configuration file.
24. By Muhammad Junaid
@Produces
@Produces defines which MIME type is
delivered by a method annotated with
@GET. In
the example text ("text/plain") is produced.
Other examples would be "application/xml"
or "application/json"