Java/J2ee Programming Training
Java Rest
Page 2Classification: Restricted
Agenda
• Roy fielding has authored REST specification.
• REST webservices
Page 3Classification: Restricted
• concepts of rest are very closely related to HTTP
• Http is used by many of the web pages to send a request and receive an response.
Page 4Classification: Restricted
HTTPExchange
CLIENT SERVERHTTP
Request
CLIENT SERVER
HTTP
Response
Page 5Classification: Restricted
HTTP
• Hyper text transfer protocol
• way to exchange information
• the text we exchange is called hyper text
• its structured form of text
• hypertext contains links to other text i.e hyperlinks
• hypertext is written using a markup language called
• HYPER TEXT MARKUP LANGUAGE.
Page 6Classification: Restricted
Resource Locations
• REST API have URI and address
CLIENT SERVERDATA
Page 7Classification: Restricted
HTML RESPoNSE
• Html response to display the weather information has css, styles….to display weather information to
user.
Page 8Classification: Restricted
Weather Web Service
• RestAPI for weather service has data represented in JSON or XML format.
• API are address.
• developers should decide what those addresses be
• resource based addresses
• weatherapp.com/weatherLookup.do?zip =12345;
Page 9Classification: Restricted
Resource based URI
• weatherapp.com/zipcode/12345
• weatherapp.com/zipcode/56789
• weatherapp.com/country/usa
Page 10Classification: Restricted
HTTP Methods
• GET
• PUT
• POST
• DELETE
Page 11Classification: Restricted
Metadata
• HTTP status code
• successful-200OK
• error – 500 ->server error
standard status code is sent by the REST services to the client to interpret the message accordingly.
Page 12Classification: Restricted
Message Headers
• Client and server can exchange information in any format.
• content information is recorded in HTTP headers
• xml- text /xml
• JSON - > application/json
• A web service may return more than one format based on client request.
Page 13Classification: Restricted
Summary
• Resource based URI
• HTTP methods
• HTTP response codes
• Message Headers
• Content Negotiation
Page 14Classification: Restricted
Designing RESTFUL URI
• Messenger
• Post message
• like message
• profile information
Page 15Classification: Restricted
Diagram
Page 16Classification: Restricted
Restful Resource based URI
• Web Application URI…
Page 17Classification: Restricted
Restful Resource based URI
• best practices
Page 18Classification: Restricted
Static Websites
• uri that uniquely identifies each web page on the server
• static profile pages.
TOM.HT
ML
MIKE.HT
ML
TEST.HT
ML
Page 19Classification: Restricted
profiles/tom.html
profiles
tom.html
jack.html
fay.html
profiles/tom
profiles/{profileName}
Page 20Classification: Restricted
resources based uri
• messages/{messageID}
• messages/1
• messages/10
• Restful URI names should be nouns.
it should not have verb/action
its plural
messages/1
profiles/jack
They are not dependent on framework.
Page 21Classification: Restricted
RESOURCE URI for RELATIONS
Page 22Classification: Restricted
Resource Relations
• messages/{messageID}
• profiles/{profileName}
• messages/{messageID}/comments/{commentId}
• messages/{messageID}/shares/{shareID}
• messages/{messageID}/likes/{likeID}
first level
second level
Page 23Classification: Restricted
Collection URI
• Instance based uri
• A single instance of message is accessible by instance based uri
• have unique id to retrieve the instance
• collection based uri
• /messages
to access all the messages
• to access all the comments for messageid =2
• messages/2/comments
• /messages
• /messages/{messageID}/comments
• messages/{messageID}/shares
• messages/{messageID}/likes
Page 24Classification: Restricted
Filters Result
• paginationusing query parameters
• messages?offset=10%limit=10;
• offset( startingpoint)= starts at message#30
• limit( pagination ) fetches 10 messages starting from 30
Page 25Classification: Restricted
Filter parameters
• retrieve message based on date
• messages?year=2014&offset=10&limit=10
Page 26Classification: Restricted
Summary
• Instance based uri
• collection based uri
• Query parameter to achieve pagination and filtering
Page 27Classification: Restricted
HTTP Methods
• Action based URI
• getMessages.do?id=10
• Resource based URI
• /messages/10
/ get Messages .do?id = 10
/ messages /10
Page 28Classification: Restricted
HTTP Methods
Method
/getMessage.do?id=1
0
/messages/10 GET
/postMessage.do?id=
10
/messages/10 POST
/deleteMessage.do?id
=10
/messages/10 DELETE
/updateMessage.do?i
d=10
/messages/10 PUT
Page 29Classification: Restricted
Creating a New Message
• to create a new Message , request is made to collection URI
• /messages
• because we don’t have message id
• to create a profile
/profiles
• To create a new resource
• use POST
• response is messageID
Page 30Classification: Restricted
Collection URI Scenarios
METHOD RESOURCE URI COMMENTS
GET /messages get All Messages
DELETE /messages/10/comments delete all comments of
Message 10
GET /messages
POST /messages/10/comments create a new comment for
message 10
PUT /message/20/comments replace all comments for
message 20 with new list
Page 31Classification: Restricted
Method Idempotence
• PUT Vs POST
• put- is for update
• POST-> create
Page 32Classification: Restricted
HTTP Methods
• GET  READ ONLY
• does not make any changes on server.
• Write
• PUTupdate
• DELETEdelete
• POSTcreate
• cannot make multiple calls/duplicate calls
Page 33Classification: Restricted
Java code example
• count = 100;
• count =100;
• count = 100;
Methods are repeatable
vs
Non repeatable
Page 34Classification: Restricted
Java code example
• count = 100;
• count =100;
• count = 100;
Methods are repeatable
vs
Non repeatable
Page 35Classification: Restricted
DELETE
• making repeated calls to delete.
• /message/20
• does not make any changes because the message is deleted.
• delete is safe
Page 36Classification: Restricted
PUT ( UPDATE )
• making repeated calls to put.( update)
• /message/20
• updates the message 20
• repeated calls makes update same messages. ( Net effect is same ).
Page 37Classification: Restricted
POST( CREATE )
• /messages
• every time a post is made it creates a new message.
• data on server is inserted on each call .
• its not safe to make multiple calls using POST method
• .
Page 38Classification: Restricted
Idempotence
• Idempotence is the property of certain operations in mathematics and computer science, that can be
applied multiple times without changing the result beyond the initial application
• Idempotent
• GET
• PUT
• DELETE
• NON Idempotent
• POST
Page 39Classification: Restricted
Resource creation
• resource creation should be non-idempotent.
• update, delete, put should be idempotent
Page 40Classification: Restricted
Cache Get Responses
CACHE
Page 41Classification: Restricted
Browser Refresh
• browser refresh last request.
• if it was a post.
• browser protects from submitting duplicate request by prompting the user with an information
message.
Page 42Classification: Restricted
HTTP Response
CLIENT
REQUEST URI
response
Page 43Classification: Restricted
WebApplication
CLIENT REQUEST URIresponse
HTML
Page 44Classification: Restricted
RESTful Web Services
CLIENT REQUEST URIresponse
XML/JSON
Page 45Classification: Restricted
JSON
• A Client to REST API is browser which makes REST API calls
• sending response in JSON makes its easy for the JavaScript client to convert the response(JSON) to
JavaScript object
• text format
• xml format
Page 46Classification: Restricted
Message Entity
public class MessageEntity
{
private int longId;
private String message;
private Date dateCreated;
private String author;
}
Page 47Classification: Restricted
JSON Object returned in response to request
{
“id”: 10,
“message”:”Hello Tom”,
“created”: “2015-09-10”
“author”:”Jack”
}
Page 48Classification: Restricted
XML response
<messageEntity>
<id>10</id>
<message>Hello john</message>
<dateCreated>10-09-2015</dateCreated>
<author>John</author>
</messageEntity>
Page 49Classification: Restricted
Responses
<messageEntity>
<id>10</id>
<message>Hello john</message>
<dateCreated>10-09-2015</dateCreated>
<author>John</author>
</messageEntity>
{
“id”: 10,
“message”:”Hello Tom”,
“created”: “2015-09-10”
“author”:”Jack”
}
MESSAGE ID : 10
Page 50Classification: Restricted
REST
Representational State transfer
we are sending or receiving representation of resources
different representation for the same resources.
JSON
XML
TEXT
Page 51Classification: Restricted
Page 52Classification: Restricted
HTTP Headers
HTTP HEADERS
BODY
conent length
MIME TYPE
content Type
Page 53Classification: Restricted
Error Mesages
• status codes
200 OK
404 NOT FOUND
100-------------------------599
Page 54Classification: Restricted
classes of Error codes
1xx Informational Codes
2XX Success
200 OK
201 CREATED
204 NO CONTENT ( Eg: delete request, send an
acknowledgment without body)
3XX Redirection codes.
used by the server to redirect the client
300
304 not modified
400 Client error
client makes an error in request
forbidden access
404 Not found
Page 55Classification: Restricted
classes of Error codes
1xx Informational Codes
500 Internal Server error
200 OK
201 CREATED
204 NO CONTENT ( Eg: delete request, send an
acknowledgment without body)
3XX Redirection codes.
used by the server to redirect the client
300
304 not modified
400 Client error
client makes an error in request
forbidden access
404 Not found
Page 56Classification: Restricted
Page 57Classification: Restricted
Page 58Classification: Restricted
HATEOAS
• H-HyperMedia
• A-As
• T-The
• E-Engine
• O-Of
• A-Application
• S-State
Page 59Classification: Restricted
No SERVICE
DEFINITIONS
Page 60Classification: Restricted
Web Application
CLIENT REQUEST URIresponse
HTML
Page 61Classification: Restricted
Accessing Messages
{
“id”: 10,
“message”:”Hello Tom”,
“created”: “2015-09-10”
“author”:”Jack”
“commentsUri”:”api/messages/10/comments”
“likesUri”:”api/messages/10/likes”
“sharesUri”:”api/messages/10/shares”
}
CLIENT REQUEST URIresponse
Page 62Classification: Restricted
Accesesing Messages-- /messages
[
{
“id”: 10,
“message”:”Hello Tom”,
“created”: “2015-09-10”
“author”:”Jack”
},
{
”id”: 10,
“message”:”Hello Tom”,
“created”: “2015-09-10”
“author”:”Jack”
}
]
Page 63Classification: Restricted
Thank You

Java Rest