javadayiv - leoncini writing restful applications with resteasy

30
[email protected] Javaday IV – Roma – 30 gennaio 2010 Writing Restful Applications With RESTEasy Andrea Leoncini Ugo Landini

Upload: jbug-italy

Post on 10-May-2015

1.404 views

Category:

Technology


3 download

DESCRIPTION

JavaDayIV http://roma.javaday.it/javaday2010Writing Restful Application with RestEasyAndrea Leoncini - Ugo Landini

TRANSCRIPT

Page 1: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Writing Restful ApplicationsWith RESTEasy

Andrea LeonciniUgo Landini

Page 2: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 3: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 4: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 5: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

...and so what?

REST == WWW

Page 6: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

...and so what?

REST == WWW

REST != SOAP

Page 7: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

...and so what?

REST == WWW

SOAP != WWW

REST != SOAP

Page 8: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 9: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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> …

Page 10: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

WEB promises, so REST

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

Page 11: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 12: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Using @Path

● @Path("/library") associates a URI to your representation

● Both class and methods must have @Path annotation

● URI is the concatenation of class and method

● You don't need to annotate a method you are mapping with the class @Path

@Path("/library")public class Library {

@GET @Path("/books") public String getBooks() {...}

[...] }

Page 13: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Using @Path

● @Path("/library") associates a URI to your representation

● Both class and methods must have @Path annotation

● URI is the concatenation of class and method

● You don't need to annotate a method you are mapping with the class @Path

@Path("/library")public class Library {

@GET @Path("/books") public String getBooks() {...}

[...] }

http://www.therestserver.org/rs/library/books

Page 14: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Using HTTP Methods

● @GET, @POST, @PUT and @DELETE4 methods for a CRUD environment, isn't it?

● As well as SQL

● But don't forget @HEAD

Page 15: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

[...] }

Page 16: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 17: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 18: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

@GET @Path("/used") public String getUsedCars(@QueryParam("min") int min, @QueryParam("max") int max) {...}

Page 19: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

@GET @Path("/used") public String getUsedCars(@QueryParam("min") int min, @QueryParam("max") int max) {...}

@GET @Path("/books") public String getBooks(@HeaderParam("From")String requestFrom) {...}

Page 20: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

@GET @Path("/used") public String getUsedCars(@QueryParam("min") int min, @QueryParam("max") int max) {...}

http://www.therestserver.org/rs/carshop/used?min=30000&max=40000

@GET @Path("/books") public String getBooks(@HeaderParam("From")String requestFrom) {...}

Page 21: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

And not only...

● You can exchange parameters also with:● @CookieParam● @FormParam● @Form (RESTEasy specific)● @Encoded

Page 22: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

And don't forget...

● With both paths and parameters you can useregular expressions

● For every parameter you can specify a primitive, a string or a class with a String constructor or static valueof() method

Page 23: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 24: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Using @Produces

● @Produces is used to map a client request and match it with the client's Accept header.

● The Accept HTTP header is sent by the client, and defines the media types that the client prefers to receive from the server

@Path("/library")@Produces("text/*")public class Library {

@GET @Path("/books") @Produces("text/xml") public String getXMLBooks() { return “<books>An xml list of books</books>”; }

@GET @Path("/books") @Produces("text/plain") public String getBooks() { return “a list of books”; } }

Page 25: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Using @Consumes

● @Consumes is used to specify a set of media types a resource can consume with its methods

● The client makes a request with content-type header parameter

● Then the server invokes the method that matches the media type indicated by the client

@Path("/bookshop")@Consumes("text/*")public class Library {

@POST @Path("/order") @Consumes("application/xml") public void addBookToBasket(Book xmlBook) { ... } }

Page 26: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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.

Page 27: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

Page 28: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

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

@Path("/feeder")public class Feeder {

@GET @Path("/entry") @Produces("application/atom+xml") public Entry getEntry() Entry entry = new Entry(); entry.setTitle("Hi mr president"); Content content = new Content(); content.setJAXBObject(new Customer("Ugo")); ... return entry;}

Page 29: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

Next Steps

● Hands On Lab with JBosstwo hours step by step session for a real use case, including Providers and Cache, so...stay tuned... www.it.redhat.com/events/

● http://www.jboss.org/resteasyDownload, unzip, run, code, debug, deploy, enjoy

● http://jsr311.dev.java.net/JAX-RS

18 marzo a Roma25 marzo a Milano

Page 30: JavaDayIV - Leoncini Writing Restful Applications With Resteasy

[email protected]

Javaday IV – Roma – 30 gennaio 2010

GRAZIE!

[email protected]