Top Banner
RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software Engineer SpringSource
51

RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Jun 13, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

RESTful Web Applications with

Spring 3.0

Arjen PoutsmaSenior Software Engineer

SpringSource

Page 2: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Speaker’s qualifications

• Fifteen years of experience in Enterprise Software Development

• Six years of Web service experience

• Development lead of Spring Web Services

• Working on Spring 3.0

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

Page 3: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Overview

• RESTful URLs

• URI templates

• Content negotiation

• HTTP method conversion

• ETag support

Page 4: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

RESTful URLs

Page 5: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Resources

• URLs are unique identifiers for Resources

• Typically nouns

• Customer

• Orders

• Shopping cart

Page 6: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

URLs[scheme:][//authority][path][?query][#fragment]

http://www.springsource.com

https://mybank.com

http://www.google.com/search?q=arjen%20poutsma

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#indexOf(int)

Page 7: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Paths

• Represents hierarchy

• Represents value for consumers

• Collections on higher levels

Page 8: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Example

Path Description

/hotels List of all hotels

/hotels/westindiplomat Details of Westin Diplomat

/hotels/westindiplomat/bookings List of bookings of Westin Diplomat

/hotels/westindiplomat/bookings/42584 Individual booking

Page 9: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

No Hierarchy?

Path Description

maps/24.9195,17.821 Commas

maps/24.9195;17.821 Semicolons

Page 10: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Query Variables

• Input for algorithms

• Get ignored by proxies

• Often abused• http://api.flickr.com/services/rest/?

method=flickr.photos.search&tags=penguin

• hotels?hotelId=westindiplomat

Page 11: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Do’s

• Hierarchies

• Collections

• Separate URLs for useful resources

• Queries abuse

• RPC in disguise

• Verbs in URLs

Dont’s

Page 12: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

URI Templates

Page 13: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

URI Templates

• URI-like string, containing one or more variable names

• Variables can be substituted for values to become a URI

• Helps to create nice, “RESTful” URLs

Page 14: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Examples

URI Template Request Variables

/hotels/{hotelId} /hotels/westindiplomat

hotelId= westindiplomat

/hotels/{hotelId}/bookings

/hotels/westindiplomat/

bookings

hotelId= westindiplomat

/hotels/{hotelId}/bookings/

{bookingId}

/hotels/westindiplomat/

bookings/21

hotelId= westindiplomatbookingId=21

Page 15: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

@PathVariable

• Spring 3.0 M1 introduced the @PathVariable annotation

• Allows you to use URI Templates in @MVC

Page 16: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

@Controller@RequestMapping("/hotels/{hotel}/**")public class HotelsController {

@RequestMapping public void handleHotel(@PathVariable("hotel") String hotel) { // ... }

@RequestMapping("bookings/{booking}") public void handleBooking(@PathVariable("hotel") String hotel, @PathVariable int booking) { // ... }

}

Example

Page 17: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

DemoURI Templates

Page 18: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Content Negotiation

Page 19: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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 20: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

ViewsMime Type View When

application/xml MarshallingView 3.0 M2/SWS 1.5

application/atom+xml AtomFeedView 3.0 M1

application/rss+xml RssFeedView 3.0 M1

application/json JsonView Spring-JS

Page 21: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

No DemoComing in Spring 3.0 M2!

Page 22: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

HTTP Method conversion

Page 23: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Uniform Interface

Page 24: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Uniform Interface

Page 25: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Uniform Interface

Page 26: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Uniform Interface

Page 27: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

GET

• Retrieves Representation of Resource

• Safe operation

Page 28: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

GET Example

GET /hotelsHost: example.com…

Page 29: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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

<hotels>…</hotels>

GET Example

GET /hotelsHost: example.com…

Page 30: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

PUT

• Updates resource

• Creates resource, when the destination URI is known

• Idempotent

Page 31: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

PUT Example

PUT /hotels/2Host: example.com

<hotel>…</hotel>

Page 32: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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

PUT Example

PUT /hotels/2Host: example.com

<hotel>…</hotel>

Page 33: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

POST

• Creates new Resource

• Child of other Resource

• Response Location header is used to indicate URI of child

Page 34: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

POST Example

POST /hotels/1/ bookingsHost: example.com

<booking>…</booking>

Page 35: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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 36: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

DELETE

• Deletes a resource

• Idempotent

Page 37: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

DELETE Example

DELETE /hotels/3Host: example.com…

Page 38: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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

DELETE Example

DELETE /hotels/3Host: example.com…

Page 39: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

One Problem...

• HTML only supports GET and POST

• Possible workarounds:

• Javascript

• POST with hidden method parameter

•HiddenHttpMethodFilter

Page 40: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

DemoHTTP Method Conversion

Page 41: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

ETag Support

Page 42: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

GET is Cacheable

• Servers returns ETag header

• Send on subsequent retrieval

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

Page 43: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Conditional GETGET /hotelsHost: example.com…

Page 44: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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

Conditional GETGET /hotelsHost: example.com…

Page 45: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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

Conditional GETGET /hotelsHost: example.com…

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

Page 46: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

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 47: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

ShallowEtagHeaderFilter

• Introduced in Spring 3.0 M1

• Creates ETag header based on MD5 of rendered view

• Saves bandwidth only

• Deep ETag support comes in M2

• Through @RequestHeader

Page 48: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

DemoETag Support

Page 49: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Summary

• REST uses the Web like it should be

• Spring 3.0 will help you create RESTful Web Applications

Page 50: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

Q&A

Page 51: RESTful Web Applications with Spring 3 - jaoo.dkjaoo.dk/dl/2009/Web/RESTful_Web_Applications_with_Spring_3.pdf · RESTful Web Applications with Spring 3.0 Arjen Poutsma Senior Software

SpringSource Confidential. Do not distribute without express permission

Why not JAX-RS?

• Does not help the current @MVC users

• Semantics are different

• No mix-and-match

• Web Service focus

• We do plan to provide JAX-RS integration in the future