c di s eam r e s te as y you haven t seen res t yet
play

C DI, S eam & R E S TE as y You havent seen RES T yet! Dan - PowerPoint PPT Presentation

C DI, S eam & R E S TE as y You havent 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


  1. 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

  2. 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

  3. Abus ing H TTP w ith S OAP

  4. R E S T to the res c ue Web S ervic e Web

  5. S taying g rounded w ith R E S T S imple Lig htw eig ht Hig h performanc e

  6. 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)

  7. 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)

  8. E very w eb s ite is a s ervic e

  9. E nabling automation

  10. R E S T, s pelled out R E pres entational S tate Trans fer

  11. 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

  12. 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

  13. 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)

  14. 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

  15. 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)

  16. 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

  17. 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

  18. 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

  19. 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+

  20. Our firs t R E S T res ourc e http://socialize.com/rest/timeline http://socialize.com/rest/timeline

  21. 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>"; } } } }

  22. 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!

  23. Throttling the res pons e http://socialize.com/rest/timeline?count=25 http://socialize.com/rest/timeline?count=25

  24. 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

  25. 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

  26. S tepping into a s ub-res ourc e http://socialize.com/rest/timeline/mojavelinux http://socialize.com/rest/timeline/mojavelinux

  27. 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) { ... ... } } } }

  28. 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) { ... ... } } } }

  29. 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

  30. 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) { ... ... } } ... ... } }

  31. 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) { ... ... } } ... ... } }

  32. 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

  33. 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; } } } }

  34. 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 } }

  35. 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); } } } }

  36. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend