having fun with the resttemplate - goto...

21
Having Fun with the RestTemplate Arjen Poutsma SpringSource VMware woensdag 6 oktober 2010

Upload: others

Post on 23-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

Having Fun with the RestTemplate

Arjen PoutsmaSpringSource

VMware

woensdag 6 oktober 2010

Page 2: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Overview

• REST in 10 minutes

• RestTemplate

woensdag 6 oktober 2010

Page 3: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

REST in 10 minutes

woensdag 6 oktober 2010

Page 4: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

REST

woensdag 6 oktober 2010

Page 5: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Identification

• Resources

• Typically nouns

• URIs

http://example.com/orders?year=2008http://example.com/customers/1234http://example.com/orders/2007/10/776654

woensdag 6 oktober 2010

Page 6: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Uniform Interface

woensdag 6 oktober 2010

Page 7: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Uniform Interface

woensdag 6 oktober 2010

Page 8: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Uniform Interface

GET Read

PUT Create or Update

POST Create

DELETE Delete

woensdag 6 oktober 2010

Page 9: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Uniform Interface

woensdag 6 oktober 2010

Page 10: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

Uniform Interface

woensdag 6 oktober 2010

Page 11: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

HATEOAS

• Hypermedia as the Engine of Application State

• No server-side state

• State transfer through links

• Google

woensdag 6 oktober 2010

Page 12: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

GET /service/customers/1234 HTTP 1.1Host: www.example.comUser-Agent: XYZ 1.1Accept: text/html,application/xml;q=0.9,*/*;q=0.8If-Modified-Since: Thu, 18 Dec 2008 16:47:31 GMT If-None-Match: "600028c-59fb-474f6852c9dab"

HTTP/1.1 304 Not ModifiedDate: Fri, 19 Dec 2008 19:36:25 GMTLast-Modified: Thu, 18 Dec 2008 16:47:31 GMTEtag: "600028c-59fb-474f6852c9dab"Content-Length: 7160Content-Type: application/xml

<?xml version=‘1.0’ encoding=‘utf-8’ ?>

Self-Descriptive Messages

woensdag 6 oktober 2010

Page 13: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

REST

woensdag 6 oktober 2010

Page 14: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

REST

woensdag 6 oktober 2010

Page 15: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

REST

woensdag 6 oktober 2010

Page 16: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

RestTemplate

woensdag 6 oktober 2010

Page 17: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

RestTemplate

• Introduced in Spring 3.0

• RestTemplate as core client-side component

• Similar to other templates in Spring

• JdbcTemplate

• JmsTemplate

woensdag 6 oktober 2010

Page 18: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

RestTemplate methods

• getForObject

• Performs GET and converts

• put

• Performs PUT

• postForLocation

• Performs POST, and retrieves Location header

• deletewoensdag 6 oktober 2010

Page 19: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

SpringSource Confidential. Do not distribute without express permission

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, );

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

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

woensdag 6 oktober 2010

Page 20: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

Demo’s

woensdag 6 oktober 2010

Page 21: Having Fun with the RestTemplate - GOTO Conferencegotocon.com/dl/jaoo-aarhus-2010/slides/ArjenPoutsma_HavingFunW… · HTTP/1.1 304 Not Modified Date: Fri, 19 Dec 2008 19:36:25 GMT

Q & A

woensdag 6 oktober 2010