+ All Categories
Home > Documents > Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for...

Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for...

Date post: 07-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
46
Seam and RESTEasy: You haven’t seen REST yet Dan Allen Author of Seam in Action Senior Software Engineer JBoss, by Red Hat Lincoln Baxter, III Creator of PrettyFaces Co-founder of OcpSoft
Transcript
Page 1: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Seam and RESTEasy:

You haven’t seen REST yet

Dan AllenAuthor of Seam in ActionSenior Software EngineerJBoss, by Red Hat

Lincoln Baxter, IIICreator of PrettyFacesCo-founder of OcpSoft

Page 2: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Agenda

REST principles

JAX-RS and RESTEasy

Seam RESTEasy integration

Demo

Page 3: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Abusing HTTP

Page 4: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

REST to the rescue

Web ServiceWeb

Page 5: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

REST, spelled out

REpresentational State Transfer

Page 6: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Staying grounded with REST

Simple

Lightweight

High performance

Page 7: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Core ingredients of the Web

HTTP application protocol

URI naming standard

XML markup language (and alternatives)

Page 8: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Every web site is a service

Page 9: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

A tale of two webs

Browsable web

Programmable web

HTML +images +CSS, etc

XML(or JSON)

Document

Page 10: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Enabling automation

Page 11: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

RESTful architectural principles

Addressable resources

Uniformed, constrained interface

– GET, PUT, DELETE, POST

Representation-oriented

– multiple formats

Stateless communication

Page 12: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Addressable resources

URI for every resource in system

Resource reachable by unique ID

Provides scoping information

– Query string used to narrow result set

Stepping stones

– Makes it possible to link (linkability)– Allows disparate applications to interact

http://socialize.com/services/mojavelinux/status/311http://socialize.com/services/mojavelinux/status/311

Page 13: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Uniformed, constrained interface

Protocol method == operation

– 4 HTTP methods: GET, PUT, DELETE, POST

An architecture based on 4 methods?

– SQL (SELECT, INSERT, UPDATE, DELETE)– JMS (send, receive)

Page 14: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

HTTP methods

GET - read only, idempotent and safe

PUT - insert or update, idempotent

DELETE - remove services, idempotent

POST - NOT idempotent NOR unsafe

– constraints are relaxed for flexibility

Page 15: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Representation-oriented

Data has representation

– Negotiated between client and server

HTTP was designed for this purpose

– Content-Type header (MIME type)– Accept* headers

Page 16: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Stateless communication

More scalable

– GET lends itself well to caching

Client maintains state

Takes burden off server

Page 17: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Respect the medium

Request

– HTTP method– URI– Request headers– Entity body

Response

– HTTP response code– Response headers– Entity body

Page 18: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

What do you need to REST?

HTTP client (browser, bot, smart phone)

HTTP server that speaks REST

JAX-RS

Page 19: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

JSR-311: JAX-RS

Java APIs for developing Web Services following the REST architectural style

Goals:

– POJO-based (annotations)– HTTP-centric– Format independent (MIME type)– Container independent– Inclusion in Java EE 5+

Page 20: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

http://socialize.com/services/timelinehttp://socialize.com/services/timeline

Our first REST resource

Page 21: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Path("/timeline")public class TimelineService {

@GET public String getUpdates() { return "<updates><update>...</update></updates>"; }

}

@Path("/timeline")public class TimelineService {

@GET public String getUpdates() { return "<updates><update>...</update></updates>"; }

}

Our first JAX-RS resource

Page 22: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

How it works

REST servlet handles GET request

Instance of TimelineService is created

@GET method called

Return value sent as response

Resource instance thrown away

JAX-RS component model is intentionally simple!

Page 23: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

http://socialize.com/services/timeline?count=25http://socialize.com/services/timeline?count=25

Throttling the response

Page 24: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Path("/timeline")public class TimelineService {

@GET public String getUpdates(@QueryParam("count") @DefaultValue("50") int count) { ... }

}

@Path("/timeline")public class TimelineService {

@GET public String getUpdates(@QueryParam("count") @DefaultValue("50") int count) { ... }

}

Accepting a query parameter

http://socialize.com/services/timeline http://socialize.com/services/timeline

http://socialize.com/services/timeline?count=50http://socialize.com/services/timeline?count=50

Page 25: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Parameter types

@QueryParam – Query string

@HeaderParam – HTTP header

@CookieParam – HTTP cookie

@FormParam – Form input

@PathParam – URI path

Page 26: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Stepping into a sub-resource

http://socialize.com/services/timeline/mojavelinuxhttp://socialize.com/services/timeline/mojavelinux

Page 27: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") public String getUpdates(@PathParam("user") String u) { ... }

}

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") public String getUpdates(@PathParam("user") String u) { ... }

}

Mapping a path parameter

Name defined in path expression;segment injected into method

Page 28: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Negotiating a response

So what’s in the response?

– Plain text?– HTML?– XML?– JSON?

The client needs to tell us

– Lists formats, weighted by preference

We have to decide what we support

– Respond with best match

Page 29: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") @Produces("application/xml") public String getUpdatesXml(@PathParam("user") String u) { ... }

}

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") @Produces("application/xml") public String getUpdatesXml(@PathParam("user") String u) { ... }

}

Producing explicitly

Specify which formats aresupported using @Produces

Page 30: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") @Produces("application/json") public String getUpdatesJson(@PathParam("user") String u) { ... }

}

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") @Produces("application/json") public String getUpdatesJson(@PathParam("user") String u) { ... }

}

Producing explicitly

Specify which formats aresupported using @Produces

Page 31: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Simplifying response production

Creating XML and JSON is laborious :(

JAX-RS supports converters

– HTTP entity body readers/writers

Better yet, built-in JAXB provider!

– Object ⇔ XML

RESTEasy provides more built-ins

Page 32: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@XmlRootElement(name = "updates")public class Timeline implements Serializable {

private List<Update> updates = new ArrayList<Update>();

@XmlElement(name = "update") public List<Update> getUpdates() { return updates; }

public void setUpdates(List<Update> updates) { this.updates = updates; }

}

@XmlRootElement(name = "updates")public class Timeline implements Serializable {

private List<Update> updates = new ArrayList<Update>();

@XmlElement(name = "update") public List<Update> getUpdates() { return updates; }

public void setUpdates(List<Update> updates) { this.updates = updates; }

}

A model with XML marshaling hints

Page 33: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@XmlRootElement(name = "update")public class Update implements Serializable {

private Long id; private User user; private Date created; private String text;

// getters and setters

}

@XmlRootElement(name = "update")public class Update implements Serializable {

private Long id; private User user; private Date created; private String text;

// getters and setters

}

A model with XML marshaling hints

Page 34: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") @Produces("application/xml") public Timeline getUpdates(@PathParam("user") String u) { ... }

}

@Path("/timeline")public class TimelineService {

@GET @Path("/{user}") @Produces("application/xml") public Timeline getUpdates(@PathParam("user") String u) { ... }

}

Turning production over to JAXB

Page 35: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

RESTEasy

Fully certified JAX-RS implementation

Portable to any container

Embedded server for testing

Client-side framework for JAX-RS

Response caching and compression

Rich set of providers

– XML, JSON, Atom, YAML, etc.

Asynchronous support

Page 36: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

What does Seam provide?

RESTEasy bootstrap and configuration

Automatic resource/provider discovery

Resource/provider as Seam component

Seam security (role, rule and ACLs)

HTTP authentication

Exception to HTTP response mapping

REST CRUD framework

HTTP session control

Facelets templates

Page 37: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

public abstract class Application { public abstract Set<Class<?>> getClasses(); public abstract Set<Object>getSingletons();}

public SocializeConfig extends Application { ...}

<context-param> <param-name>javax.ws.rs.core.Application</param-name> <param-value>com.socialize.SocializeConfig</param-value></context-param>

public abstract class Application { public abstract Set<Class<?>> getClasses(); public abstract Set<Object>getSingletons();}

public SocializeConfig extends Application { ...}

<context-param> <param-name>javax.ws.rs.core.Application</param-name> <param-value>com.socialize.SocializeConfig</param-value></context-param>

Typical JAX-RS setup

Page 38: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

REST as a Seam resource

http://socialize.com/seam/resources/rest/timeline/mojavelinuxhttp://socialize.com/seam/resources/rest/timeline/mojavelinux

Page 39: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

@Name("timelineService")@Path("/timeline")public class TimelineService { @In TimelineDao timelineDao;

@GET @Path("/{user}") @Produces("application/xml") public Timeline getUpdates(@PathParam("user") String user) { return timelineDao.fetchAll(user); }

}

@Name("timelineService")@Path("/timeline")public class TimelineService { @In TimelineDao timelineDao;

@GET @Path("/{user}") @Produces("application/xml") public Timeline getUpdates(@PathParam("user") String user) { return timelineDao.fetchAll(user); }

}

Seam-infused REST

Page 40: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

<exception class="com.socialize.NoSuchUserException"> <http-error error-code="404"> <message>No such user</message> </http-error></exception>

<exception class="com.socialize.NoSuchUserException"> <http-error error-code="404"> <message>No such user</message> </http-error></exception>

Trapping exceptions

Page 41: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

REST in a few fragments

<framework:entity-home name="userHome" entity-class="com.socialize.model.User"/>

<resteasy:resource-home name="userResourceHome" path="/users" entity-home="#{userHome}" entity-id-class="java.lang.Long" media-types="application/xml application/json" readonly="false"/>

<resteasy:resource-query name="userResourceQuery" path="/users" entity-class="com.socialize.model.User" media-types="application/xml application/json"/>

<framework:entity-home name="userHome" entity-class="com.socialize.model.User"/>

<resteasy:resource-home name="userResourceHome" path="/users" entity-home="#{userHome}" entity-id-class="java.lang.Long" media-types="application/xml application/json" readonly="false"/>

<resteasy:resource-query name="userResourceQuery" path="/users" entity-class="com.socialize.model.User" media-types="application/xml application/json"/>

Page 42: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Socialize demo

Page 43: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Summary and call to action

REST is why the web works

– Addressable resources– Uniformed-interface– Representation-oriented– Stateless communication

Expose your data in a standard way

– Don’t ball it up inside web pages!

Embrace simplicity → Seam+RESTEasy

Page 44: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Must reads

RESTful Web ServicesRichardson & RubyO’Reilly

RESTful Java with JAX-RSBill BurkeO’Reilly

Page 45: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

GET /questions? HTTP/1.1

Page 46: Seam and RESTEasyFully certified JAX-RS implementation Portable to any container Embedded server for testing Client-side framework for JAX-RS Response caching and compression Rich

Presentation resources

JSR-311– http://jcp.org/en/jsr/detail?id=311

RESTEasy– http://jboss.org/resteasy

Seam RESTEasy integration– Web Services chapter of Seam reference guide

Bill Burke’s REST series on DZone– http://java.dzone.com/articles/putting-java-rest

Code samples– http://seaminaction.googlecode.com/svn/demos


Recommended