Writing Restful Applications
      With RESTEasy

        Andrea Leoncini
          Ugo Landini



                       ...
Who's Andrea

●   Serves as presales & GPS @ Red Hat
●   Partecipates:
    ●   JBoss User Group Roma (member & sponsor)
  ...
B42K

●   HTTP/1.1
    ●   8 methods, 4 of them (GET,
        POST, PUT, DELETE) sufficient to
        create a Constraine...
...Roy Fielding works on REST

●   Roy is one of the contributors of the HTTP
    specification
●   He thinks to REST as a...
...and so what?




REST == WWW


                  Andrea.Leoncini@redhat.com
         Javaday IV – Roma – 30 gennaio 2010
...and so what?


   REST != SOAP




REST == WWW


                           Andrea.Leoncini@redhat.com
                ...
...and so what?


   REST != SOAP




REST == WWW
   SOAP != WWW



                           Andrea.Leoncini@redhat.com
...
What's REST

●   REpresentational State Transfer
●   Is a set of architectural principles or an architectural style
●   is...
Addressability also means Linkability

 ●   Resource representations have a standardized way of
     referencing other res...
WEB promises, so REST

●   Simple
●   Fast & Scalable
●   Interoperable
●   Ubiquitous
●   Updateable



                 ...
Let's start working

●   Deploy RESTEasy as web application.
●   Annotate your classes which have representations you
    ...
Using @Path

●   @Path("/library") associates a
                                     @Path("/library")
    URI to your rep...
Using @Path

●   @Path("/library") associates a
                                                 @Path("/library")
    URI...
Using HTTP Methods

●   @GET, @POST, @PUT and @DELETE
    4 methods for a CRUD environment, isn't it?
●   As well as SQL

...
Ok, what about parameters?

●   @PathParam enables you to map variables from URL to
    your method
                      ...
Ok, what about parameters?

●   @PathParam enables you to map variables from URL to
    your method
                      ...
Do we have other ways?

●   Use @QueryParam to specify parameters on
    QueryString of the URL



●   Or @HeaderParam to ...
Do we have other ways?

●   Use @QueryParam to specify parameters on
         @GET
    QueryString of the URL
         @Pa...
Do we have other ways?

●   Use @QueryParam to specify parameters on
         @GET
    QueryString of the URL
         @Pa...
Do we have other ways?

●   Use @QueryParam to specify parameters on
          @GET
    QueryString of the URL
          @...
And not only...

●   You can exchange parameters also with:
    ●   @CookieParam
    ●   @FormParam
    ●   @Form (RESTEas...
And don't forget...

●   With both paths and parameters you can use
    regular expressions
●   For every parameter you ca...
HTTP Content Negotiation

●   Which type of objects can my clients obtain or my server
    receive?
●   The HTTP protocol ...
Using @Produces
                                          @Path("/library")
                                          @Pro...
Using @Consumes
●   @Consumes is used to specify a
    set of media types a resource can   @Path("/bookshop")

    consume...
Using Cache Annotations
●   @Cache and @NoCache enables you to set the Cache-Control
    headers on a successful GET reque...
ATOM support

●   RESTEasy supports ATOM
●   What is ATOM?
    ●   XML doc for listing related information, AKA feeds.
   ...
ATOM support
                                            @Path("/feeder")
●   RESTEasy supports ATOM                  publ...
Next Steps

●   Hands On Lab with JBoss
    two hours step by step session for a real use case, including
    Providers an...
GRAZIE!
andrea.leoncini@redhat.com




                                  Andrea.Leoncini@redhat.com
                      ...
Upcoming SlideShare
Loading in...5
×

JavaDayIV - Leoncini Writing Restful Applications With Resteasy

1,418

Published on

JavaDayIV
http://roma.javaday.it/javaday2010
Writing Restful Application with RestEasy
Andrea Leoncini - Ugo Landini

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,418
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

JavaDayIV - Leoncini Writing Restful Applications With Resteasy

  1. 1. Writing Restful Applications With RESTEasy Andrea Leoncini Ugo Landini Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  2. 2. Who's Andrea ● Serves as presales & GPS @ Red Hat ● Partecipates: ● JBoss User Group Roma (member & sponsor) www.jbugroma.org ● Java User Group Roma (member & proudly cofounder) www.jugroma.it ● JBoss.org Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  3. 3. B42K ● HTTP/1.1 ● 8 methods, 4 of them (GET, POST, PUT, DELETE) sufficient to create a Constrained Interface (as well as SQL) ● Ubiquitous, stable ● At the same time... Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  4. 4. ...Roy Fielding works on REST ● Roy is one of the contributors of the HTTP specification ● He thinks to REST as a key architectural principle of the World Wide Web. ● In other words everything we need to write distributed services is available in the protocol himself Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  5. 5. ...and so what? REST == WWW Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  6. 6. ...and so what? REST != SOAP REST == WWW Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  7. 7. ...and so what? REST != SOAP REST == WWW SOAP != WWW Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  8. 8. What's REST ● REpresentational State Transfer ● Is a set of architectural principles or an architectural style ● isn’t protocol specific but usually REST == REST + HTTP ● It's a different way for writing Web Services ● Addressability is the real keyword Everything should have a URI Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  9. 9. Addressability also means Linkability ● Resource representations have a standardized way of referencing other resource representations ● Representations have a standardized way to compose themselves: <book id=“123”> <author>http://rs.bookshop.com/authors/345</author> <title>Java Cookbook</title> <abstract> … Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  10. 10. WEB promises, so REST ● Simple ● Fast & Scalable ● Interoperable ● Ubiquitous ● Updateable Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  11. 11. Let's start working ● Deploy RESTEasy as web application. ● Annotate your classes which have representations you want to expose. ● JAX-RS annotation framework lead by Sun Microsystems – Marc Hadley ● Add annotated classes to the container, RESTEasy has a ServletContextListener to initialize the registry of your services (you can programmatically interact with it). Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  12. 12. Using @Path ● @Path("/library") associates a @Path("/library") URI to your representation public class Library { ● Both class and methods must @GET @Path("/books") have @Path annotation public String getBooks() {...} ● URI is the concatenation of [...] class and method } ● You don't need to annotate a method you are mapping with the class @Path Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  13. 13. Using @Path ● @Path("/library") associates a @Path("/library") URI to your representation public class Library { ● Both class and methods must @GET @Path("/books") have @Path annotation public String getBooks() {...} ● http://www.therestserver.org/rs/library/books URI is the concatenation of [...] class and method } ● You don't need to annotate a method you are mapping with the class @Path Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  14. 14. Using HTTP Methods ● @GET, @POST, @PUT and @DELETE 4 methods for a CRUD environment, isn't it? ● As well as SQL ● But don't forget @HEAD Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  15. 15. Ok, what about parameters? ● @PathParam enables you to map variables from URL to your method @Path("/library") public class Library { @GET @Path("/book/{isbn}") public String getBook(@PathParam("isbn") ISBN id) {...} [...] } Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  16. 16. Ok, what about parameters? ● @PathParam enables you to map variables from URL to your method @Path("/library") public class Library { @GET @Path("/book/{isbn}") public String getBook(@PathParam("isbn") ISBN id) {...} [...] http://www.therestserver.org/rs/library/book/357 } Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  17. 17. Do we have other ways? ● Use @QueryParam to specify parameters on QueryString of the URL ● Or @HeaderParam to access the HTTP header Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  18. 18. Do we have other ways? ● Use @QueryParam to specify parameters on @GET QueryString of the URL @Path("/used") public String getUsedCars(@QueryParam("min") int min, @QueryParam("max") int max) {...} ● Or @HeaderParam to access the HTTP header Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  19. 19. Do we have other ways? ● Use @QueryParam to specify parameters on @GET QueryString of the URL @Path("/used") public String getUsedCars(@QueryParam("min") int min, @QueryParam("max") int max) {...} ● Or @HeaderParam to access the HTTP header @GET @Path("/books") public String getBooks(@HeaderParam("From")String requestFrom) {...} Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  20. 20. Do we have other ways? ● Use @QueryParam to specify parameters on @GET QueryString of the URL @Path("/used") public String getUsedCars(@QueryParam("min") int min, @QueryParam("max") int max) {...} http://www.therestserver.org/rs/carshop/used?min=30000&max=40000 ● Or @HeaderParam to access the HTTP header @GET @Path("/books") public String getBooks(@HeaderParam("From")String requestFrom) {...} Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  21. 21. And not only... ● You can exchange parameters also with: ● @CookieParam ● @FormParam ● @Form (RESTEasy specific) ● @Encoded Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  22. 22. And don't forget... ● With both paths and parameters you can use regular expressions ● For every parameter you can specify a primitive, a string or a class with a String constructor or static valueof() method Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  23. 23. HTTP Content Negotiation ● Which type of objects can my clients obtain or my server receive? ● The HTTP protocol has built-in content negotiation headers that allow the client and server to specify the type of content that they transfer, and the type of content they prefer to receive. ● On the server side we can specify content preferences using @Produces and @Consumes annotations Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  24. 24. Using @Produces @Path("/library") @Produces("text/*") ● @Produces is used to map a public class Library { client request and match it with @GET the client's Accept header. @Path("/books") @Produces("text/xml") ● The Accept HTTP header is sent public String getXMLBooks() { return “<books>An xml list of books</books>”; by the client, and defines the } media types that the client prefers @GET to receive from the server @Path("/books") @Produces("text/plain") public String getBooks() { return “a list of books”; } } Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  25. 25. Using @Consumes ● @Consumes is used to specify a set of media types a resource can @Path("/bookshop") consume with its methods @Consumes("text/*") public class Library { ● The client makes a request with @POST content-type header parameter @Path("/order") @Consumes("application/xml") public void addBookToBasket(Book xmlBook) { ● Then the server invokes the ... method that matches the media } type indicated by the client } Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  26. 26. Using Cache Annotations ● @Cache and @NoCache enables you to set the Cache-Control headers on a successful GET request, that is any any request that returns a 200 OK response. ● It can be used only on GET annotated methods. ● @Cache annotation builds the Cache-Control header, @NoCache actually sets Cache-Control: nocache. ● If (and only if) you have specified a Cache annotation on your method server side implementation of RESTEasy checks to see if the URL has been already served. If it does it uses the already marshalled response without invoking the method. Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  27. 27. ATOM support ● RESTEasy supports ATOM ● What is ATOM? ● XML doc for listing related information, AKA feeds. It is primarily used to syndicate the web ● ATOM is very likely the RSS feed of the next generation ● Used with REST can be considered as a simplyfied envelope Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  28. 28. ATOM support @Path("/feeder") ● RESTEasy supports ATOM public class Feeder { ● What is ATOM? @GET @Path("/entry") @Produces("application/atom+xml") ● XML doc for listing related information, AKA feeds. public Entry getEntry() Entry entry = new Entry(); It is primarily used to syndicate the web mr president"); entry.setTitle("Hi Content content = new Content(); ● ATOM is very likely the RSS feed of the next generation content.setJAXBObject(new Customer("Ugo")); ... Used with REST can be considered as a simplyfied return entry; ● } envelope Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  29. 29. Next Steps ● Hands On Lab with JBoss two hours step by step session for a real use case, including Providers and Cache, so... 18 marzo a Roma stay tuned... www.it.redhat.com/events/ 25 marzo a Milano ● http://www.jboss.org/resteasy Download, unzip, run, code, debug, deploy, enjoy ● http://jsr311.dev.java.net/ JAX-RS Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  30. 30. GRAZIE! andrea.leoncini@redhat.com Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×