C DI, S eam & R E S TE as y You haven’t seen RES T yet! Dan Allen S enior S oftware Engineer JBoss, by Red Hat
Ag enda R E S T princ iples JAX -R S S eam R E S TE as y integ ration JAX -R S enhanc ed w ith C DI Demo
Abus ing H TTP w ith S OAP
R E S T to the res c ue Web S ervic e Web
S taying g rounded w ith R E S T S imple Lig htw eig ht Hig h performanc e
S imple ing redients of the Web HTTP applic ation protoc ol U R I naming s tandard X M L markup lang uag e (and alternatives)
A tale of tw o w ebs Document HTML + B row s able w eb images + CSS, etc XML Prog rammable w eb (or JSON)
E very w eb s ite is a s ervic e
E nabling automation
R E S T, s pelled out R E pres entational S tate Trans fer
R E S Tful arc hitec tural princ iples Addres s able res ourc es U niformed, c ons trained interfac e R epres entation-oriented S tateles s c ommunic ation
Addres s able res ourc es U R I for every res ourc e in s ys tem R es ourc e reac hable by unique ID http://socialize.com/rest/updates/311 http://socialize.com/rest/updates/311 Provides s c oping information – Query string used to narrow result set S tepping s tones – Makes it possible to link (linkability) – Allows disparate applications to interact
U niformed, c ons trained interfac e Protoc ol method == operation – 4 HTTP methods: GET, PUT, DELETE, POS T An arc hitec ture bas ed on 4 methods ? – SQL (SELECT, INSERT, UPDATE, DELETE) – JMS (send, receive)
HTTP methods G E T - read only, idempotent and s afe PU T - ins ert or update, idempotent DE LE TE - remove s ervic es , idempotent POS T - NOT idempotent NOR uns afe – constraints are relaxed for flexibility
R epres entation-oriented Data has a repres entation – Negotiated between client and server HTTP w as des ig ned for this purpos e C lient – “I would prefer...” – Accept (MIME type) – Accept-Language – Accept-Encoding S erver – “Here's what I'll give you...” – Content-type header (MIME type)
S tateles s c ommunic ation M ore s c alable – GET lends itself well to caching C lient maintains s tate Takes burden off s erver
R es pec t the medium R es pons e R eques t – HTTP method – HTTP response code – Response headers – URI – Request headers – Entity body – Entity body
What do you need to R E S T? HTTP c lient (browser, bot, smart phone) HTTP s erver that s peaks R E S T JAX -R S
JS R -311: JAX -R S Java APIs for developing Web S ervic es follow ing the R E S T arc hitec tural s tyle S erved via an HTTP s ervlet G oals : – POJO-based (annotations) – HTTP-centric – Format independent (MIME type) – Container independent – Inclusion in Java EE 5+
Our firs t R E S T res ourc e http://socialize.com/rest/timeline http://socialize.com/rest/timeline
Our firs t JAX -R S res ourc e @Path("/timeline") @Path("/timeline") public class TimelineService { public class TimelineService { @GET @GET public String getUpdates() { public String getUpdates() { return "<updates><update>...</update></updates>"; return "<updates><update>...</update></updates>"; } } } }
How it w orks R E S T S ervlet handles G E T reques t Ins tanc e of TimelineS ervic e is c reated @G E T method c alled R eturn value s ent as res pons e R es ourc e ins tanc e throw n aw ay JAX-RS component model is intentionally simple!
Throttling the res pons e http://socialize.com/rest/timeline?count=25 http://socialize.com/rest/timeline?count=25
Ac c epting a query parameter @Path("/timeline") @Path("/timeline") public class TimelineService { public class TimelineService { @GET @GET public String getUpdates(@QueryParam("count") public String getUpdates(@QueryParam("count") @DefaultValue("50") int count) { @DefaultValue("50") int count) { ... ... } } } } ↴ http://socialize.com/rest/timeline http://socialize.com/rest/timeline http://socialize.com/rest/timeline?count=50 http://socialize.com/rest/timeline?count=50
Parameter types @QueryParam – Query s tring @HeaderParam – HTTP header @C ookieParam – HTTP c ookie @FormParam – Form input @PathParam – U R I path @M atrixParam – M atrix U R I s eg ment
S tepping into a s ub-res ourc e http://socialize.com/rest/timeline/mojavelinux http://socialize.com/rest/timeline/mojavelinux
M apping a path parameter Name defined in path expression; @Path("/timeline") @Path("/timeline") segment injected into method public class TimelineService { public class TimelineService { @GET @GET @Path("/{username}") @Path("/{username}") public String getUpdates(@PathParam("username") String u) { public String getUpdates(@PathParam("username") String u) { ... ... } } } }
M apping different patterns @Path("/timeline") @Path("/timeline") public class TimelineService { public class TimelineService { @GET @GET @Path("/{id:[0-9]+}") @Path("/{id:[0-9]+}") public String getUpdatesById(@PathParam("id") long id) { public String getUpdatesById(@PathParam("id") long id) { ... ... Fallback if patterns don't match } } @GET @GET @Path("/{username}") @Path("/{username}") public String getUpdatesByUsername( public String getUpdatesByUsername( @PathParam("username") String u) { @PathParam("username") String u) { ... ... } } } }
N eg otiating a res pons e Whic h repres entation? – Plain text? – HTML? – XML? – JSON? The c lient needs to tell us – Accept formats, weighted by preference We have to dec ide w hat w e s upport – Respond with best match
Produc ing explic itly Specify which formats are @Path("/timeline") @Path("/timeline") supported using @Produces public class TimelineService { public class TimelineService { @GET @GET @Path("/{u}") @Path("/{u}") @Produces(" applic ation/xml ") @Produces(" applic ation/xml ") public String getUpdates As X ml (@PathParam("u") String u) { public String getUpdates As X ml (@PathParam("u") String u) { ... ... } } ... ... } }
Produc ing explic itly Specify which formats are @Path("/timeline") @Path("/timeline") supported using @Produces public class TimelineService { public class TimelineService { @GET @GET @Path("/{u}") @Path("/{u}") @Produces(" applic ation/js on ") @Produces(" applic ation/js on ") public String getUpdates As Js on (@PathParam("u") String u) { public String getUpdates As Js on (@PathParam("u") String u) { ... ... } } ... ... } }
S implifying res pons e produc tion C reating X M L and JS ON is laborious :( JAX -R S s upports c onverters – HTTP entity body readers/writers Provides built-in JAX B provider! – Object ⇔ XML – Object ⇔ JSON
A model w ith X M L hints @XmlRootElement(name = "updates") @XmlRootElement(name = "updates") public class Timeline implements Serializable { public class Timeline implements Serializable { private List<Update> updates = new ArrayList<Update>(); private List<Update> updates = new ArrayList<Update>(); // constructors // constructors @XmlElement(name = "update") @XmlElement(name = "update") public List<Update> getUpdates() { public List<Update> getUpdates() { return updates; return updates; } } public void setUpdates(List<Update> updates) { public void setUpdates(List<Update> updates) { this.updates = updates; this.updates = updates; } } } }
A related model w ith X M L hints @Entity @Entity @XmlRootElement @XmlRootElement public class Update implements Serializable { public class Update implements Serializable { private Long id; private Long id; private User user; private User user; private Date created; private Date created; private String text; private String text; // getters and setters // getters and setters } }
Turning produc tion over to JAX B @Path("/timeline") @Path("/timeline") public class TimelineService { public class TimelineService { @GET @GET @Path("/{u}") @Path("/{u}") @Produces("application/xml") @Produces("application/xml") public Timeline getUpdates(@PathParam("u") String u) { public Timeline getUpdates(@PathParam("u") String u) { List<Update> updates = ...; List<Update> updates = ...; return new Timeline(updates); return new Timeline(updates); } } } }
R E S TE as y Fully c ertified JAX -R S implementation Portable to any c ontainer E mbedded s erver for tes ting C lient-s ide framew ork for JAX -R S R es pons e c ac hing and c ompres s ion R ic h s et of providers – XML, JSON, RSS, Atom, YAML, etc. As ync hronous s upport
Recommend
More recommend