Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

An introduction to Struts 2 and RESTful applications

29,022 views

Published on

Presentation given by Don Brown at OSDC Sydney 2008

Published in: Technology, Education

An introduction to Struts 2 and RESTful applications

  1. 1. An introduction to Struts 2 and RESTful applications Don Brown Atlassian mrdon@twdata.org
  2. 2. What’s wrong with my web application?  Modern web framework  Presentation and business logic separation  Code not tied to HTTP
  3. 3. Problem: Confusing URIs GET http://localhost/spaces/addspacenotification.action? key=AJOBS GET http://localhost/spaces/addspacenotification.action? key=AJOBS GET http://localhost/spaces/addspacenotification.action? key=AJOBS GET http://localhost/spaces/addspacenotification.action? key=AJOBS
  4. 4. Problem: Poor caching HTTP traffic laws broken:  GET - Could be anything, including deletes and updates  POST - Also could be anything, even regular page views  PUT - Not used  DELETE - Not used
  5. 5. Problem: People can’t get at their data
  6. 6. REST is the Way of the Web
  7. 7. Not just for Web Services http://www.flickr.com/photos/psd/2217422218/
  8. 8. Solution: Humane URIs GET http://localhost/spaces/addspacenotification.action? key=AJOBS POST http://localhost/spaces/AJOBS/notifications GET http://localhost/spaces/AJOBS/notifications/3 PUT http://localhost/spaces/AJOBS/notifications/3 DELETE http://localhost/spaces/AJOBS/notifications/3
  9. 9. Solution: Scalable caching Follow HTTP traffic laws:  GET - No side-effects, can repeat  POST - Changes state, cannot repeat  PUT - Changes state, can repeat  DELETE - Changes state, can repeat
  10. 10. Solution: One API for all
  11. 11. REST in action
  12. 12. Detour: Struts 2
  13. 13. Struts 2 History
  14. 14. Struts 2 plugins
  15. 15. Struts 2 Codebehind Plugin http://example.com/myapp/public/profile.xhtml Calls: com.example.myapp.actions.public.ProfileAction Forwards to: /WEB-INF/jsp/public/profile.jsp
  16. 16. Struts 2 REST Plugin URI HTTP Method Action Method /orders GET index() /orders/3 GET show() /orders POST create() /orders/3 PUT update() /orders/3 DELETE destroy() /orders/new GET editNew() /orders/3/edit GET edit()
  17. 17. A RESTful example: orders . . . Let’s see some code!
  18. 18. REST in the real world
  19. 19. Not everything is a resource Account: • GET . . . Yep • POST . . . Yep • PUT . . . Yep • DELETE . . . Yep • Renew?
  20. 20. Account Renewal: resource or operation? Renew as a Resource POST: http://localhost/accounts/3/renewals Renew as an Operation POST http://localhost/accounts/3/renew
  21. 21. REST is training friendly
  22. 22. WS-* REST XML HTTP WSDL XML * XML Schema SOAP AXIS/CXF V.S. JAXB WS-Security* UDDI*
  23. 23. Clients are and aren’t free But not
  24. 24. REST: the silver bullet?
  25. 25. The takeaway: apply REST to your app
  26. 26. Questions? http://struts.apache.org/2.x/docs/rest-plugin.html

×