44
RESTful Web Services with Spring Explained Using Airport Symbols Arjen Poutsma SpringSource

RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Embed Size (px)

Citation preview

Page 1: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RESTful Web Services with Spring

Explained Using Airport Symbols

Arjen PoutsmaSpringSource

Page 2: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

About me

• Fifteen years of experience in Enterprise Software Development

• Six years of Web service experience

• Development lead of Spring Web Services

• Now working on Spring 3.0

• Contributor to various Open Source frameworks: (XFire, Axis2, NEO, ...)

Page 3: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Agenda

• What is REST?

• RESTful architecture & design

• REST in Java

• REST in Spring

Page 4: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

What is REST?

Page 5: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

What is REST?

• Representational State Transfer

• Architectural style

• Architectural basis for HTTP

Page 6: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Resources

• Typically nouns

• Customer

• Orders

• Shopping cart

• Expressed by URIs

Page 7: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Uniform Interface

Page 8: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Uniform Interface

Page 9: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Uniform Interface

Page 10: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Uniform Interface

Page 11: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

GET

• Retrieves Representation of Resource

• Safe operation

Page 12: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

GET is Cacheable

• Servers returns ETag header

• Send on subsequent retrieval

• If not changed, 304 (Not Modified) is returned

Page 13: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

GET Example

GET /hotelsHost: example.com…

Page 14: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 200 OKDate: …Content-Length: 1456Content-Type:application/xml

<hotels>…</hotels>

GET Example

GET /hotelsHost: example.com…

Page 15: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Conditional GETGET /hotelsHost: example.com…

Page 16: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 200 OKDate: …ETag: "b4bdb3"Content-Length: 1456…

Conditional GETGET /hotelsHost: example.com…

Page 17: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 200 OKDate: …ETag: "b4bdb3"Content-Length: 1456…

Conditional GETGET /hotelsHost: example.com…

GET /hotelsIf-None-Match:"b4bdb3" Host: example.com…

Page 18: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 200 OKDate: …ETag: "b4bdb3"Content-Length: 1456…

Conditional GETGET /hotelsHost: example.com…

HTTP/1.1 304 Not Modified

Date: …ETag: "b4bdb3"Content-Length: 0

GET /hotelsIf-None-Match:"b4bdb3" Host: example.com…

Page 19: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

PUT

• Updates resource

• Creates resource, when the destination URI is known

• Idempotent

Page 20: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

PUT Example

PUT /hotels/2Host: example.com

<hotel>…</hotel>

Page 21: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 201 CreatedDate: …Content-Length: 0

PUT Example

PUT /hotels/2Host: example.com

<hotel>…</hotel>

Page 22: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

POST

• Creates new Resource

• Child of other Resource

• Response Location header is used to indicate URI of child

Page 23: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

POST Example

POST /hotels/1/ bookingsHost: example.com

<booking>…</booking>

Page 24: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 201 CreatedDate: …Content-Length: 0Location: http://example.com/hotels/1/bookings/50…

POST Example

POST /hotels/1/ bookingsHost: example.com

<booking>…</booking>

Page 25: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

DELETE

• Deletes a resource

• Idempotent

Page 26: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

DELETE Example

DELETE /hotels/3Host: example.com…

Page 27: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

HTTP/1.1 204 No ContentDate: …Content-Length: 0…

DELETE Example

DELETE /hotels/3Host: example.com…

Page 28: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Representations

• Access resource through representations

• More that one representation possible

• Desired representation in Accept header

• Or file extension

• Delivered representation show in Content-Type

Page 29: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Stateless conversation

• Server does not maintain state

• No HTTP Session!

• Client maintains state through links

• Very scalable

• Loose coupling

Page 30: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Hypermedia• Resources contain links

• Client state transitions are made through these links

• Links are provided by server

• XLink

• Seamless evolution

Page 31: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RESTful architecture

Page 32: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RESTful Architecture

+ getOrders()+ submitOrders()+ getOrderDetails()+ getOrdersForCustomers()+ updateOrder()+ addOrderItem()+ cancelOrder()

OrderManagementService

+ getCustomers()+ addCustomer()+ getCustomerDetails()+ updateCustomer()+ deleteCustomer()

CustomerManagementService

Page 33: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RESTful Architecture

GETPUTPOSTDELETE

«interface»Resource

GET - list all ordersPUT - unusedPOST - add a new orderDELETE - unused

/orders

GET - get order detailsPUT - update orderPOST - add itemDELETE - cancel order

/orders/{id}

GET - list all customersPUT - unusedPOST - add new customerDELETE - unused

/customers

GET - get customer detailsPUT - update customerPOST - unusedDELETE - delete customer

/customers/{id}

GET - get all orders for customerPUT - unusedPOST - add orderDELETE - cancel all customer orders

/customers/{id}/orders

Page 34: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RESTful application design

1. Design URIs

2. Select representations

3. Identify method semantics

4. Select response codes

Page 35: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Advantages

• Widely supported

• Languages

• Scripts

• Browsers

• It works!

Page 36: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Advantages

• Scalability

• Redirects (versioning)

• Caching

• Different representations (no more attachment hell!)

• Identification

Page 37: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

REST in Spring 3

Page 38: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Uri Templates

@Controllerpublic class HotelController { @RequestMapping(value="/hotels/{id}", method=RequestMethod.GET) public String getHotel(@PathParam String id) {

// use id

return "someView"; } @RequestMapping(value="/hotels/{id}/bookings", method=RequestMethod.POST) public void addBooking(@PathParam String id, Booking booking) { // store booking }

}

Page 39: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Representations

• View resolution

• XML

• Spring-WS OXM

• JSON

• Atom, RSS

• Flex

Page 40: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RestTemplate

• RestTemplate as core client-side component

• Similar to other templates in Spring

• JdbcTemplate

• JmsTemplate

• WebServiceTemplate

Page 41: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RestTemplate methods

• getForObject

• Performs GET and converts

• put

• Performs PUT

• postForLocation

• Performs POST, and retrieves Location header

• delete

Page 42: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

RestTemplateString uri = "http://example.com/hotels/{id}"template = new RestTemplate();HotelList result = template.getForObject(uri, HotelList.class, "1");

Booking booking = // create booking objecturi = "http://example.com/hotels/{id}/bookings";Map<String, String> vars = Collections.singletonMap("id", "1");URI location = template.postForLocation(uri, booking, vars);

template.delete(location.toString());

template.execute(uri, HttpMethod.GET, myRequestCallback, myResponseCallback);

Page 43: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Time Line

• Part of Spring 3.0

• PathParam in 3.0 M1 (October)

• Rest follows later

• 3.0 Final planned for early 2009

Page 44: RESTful Web Services with Spring - jaoo.dkjaoo.dk/.../slides/ArjenPoutsma_RESTFul_Web_Services_with_Spring.pdfRESTful Web Services with Spring ... HTTP/1.1 200 OK ... Conditional GET

Q & A