SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
Writing Restful Applications
      With RESTEasy

        Andrea Leoncini
          Ugo Landini



                                   Andrea.Leoncini@redhat.com
                          Javaday IV – Roma – 30 gennaio 2010
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
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
...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
...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
                  Javaday IV – Roma – 30 gennaio 2010
...and so what?


   REST != SOAP




REST == WWW
   SOAP != WWW



                           Andrea.Leoncini@redhat.com
                  Javaday IV – Roma – 30 gennaio 2010
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
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
WEB promises, so REST

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



                                    Andrea.Leoncini@redhat.com
                           Javaday IV – Roma – 30 gennaio 2010
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
GRAZIE!
andrea.leoncini@redhat.com




                                  Andrea.Leoncini@redhat.com
                         Javaday IV – Roma – 30 gennaio 2010

Weitere ähnliche Inhalte

Ähnlich wie JavaDayIV - Leoncini Writing Restful Applications With Resteasy

Hong Kong Drupal User Group - 2014 April 12th
Hong Kong Drupal User Group - 2014 April 12thHong Kong Drupal User Group - 2014 April 12th
Hong Kong Drupal User Group - 2014 April 12thWong Hoi Sing Edison
 
Introducing the New DSpace User Interface
Introducing the New DSpace User InterfaceIntroducing the New DSpace User Interface
Introducing the New DSpace User InterfaceTim Donohue
 
Oryoki Open Courseware Management
Oryoki Open Courseware ManagementOryoki Open Courseware Management
Oryoki Open Courseware Managementjsiarto
 
Have You Seen Spring Lately?
Have You Seen Spring Lately?Have You Seen Spring Lately?
Have You Seen Spring Lately?Joshua Long
 
Hong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13thHong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13thWong Hoi Sing Edison
 
Reviewing RESTful Web Apps
Reviewing RESTful Web AppsReviewing RESTful Web Apps
Reviewing RESTful Web AppsTakuto Wada
 
Introduction to the Semantic Web
Introduction to the Semantic WebIntroduction to the Semantic Web
Introduction to the Semantic WebMarin Dimitrov
 
Bringin the web to researchers
Bringin the web to researchersBringin the web to researchers
Bringin the web to researchersPeter Sefton
 
Practical Akka HTTP - introduction
Practical Akka HTTP - introductionPractical Akka HTTP - introduction
Practical Akka HTTP - introductionŁukasz Sowa
 
ERRest: the Basics
ERRest: the BasicsERRest: the Basics
ERRest: the BasicsWO Community
 
Building a blog with an Onion Architecture
Building a blog with an Onion ArchitectureBuilding a blog with an Onion Architecture
Building a blog with an Onion ArchitectureBarry O Sullivan
 
Onion Architecture and the Blog
Onion Architecture and the BlogOnion Architecture and the Blog
Onion Architecture and the Blogbarryosull
 
Presentation Drupal Content Management Framework
Presentation Drupal Content Management FrameworkPresentation Drupal Content Management Framework
Presentation Drupal Content Management FrameworkJoshua Powell
 
JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015
JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015
JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015Codemotion
 
JavaScript Power Tools 2015
JavaScript Power Tools 2015JavaScript Power Tools 2015
JavaScript Power Tools 2015Marcello Teodori
 
WS-* vs. RESTful Services
WS-* vs. RESTful ServicesWS-* vs. RESTful Services
WS-* vs. RESTful ServicesCesare Pautasso
 
Web Applications Development
Web Applications DevelopmentWeb Applications Development
Web Applications Developmentriround
 

Ähnlich wie JavaDayIV - Leoncini Writing Restful Applications With Resteasy (20)

Hong Kong Drupal User Group - 2014 April 12th
Hong Kong Drupal User Group - 2014 April 12thHong Kong Drupal User Group - 2014 April 12th
Hong Kong Drupal User Group - 2014 April 12th
 
Introducing the New DSpace User Interface
Introducing the New DSpace User InterfaceIntroducing the New DSpace User Interface
Introducing the New DSpace User Interface
 
Oryoki Open Courseware Management
Oryoki Open Courseware ManagementOryoki Open Courseware Management
Oryoki Open Courseware Management
 
Have You Seen Spring Lately?
Have You Seen Spring Lately?Have You Seen Spring Lately?
Have You Seen Spring Lately?
 
Oscon 2010
Oscon 2010Oscon 2010
Oscon 2010
 
Hong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13thHong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13th
 
Reviewing RESTful Web Apps
Reviewing RESTful Web AppsReviewing RESTful Web Apps
Reviewing RESTful Web Apps
 
Introduction to the Semantic Web
Introduction to the Semantic WebIntroduction to the Semantic Web
Introduction to the Semantic Web
 
Bringin the web to researchers
Bringin the web to researchersBringin the web to researchers
Bringin the web to researchers
 
Practical Akka HTTP - introduction
Practical Akka HTTP - introductionPractical Akka HTTP - introduction
Practical Akka HTTP - introduction
 
ERRest: the Basics
ERRest: the BasicsERRest: the Basics
ERRest: the Basics
 
Building a blog with an Onion Architecture
Building a blog with an Onion ArchitectureBuilding a blog with an Onion Architecture
Building a blog with an Onion Architecture
 
Onion Architecture and the Blog
Onion Architecture and the BlogOnion Architecture and the Blog
Onion Architecture and the Blog
 
Presentation Drupal Content Management Framework
Presentation Drupal Content Management FrameworkPresentation Drupal Content Management Framework
Presentation Drupal Content Management Framework
 
Spring Roo Internals Javaday IV
Spring Roo Internals Javaday IVSpring Roo Internals Javaday IV
Spring Roo Internals Javaday IV
 
JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015
JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015
JavaScript Power Tools 2015 - Marcello Teodori - Codemotion Rome 2015
 
JavaScript Power Tools 2015
JavaScript Power Tools 2015JavaScript Power Tools 2015
JavaScript Power Tools 2015
 
REST Presentation
REST PresentationREST Presentation
REST Presentation
 
WS-* vs. RESTful Services
WS-* vs. RESTful ServicesWS-* vs. RESTful Services
WS-* vs. RESTful Services
 
Web Applications Development
Web Applications DevelopmentWeb Applications Development
Web Applications Development
 

Mehr von JBug Italy

JBoss Wise: breaking barriers to WS testing
JBoss Wise: breaking barriers to WS testingJBoss Wise: breaking barriers to WS testing
JBoss Wise: breaking barriers to WS testingJBug Italy
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBossJBug Italy
 
Intro jbug milano_26_set2012
Intro jbug milano_26_set2012Intro jbug milano_26_set2012
Intro jbug milano_26_set2012JBug Italy
 
Faster & Greater Messaging System HornetQ zzz
Faster & Greater Messaging System HornetQ zzzFaster & Greater Messaging System HornetQ zzz
Faster & Greater Messaging System HornetQ zzzJBug Italy
 
Infinispan,Lucene,Hibername OGM
Infinispan,Lucene,Hibername OGMInfinispan,Lucene,Hibername OGM
Infinispan,Lucene,Hibername OGMJBug Italy
 
JBoss BRMS - The enterprise platform for business logic
JBoss BRMS - The enterprise platform for business logicJBoss BRMS - The enterprise platform for business logic
JBoss BRMS - The enterprise platform for business logicJBug Italy
 
JBoss AS7 Overview
JBoss AS7 OverviewJBoss AS7 Overview
JBoss AS7 OverviewJBug Italy
 
Intro JBug Milano - January 2012
Intro JBug Milano - January 2012Intro JBug Milano - January 2012
Intro JBug Milano - January 2012JBug Italy
 
JBoss AS7 Webservices
JBoss AS7 WebservicesJBoss AS7 Webservices
JBoss AS7 WebservicesJBug Italy
 
Intro JBug Milano - September 2011
Intro JBug Milano - September 2011Intro JBug Milano - September 2011
Intro JBug Milano - September 2011JBug Italy
 
All the cool stuff of JBoss BRMS
All the cool stuff of JBoss BRMSAll the cool stuff of JBoss BRMS
All the cool stuff of JBoss BRMSJBug Italy
 
Infinispan and Enterprise Data Grid
Infinispan and Enterprise Data GridInfinispan and Enterprise Data Grid
Infinispan and Enterprise Data GridJBug Italy
 
Drools Introduction
Drools IntroductionDrools Introduction
Drools IntroductionJBug Italy
 
September 2010 - Arquillian
September 2010 - ArquillianSeptember 2010 - Arquillian
September 2010 - ArquillianJBug Italy
 
September 2010 - Gatein
September 2010 - GateinSeptember 2010 - Gatein
September 2010 - GateinJBug Italy
 
May 2010 - Infinispan
May 2010 - InfinispanMay 2010 - Infinispan
May 2010 - InfinispanJBug Italy
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasyJBug Italy
 

Mehr von JBug Italy (20)

JBoss Wise: breaking barriers to WS testing
JBoss Wise: breaking barriers to WS testingJBoss Wise: breaking barriers to WS testing
JBoss Wise: breaking barriers to WS testing
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBoss
 
AS7 and CLI
AS7 and CLIAS7 and CLI
AS7 and CLI
 
Intro jbug milano_26_set2012
Intro jbug milano_26_set2012Intro jbug milano_26_set2012
Intro jbug milano_26_set2012
 
Faster & Greater Messaging System HornetQ zzz
Faster & Greater Messaging System HornetQ zzzFaster & Greater Messaging System HornetQ zzz
Faster & Greater Messaging System HornetQ zzz
 
Infinispan,Lucene,Hibername OGM
Infinispan,Lucene,Hibername OGMInfinispan,Lucene,Hibername OGM
Infinispan,Lucene,Hibername OGM
 
AS7
AS7AS7
AS7
 
JBoss BRMS - The enterprise platform for business logic
JBoss BRMS - The enterprise platform for business logicJBoss BRMS - The enterprise platform for business logic
JBoss BRMS - The enterprise platform for business logic
 
JBoss AS7 Overview
JBoss AS7 OverviewJBoss AS7 Overview
JBoss AS7 Overview
 
Intro JBug Milano - January 2012
Intro JBug Milano - January 2012Intro JBug Milano - January 2012
Intro JBug Milano - January 2012
 
JBoss AS7 Webservices
JBoss AS7 WebservicesJBoss AS7 Webservices
JBoss AS7 Webservices
 
JBoss AS7
JBoss AS7JBoss AS7
JBoss AS7
 
Intro JBug Milano - September 2011
Intro JBug Milano - September 2011Intro JBug Milano - September 2011
Intro JBug Milano - September 2011
 
All the cool stuff of JBoss BRMS
All the cool stuff of JBoss BRMSAll the cool stuff of JBoss BRMS
All the cool stuff of JBoss BRMS
 
Infinispan and Enterprise Data Grid
Infinispan and Enterprise Data GridInfinispan and Enterprise Data Grid
Infinispan and Enterprise Data Grid
 
Drools Introduction
Drools IntroductionDrools Introduction
Drools Introduction
 
September 2010 - Arquillian
September 2010 - ArquillianSeptember 2010 - Arquillian
September 2010 - Arquillian
 
September 2010 - Gatein
September 2010 - GateinSeptember 2010 - Gatein
September 2010 - Gatein
 
May 2010 - Infinispan
May 2010 - InfinispanMay 2010 - Infinispan
May 2010 - Infinispan
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 

Kürzlich hochgeladen

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Kürzlich hochgeladen (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

JavaDayIV - Leoncini Writing Restful Applications With Resteasy

  • 1. Writing Restful Applications With RESTEasy Andrea Leoncini Ugo Landini Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  • 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. 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. ...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. ...and so what? REST == WWW Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  • 6. ...and so what? REST != SOAP REST == WWW Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  • 7. ...and so what? REST != SOAP REST == WWW SOAP != WWW Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  • 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. 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. WEB promises, so REST ● Simple ● Fast & Scalable ● Interoperable ● Ubiquitous ● Updateable Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. GRAZIE! andrea.leoncini@redhat.com Andrea.Leoncini@redhat.com Javaday IV – Roma – 30 gennaio 2010