REST based web applications with Spring 3

27
© 2010 SpringSource, A division of VMware. All rights reserved REST based web applications with Spring 3 Oliver Gierke - Senior Consultant

description

Slides of the talk I gave at Java User Group Frankfurt, March 31st 2010.

Transcript of REST based web applications with Spring 3

Page 1: REST based web applications with Spring 3

© 2010 SpringSource, A division of VMware. All rights reserved

REST based web applications with Spring 3

Oliver Gierke - Senior Consultant

Page 2: REST based web applications with Spring 3

Agenda

2

Intro

What is REST all about?

Recap: @Controller

What‘s new in Spring MVC 3.0?

What‘s beyond Spring MVC

Demo

Page 3: REST based web applications with Spring 3

About

Oliver Gierke - Senior Consultant

OpenSource enthusiast

(OR)Persistence, Web, Integration

[email protected]

http://www.olivergierke.de / olivergierke

3

Hi! My name is...

Page 4: REST based web applications with Spring 3

SpringSource - a division of VMware

Spring Framework• exhaustive ecosystem

Roo

Groovy / Grails

Tomcat / tc Server / Hyperic

4

Page 5: REST based web applications with Spring 3

REST

5

Page 6: REST based web applications with Spring 3

The architecture of the web

6

Page 7: REST based web applications with Spring 3

Resources

7

IdentifiersVerbs

RepresentationsHypermedia

Page 8: REST based web applications with Spring 3

Spring MVC

8

Page 9: REST based web applications with Spring 3

Spring MVC

9

Model View Controller

Page 10: REST based web applications with Spring 3

Recap: Spring MVC 2.5

10

Page 11: REST based web applications with Spring 3

@Controller@Controller@RequestMapping(„/addresses“)public class AddressController {

@RequestMapping(value=“/address“, method = GET) public String showAddress( @RequestParam Long id, Model model) {…}

}

11

Page 12: REST based web applications with Spring 3

Other special types

Strongly typed request parameterView name

Strongly typed methods

@RequestMapping(value=“/address“, method = GET)public String showAddress( @RequestParam Long id, Model model) {…}

12

Page 13: REST based web applications with Spring 3

What‘s new in Spring MVC 3.0

@PathVariable

Content negotiation

ResponseBody / RequestBody / ResponseStatus

Declarative validation (JSR-303)

13

Page 14: REST based web applications with Spring 3

@PathVariable

@RequestMapping(value=“/addresses/{id}“, method = GET)public String showAddress( @PathVariable Long id, Model model) {…}

14

Page 15: REST based web applications with Spring 3

Content negotiation

http://myapp.com/addresses/1.xmlhttp://myapp.com/addresses/1.jsonhttp://myapp.com/addresses/1.html

Accept: application/xmlAccept: application/jsonAccept: application/html

15

Page 16: REST based web applications with Spring 3

@ResposeBody

@RequestMapping(value=“/addresses/{id}“, method = GET)@ResponseBodypublic Address showAddress( @PathVariable Long id, Model model) {…}

16

Page 17: REST based web applications with Spring 3

@ResponseStatus

@ResponseStatus(NOT_FOUND)public class MyException extends RuntimeException {…}

17

Page 18: REST based web applications with Spring 3

@ResponseStatus

@RequestMapping(value=“/addresses/{id}“, method = GET)public String showAddress( @PathVariable Long id, Model model) {

if (id == null) throw new MyException(); …}

18

Page 19: REST based web applications with Spring 3

@RequestBody

@RequestMapping(value=“/addresses“, method = POST)@ResponseStatus(CREATED)public void createAddress( @RequestBody Address address) {…}

19

Page 20: REST based web applications with Spring 3

Declarative Validation

@RequestMapping(value=“/addresses“, method = POST)@ResponseStatus(CREATED)public void createAddress( @RequestBody @Valid Address address) {…}

20

Page 21: REST based web applications with Spring 3

What‘s new in Spring MVC 3.0

New data binding and type conversion service

Object-XML-Mapping

HiddenHttpMethodFilter

XML namespace

21

Page 22: REST based web applications with Spring 3

Demo

22

Page 23: REST based web applications with Spring 3

23

Where do wego from here?

Page 24: REST based web applications with Spring 3

24

Spring Ecosystem

Spring WebFlowSpring Integration

Spring BatchSpring Roo

Spring Security

Spring WebServices

Page 25: REST based web applications with Spring 3

Demo

25

Page 26: REST based web applications with Spring 3

Questions

26

Page 27: REST based web applications with Spring 3

Resources

Sample: http://github.com/olivergierke/spring-rest

Petcare: http://src.springsource.org/svn/spring-samples/petcare/trunk

Photos: http://www.sxc.hu/

27