An introduction to Struts 2 and RESTful applications

Post on 29-Nov-2014

27.480 views 2 download

description

Presentation given by Don Brown at OSDC Sydney 2008

Transcript of An introduction to Struts 2 and RESTful applications

An introduction to Struts 2 andRESTful applications

Don BrownAtlassian mrdon@twdata.org

What’s wrong with my web application?

Modern web framework

Presentation and business logic separation

Code not tied to HTTP

Problem: Confusing URIs

GEThttp://localhost/spaces/addspacenotification.action?key=AJOBS

GEThttp://localhost/spaces/addspacenotification.action?key=AJOBS

GEThttp://localhost/spaces/addspacenotification.action?key=AJOBS

GEThttp://localhost/spaces/addspacenotification.action?key=AJOBS

Problem: Poor caching

HTTP traffic laws broken:

GET - Could be anything,including deletes andupdates

POST - Also could beanything, even regularpage views

PUT - Not used

DELETE - Not used

Problem: People can’t get at their data

REST is the Way of the Web

Not just for Web Services

http://www.flickr.com/photos/psd/2217422218/

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

Solution: Scalable caching

Follow HTTP traffic laws:

GET - No side-effects, canrepeat

POST - Changes state,cannot repeat

PUT - Changes state, canrepeat

DELETE - Changes state,can repeat

Solution: One API for all

REST in action

Detour: Struts 2

Struts 2 History

Struts 2 plugins

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

Struts 2 REST Plugin

edit()GET/orders/3/edit

editNew()GET/orders/new

destroy()DELETE/orders/3

update()PUT/orders/3

create()POST/orders

show()GET/orders/3

index()GET/orders

Action MethodHTTP MethodURI

A RESTful example: orders

. . . Let’s see some code!

REST in the real world

Not everything is a resource

Account:

• GET . . . Yep

• POST . . . Yep

• PUT . . . Yep

• DELETE . . . Yep

• Renew?

Account Renewal: resource or operation?

Renew as a ResourcePOST:

http://localhost/accounts/3/renewals

Renew as an OperationPOST

http://localhost/accounts/3/renew

REST is training friendly

RESTWS-*

V.S.

UDDI*

WS-Security*

JAXB

AXIS/CXF

SOAP

XML Schema

XML *WSDL

HTTPXML

Clients are and aren’t free

But not

REST: the silver bullet?

The takeaway: apply REST to your app

Questions?

http://struts.apache.org/2.x/docs/rest-plugin.html