restful web services using java and spring
TRANSCRIPT
RESTFUL SERVICE USING JAVA AND
SPRING
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)
By Muhammad Junaid
REST - not a Standard● But it uses several standards:
o HTTPo URLo 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.1Host: www.pitt.edu
HTTP/1.1 200 OKContent-Type: text/html
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
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.)
By Muhammad Junaid
Sample Client Requests:GET /view?id=1 HTTP/1.1 Request HeadersUser-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: IEContent-Type: application/x-www-form-urlencoded Request
Headers
name=x&id=2 Request Body (e.g. form parameters)
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)
By Muhammad Junaid
Sample Server Responses:
HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 1337[CRLF]<html> <!-- Some HTML Content. --></html>
HTTP/1.1 500 Internal Server Error
HTTP/1.1 201 CreatedLocation: /view/7[CRLF]Some message goes here.
Response Status
Response Headers
Response Body (content)
Response StatusResponse Header
Response Body
Response Status
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
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
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
By Muhammad Junaid
Representations (MediaType) XML
<COURSE><ID>CS2650</ID><NAME>Distributed Multimedia Software</NAME>
</COURSE> JSON
{“course”: {“id”: “CS2650”“name”: “Distributed Multimedia Software”}
}
By Muhammad Junaid
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
By Muhammad Junaid
Examples of Rest URIsInsert 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/orders
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)
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 .
By Muhammad Junaid
Big Picture
By Muhammad Junaid
SOAP vs. REST: OverviewBoth 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.
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
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
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>
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.
By Muhammad Junaid
@GET, @PUT, @POST, @DELETE, ...
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"
By Muhammad Junaid
URI Mappings example