multi client development with spring

49
© 2013 SpringSource, by VMware Multi Client Development with Spring Josh Long (之春) @starbuxman joshlong.com [email protected] slideshare.net/joshlong

Upload: josh-long

Post on 12-Jan-2015

1.115 views

Category:

Technology


1 download

DESCRIPTION

No application is an island and this is more obvious today than ever as applications extend their reach into people's pockets, desktops, tablets, TVs, Blu-ray players and cars. What's a modern developer to do to support these many platforms? In this talk, join Josh Long to learn how Spring can extend your reach through (sometimes Spring Security OAuth-secured) RESTful services exposed through Spring MVC, HTML5 and client-specific rendering thanks to Spring Mobile, and powerful, native support for Android with Spring Android. Josh Long is the Spring developer advocate. He is the lead author on Apress’ Spring Recipes, 2nd Edition, and a SpringSource committer and contributor. When he's not hacking on code, he can be found at the local Java User Group or at the local coffee shop. Josh likes solutions that push the boundaries of the technologies that enable them. His interests include scalability, BPM, grid processing, mobile computing and so-called "smart" systems. He blogs at blog.springsource.org or joshlong.com and on Twitter as @starbuxman.

TRANSCRIPT

Page 1: Multi Client Development with Spring

© 2013 SpringSource, by VMware

Multi Client Development with Spring

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong

Page 2: Multi Client Development with Spring

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong

Page 3: Multi Client Development with Spring

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong

Contributor To:

•Spring Integration•Spring Batch •Spring Hadoop•Activiti Workflow Engine

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong

Page 4: Multi Client Development with Spring

4

Why Are We Here?

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

-Bob Martin

”“

Page 5: Multi Client Development with Spring

5

do NOT reinvent the Wheel!

Why Are We Here?

Page 6: Multi Client Development with Spring

6

Spring’s aim:

bring simplicity to java development

web tier &

RIAservice tier batch

processingintegration & messaging

data access

/ NoSQL / Big Data

mobile

tc ServerTomcat

Jetty

lightweightCloudFoundry

Google App EngineAmazon BeanStalk

CloudBeesOpenShift

the cloud

WebSphereJBoss ASWebLogic

(on legacy versions, too!)

traditional

The Spring framework

Page 7: Multi Client Development with Spring

7

Spring Web Support

Page 8: Multi Client Development with Spring

Thin, Thick, Web, Mobile and Rich Clients: Web Core

§Spring Dispatcher Servlet• Objects don’t have to be web-specific.

• Spring web supports lower-level web machinery: ‘• HttpRequestHandler (supports remoting: Caucho, Resin, JAX RPC)• DelegatingFilterProxy. • HandlerInterceptor wraps requests to HttpRequestHandlers• ServletWrappingController lets you force requests to a servlet through the Spring Handler chain • OncePerRequestFilter ensures that an action only occurs once, no matter how many filters are applied. Provides a nice way to avoid duplicate

filters

• Spring provides access to the Spring application context using WebApplicationContextUtils, which has a static method to look up the context, even in environments where Spring isn’t managing the web components

Page 9: Multi Client Development with Spring

Thin, Thick, Web, Mobile and Rich Clients: Web Core

§Spring provides the easiest way to integrate with your web framework of choice• Spring Faces for JSF 1 and 2

• Struts support for Struts 1

• Tapestry, Struts 2, Stripes, Wicket, Vaadin, Play framework, etc.

• GWT, Flex

Page 10: Multi Client Development with Spring

Thin, Thick, Web, Mobile and Rich Clients: Spring MVC

10

Page 11: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController {

}

11

The Anatomy of a Spring MVC @Controller

Page 12: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”) public String processTheRequest() { // ... return “home”; }

}

12

The Anatomy of a Spring MVC @Controller

GET http://127.0.0.1:8080/url/of/my/resource

Page 13: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public String processTheRequest() { // ... return “home”; }

}

13

The Anatomy of a Spring MVC @Controller

GET http://127.0.0.1:8080/url/of/my/resource

Page 14: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public String processTheRequest( HttpServletRequest request) { String contextPath = request.getContextPath(); // ... return “home”; }

}

14

The Anatomy of a Spring MVC @Controller

GET http://127.0.0.1:8080/url/of/my/resource

Page 15: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public String processTheRequest( @RequestParam(“search”) String searchQuery ) { // ... return “home”; }

}

15

The Anatomy of a Spring MVC @Controller

GET http://127.0.0.1:8080/url/of/my/resource?search=Spring

Page 16: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/{id}”, method = RequestMethod.GET) public String processTheRequest( @PathVariable(“id”) Long id) { // ... return “home”; }

}

16

The Anatomy of a Spring MVC @Controller

GET http://127.0.0.1:8080/url/of/my/2322

Page 17: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/{id}” ) public String processTheRequest( @PathVariable(“id”) Long customerId, Model model ) {

model.addAttribute(“customer”, service.getCustomerById( customerId ) ); return “home”; }

@Autowired CustomerService service;

}

17

The Anatomy of a Spring MVC @Controller

GET http://127.0.0.1:8080/url/of/my/232

Page 18: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public String processTheRequest( HttpServletRequest request, Model model) { return “home”; }

}

18

The Anatomy of a Spring MVC @Controller

Page 19: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public View processTheRequest( HttpServletRequest request, Model model) { return new XsltView(...); }

}

19

The Anatomy of a Spring MVC @Controller

Page 20: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public ResponseEntity<byte[]> processTheRequest( HttpServletRequest request, Model model) { return new ResponseEntity<byte[]>( bytes, ...) ; }

}

20

The Anatomy of a Spring MVC @Controller

Page 21: Multi Client Development with Spring

Not confidential. Tell everyone.

DEMO§Demos• Simple Spring MVC based Application

Page 22: Multi Client Development with Spring

22

the new hotness...

Page 23: Multi Client Development with Spring

23

dumb terminals ruled the earth....

Page 24: Multi Client Development with Spring

24

then something magical happened: the web

Page 25: Multi Client Development with Spring

25

but the web didn’t know how to do UI state... so we hacked...

Page 26: Multi Client Development with Spring

....then the web leveled up

26

Page 27: Multi Client Development with Spring

How Powerful is JavaScript? ...It Boots Linux!!

27

Page 28: Multi Client Development with Spring

28

REST

Page 29: Multi Client Development with Spring

CONFIDENTIALCONFIDENTIAL

Thin, Thick, Web, Mobile and Rich Clients: REST

§Origin• The term Representational State Transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation.

§His paper suggests these four design principles:• Use HTTP methods explicitly.

• POST, GET, PUT, DELETE• CRUD operations can be mapped to these existing methods

• Be stateless.• State dependencies limit or restrict scalability

• Expose directory structure-like URIs.• URI’s should be easily understood

• Transfer XML, JavaScript Object Notation (JSON), or both.• Use XML or JSON to represent data objects or attributes

29

Page 30: Multi Client Development with Spring

REST on the Server

§Spring MVC is basis for REST support• Spring’s server side REST support is based on the standard controller model

§ JavaScript and HTML5 can consume JSON-data payloads

Page 31: Multi Client Development with Spring

REST on the Client

§RestTemplate• provides dead simple, idiomatic RESTful services consumption

• can use Spring OXM, too.

• Spring Integration and Spring Social both build on the RestTemplate where possible.

§Spring supports numerous converters out of the box• JAXB

• JSON (Jackson)

Page 32: Multi Client Development with Spring

CONFIDENTIALCONFIDENTIAL

Building clients for your RESTful services: RestTemplate

32

§Google search example

§Multiple parameters

RestTemplate restTemplate = new RestTemplate();

String url = "http://example.com/hotels/{hotel}/bookings/{booking}";

String result = restTemplate.getForObject(url, String.class, "42", “21”);

RestTemplate restTemplate = new RestTemplate();

String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}";

String result = restTemplate.getForObject(url, String.class, "SpringSource");

Page 33: Multi Client Development with Spring

CONFIDENTIALCONFIDENTIAL

Thin, Thick, Web, Mobile and Rich Clients: RestTemplate

§RestTemplate class is the heart the client-side story• Entry points for the six main HTTP methods

• DELETE - delete(...)• GET - getForObject(...)• HEAD - headForHeaders(...)• OPTIONS - optionsForAllow(...)• POST - postForLocation(...)• PUT - put(...)• any HTTP operation - exchange(...) and execute(...)

33

Page 34: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/resource”, method = RequestMethod.GET) public @ResponseBody Customer processTheRequest( ... ) { Customer c = service.getCustomerById( id) ; return c; }

@Autowired CustomerService service;

}

34

The Anatomy of a Spring MVC @Controller

Page 35: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/someurl”, method = RequestMethod.POST) public String processTheRequest( @RequestBody Customer postedCustomerObject) { // ... return “home”; }

}

35

The Anatomy of a Spring MVC @Controller

POST http://127.0.0.1:8080/url/of/my/someurl

Page 36: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController { @RequestMapping(value=”/url/of/my/{id}”, method = RequestMethod.POST) public String processTheRequest( @PathVariable(“id”) Long customerId, @RequestBody Customer postedCustomerObject) { // ... return “home”; }

}

36

The Anatomy of a Spring MVC @Controller

POST http://127.0.0.1:8080/url/of/my/someurl

Page 37: Multi Client Development with Spring

Spring MVC configuration - config

@Controller public class CustomerController {

@ResponseStatus( HttpStatus.CREATED ) @RequestMapping(value=”/url/of/my/{id}”, method = RequestMethod.POST) public ResponseEntity<?> processTheRequest() { // ... }

}

37

The Anatomy of a Spring MVC @Controller

POST http://127.0.0.1:8080/url/of/my/someurl

Page 38: Multi Client Development with Spring

What About the Browsers, Man?

• Problem: modern browsers only speak GET and POST

• Solution: use Spring’s HiddenHttpMethodFilter only then send _method request parameter in the request

<filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/</url-pattern> <servlet-name>appServlet</servlet-name> </filter-mapping>

Page 39: Multi Client Development with Spring

Not confidential. Tell everyone.

DEMO§Demos:• Spring REST service

• Spring REST client

Page 40: Multi Client Development with Spring

40

Spring Mobile

Page 41: Multi Client Development with Spring

§Best strategy? Develop Native• Fallback to client-optimized web applications

§Spring MVC 3.1 mobile client-specific content negotiation and rendering • for other devices • (there are other devices besides Android??)

Thin, Thick, Web, Mobile and Rich Clients: Mobile

41

Page 42: Multi Client Development with Spring

Not confidential. Tell everyone.

DEMO§Demos:• Mobile clients using client specific rendering

• preferences, device resolver, site switching, device delegating view resolver

Page 43: Multi Client Development with Spring

43

Spring Android

Page 44: Multi Client Development with Spring

Thin, Thick, Web, Mobile and Rich Clients: Mobile

44

§Spring REST is ideal for mobile devices§Spring MVC 3.1 mobile client-specific content negotiation and rendering • for other devices

§Spring Android • RestTemplate

Page 45: Multi Client Development with Spring

OK, so.....

45

Page 46: Multi Client Development with Spring

Thin, Thick, Web, Mobile and Rich Clients: Mobile

46

CustomerServiceClient - client

! private <T> T extractResponse( ResponseEntity<T> response) {! ! if (response != null && response().value() == 200) {! ! ! return response.getBody();! ! }! ! throw new RuntimeException("couldn't extract response.");! }

! @Override! public Customer updateCustomer(long id, String fn, String ln) { ! ! String urlForPath = urlForPath("customer/{customerId}");! !! ! return extractResponse(this.restTemplate.postForEntity( urlForPath, new Customer(id, fn, ln), Customer.class, id));! }

Page 47: Multi Client Development with Spring

Thin, Thick, Web, Mobile and Rich Clients: Mobile

§Demos:• consuming the Spring REST service from Android

Page 48: Multi Client Development with Spring

And The REST of it

• Spring Data REST - exposes (JPA, MongoDB, GemFire, Neo4J, Redis) repositories built on Spring Data repositories • Spring HATEOAS - take your REST-fu to the next level with support for HTTP as the engine of application state

• Spring Social - provides connectivity and authentication support for OAuth

• Spring Security OAuth - provides a way to expose RESTful endpoints through OAuth on the server-side

Page 49: Multi Client Development with Spring

Questions?

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong