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