Date post: | 06-Apr-2018 |
Category: |
Documents |
Upload: | khureltulga-dashdavaa |
View: | 217 times |
Download: | 0 times |
of 74
8/3/2019 Bill Burke
1/74
1
QuickTime and adecompressor
are needed to see this picture.
RESTful Java with JAX-RS
Bill BurkeEngineering FellowRed Hat
8/3/2019 Bill Burke
2/74
2
QuickTime and adecompressor
are needed to see this picture.
Agenda
Why REST? REST Principles
Writing RESTFul Web Services in Java
JAX-RS
8/3/2019 Bill Burke
3/74
3
QuickTime and adecompressor
are needed to see this picture.
Speakers Qualifications
RESTEasy project lead
Fully certified JAX-RS implementation
JAX-RS JSR member
Also served on EE 5 and EJB 3.0 committees JBoss contributor since 2001
Clustering, EJB, AOP
Published author
Books, articles
8/3/2019 Bill Burke
4/74
4
QuickTime and adecompressor
are needed to see this picture.
What are the goals of SOA?
8/3/2019 Bill Burke
5/74
5
QuickTime and adecompressor
are needed to see this picture.
SOA Goals
Reusable
Interoperable
Evolvable
Versioning
Scalable
Manageable
8/3/2019 Bill Burke
6/74
6
QuickTime and adecompressor
are needed to see this picture.
What system has these properties?
8/3/2019 Bill Burke
7/74
7
QuickTime and adecompressor
are needed to see this picture.
The Web!
8/3/2019 Bill Burke
8/74
8
QuickTime and adecompressor
are needed to see this picture.
What is REST?
REpresentational State Transfer PhD by Roy Fielding
REST answers the questions of
Why is the Web so prevalent and ubiquitous? What makes the Web scale?
How can I apply the architecture of the web tomy applications?
8/3/2019 Bill Burke
9/74
9
QuickTime and adecompressor
are needed to see this picture.
What is REST?
It can mean a simple, lightweight, distributedinterface over HTTP
REST is really a set of architectural principles
Principles that make the Web unique
REST isnt protocol specific
But, usually REST == REST + HTTP
A different way to look at writing Web Services
Many say its the anti-WS-*
Rediscovery of HTTP
8/3/2019 Bill Burke
10/74
10
QuickTime and adecompressor
are needed to see this picture.
Why REST?
HTTP is everywhere Zero-footprint clients
A Lightweight stack
Lightweight interoperability
Evolvability
Link driven systems allow you to redirect easily
Content negotiation allows you to support old and new
formats
8/3/2019 Bill Burke
11/74
11
QuickTime and adecompressor
are needed to see this picture.
REST Architectural Principles
Addressable Resources
Representation Oriented
Constrained interface
Hypermedia and Link Driven
Communicate statelessly
8/3/2019 Bill Burke
12/74
12
QuickTime and adecompressor
are needed to see this picture.
Lets build a RESTful interface!
8/3/2019 Bill Burke
13/74
13
QuickTime and adecompressor
are needed to see this picture.
Building a RESTful Interface
Well build a simple Order Entry System Well apply each architectural principle as we design
Ill describe the implications of each principle
8/3/2019 Bill Burke
14/74
14
QuickTime and adecompressor
are needed to see this picture.
Simple Order Entry System
8/3/2019 Bill Burke
15/74
15
QuickTime and adecompressor
are needed to see this picture.
Addressable Resources
Resources are our endpoints in a RESTful interface The things in our object model become resources
Order
Customer
Product
Each resource should have its own URI
8/3/2019 Bill Burke
16/74
16
QuickTime and adecompressor
are needed to see this picture.
URI Scheme
/orders This URI represents all orders
Well be able to query and create orders from this URI
/orders/{id}
This URI represents one order From this URI, well be able to read, update, and
remove an order
{id} is a matching pattern. A wildcard.
/orders/{id}/lineitems We may or may not want to make lineitems addressable
8/3/2019 Bill Burke
17/74
17
QuickTime and adecompressor
are needed to see this picture.
URI Scheme
Similar URI Scheme for other objects /customers
/customers/{id}
/products
/products/{id}
8/3/2019 Bill Burke
18/74
18
QuickTime and adecompressor
are needed to see this picture.
Implications of Addressability
Use HTTPs identification mechanism WS-* usually has one URI you communicate through
WS-* requires tunnelling additional information aboutobject identity through SOAP contexts.
Allows for linking
Enables the constrained interface (well see later)
URI schemes should be an implementation detail
They should be opaque Published via links (well see later)
8/3/2019 Bill Burke
19/74
19
QuickTime and adecompressor
are needed to see this picture.
Clients and servers exchange representations of a resource through the
uniform interface (which well discuss later)
XML documents
JSON documents
HTTPs Content-Type header identifies what were exchanging
This is a familiar data exchange pattern for Java developers
Swing->RMI->Hibernate
Hibernate objects exchanged to and from client and server
Client modifies state, uses entities as DTOs, server merges changes
No different than how REST operates
No reason a RESTFul webservice and client cant exchange Java objects!
Representation Oriented
8/3/2019 Bill Burke
20/74
20
QuickTime and adecompressor
are needed to see this picture.
Choosing a Representation
Well choose XML Can add others as needed
8/3/2019 Bill Burke
21/74
21
QuickTime and adecompressor
are needed to see this picture.
Customer XML
BillBurke555 Beacon Str.BostonMA02115
8/3/2019 Bill Burke
22/74
22
QuickTime and adecompressor
are needed to see this picture.
Product XML
iPhone$199.99
8/3/2019 Bill Burke
23/74
23
QuickTime and adecompressor
are needed to see this picture.
Order XML
$199.9901/20/2010
BillBurke555 Beacon Str.
BostonMA02115
iPhone$199.99
Q i kTi d
8/3/2019 Bill Burke
24/74
24
QuickTime and adecompressor
are needed to see this picture.
Implications of Representations
Each URI can exchange multiple representations HTTP Content Negotiation allows clients and servers
to choose whats best for them
QuickTime and a
8/3/2019 Bill Burke
25/74
25
QuickTime and adecompressor
are needed to see this picture.
HTTP Negotiation
HTTP allows the client to specify the type of data it is
sending and the type of data it would like to receive
Depending on the environment, the client negotiates
on the data exchanged
An AJAX application may want JSON
A Ruby application my want the XML representation of
a resource
QuickTime and a
8/3/2019 Bill Burke
26/74
26
QuickTime and adecompressor
are needed to see this picture.
HTTP Headers manage this negotiation
ACCEPT: comma delimited list of one or more MIME types the clientwould like to receive as a response
In the following example, the client is requesting a customerrepresentation in either xml or json format
Preferences are supported and defined by HTTP specification
GET /customers/33323Accept: application/xml,application/json
GET /customers/33323Accept: text/html;q=1.0,
application/json;q=0.7;application/xml;q=0.5
HTTP Negotiation
QuickTime and a
8/3/2019 Bill Burke
27/74
27
QuickTime and adecompressor
are needed to see this picture.Implications of Representations
Evolvable integration-friendly services
Common consistent location (URI)
Common consistent set of operations (uniform interface)
Interactions defined, formats slapped on as needed
Built-in service versioning application/customers+xml;version=1
application/customers+xml;version=2
QuickTime and a
C i d U if I f
8/3/2019 Bill Burke
28/74
28
decompressorare needed to see this picture.Constrained, Uniform Interface
The idea is to have a well-defined, fixed, finite set of operations Clients can only use these operations
Each operation has well-defined, explicit behavior
In HTTP land, these methods are GET, POST, PUT, DELETE
How can we build applications with only 4+ methods?
SQL only has 4 operations: INSERT, UPDATE, SELECT,DELETE
JMS has a well-defined, fixed set of operations
Both are pretty powerful and useful APIs with constrainedinterfaces
QuickTime and adC t i d U if I t f
8/3/2019 Bill Burke
29/74
29
decompressorare needed to see this picture.Constrained, Uniform Interface
GET - readonly operation
PUT - used for insert or update of a resource
DELETE - remove a resource
POST - used for creation or as an anything goes operation
GET, PUT, DELETE are idempotent
If you invoke same operation more than once, you should getthe same result every time
POST is not idempotent
Each POST can have a different effect on the resource
QuickTime and adecompressor
8/3/2019 Bill Burke
30/74
30
decompressorare needed to see this picture.
Read a Customer
Request:GET /customer/771 HTTP/1.1
Response:HTTP/1.1 200 OKContent-Type: application/xml
BillBurke555 Beacon Str.BostonMA
02115
QuickTime and adecompressor
8/3/2019 Bill Burke
31/74
31
decompressorare needed to see this picture.
Update a Customer: Change address
Request:PUT /customer/771 HTTP/1.1Content-Type: application/xml
BillBurke
101 Dartmouth Str.BostonMA02115
QuickTime and adecompressor
8/3/2019 Bill Burke
32/74
32
decompressorare needed to see this picture.
Creation
There is a common pattern for creation
POST to a top resource URI
Get back the location (URI) of created resource
Response contains a Location header
QuickTime and adecompressor
8/3/2019 Bill Burke
33/74
33
pare needed to see this picture.
Create a Customer
Request:POST /customers HTTP/1.1Content-Type: application/xml
MonicaBurke
101 Dartmouth Str.BostonMA02115
Response:HTTP/1.1 201 CreatedLocation: http://example.com/customers/2322
QuickTime and adecompressor
d d t thi i t
8/3/2019 Bill Burke
34/74
34
are needed to see this picture.
When 4 methods dont fit
What operations are required on Orders?
Create - POST on /orders
Read - GET on /orders/{id}
Update - PUT on /orders/{id}
Remove - DELETE on /orders/{id}
Cancel?
QuickTime and adecompressor
are needed to see this picture
8/3/2019 Bill Burke
35/74
35
are needed to see this picture.
Operations modeled as state
Can Cancel be modeled as state?
Yes, cancelled is a state of the order
Lets add a element to our representation
The act of cancelling becomes an update of the
representation
QuickTime and adecompressor
are needed to see this picture
C l O d
8/3/2019 Bill Burke
36/74
36
are needed to see this picture.
Cancel an Order
Request:PUT /order/331 HTTP/1.1Content-Type: application/xml
$199.9901/20/2010
true
QuickTime and adecompressor
are needed to see this picture.
O ti t d l d t t
8/3/2019 Bill Burke
37/74
37
are needed to see this picture.
Operations not modeled as state
What if an operation cant be modeled as state?
Example: order purging
Remove all cancelled orders.
In this case, define a new resource:
/orders/purge
POST or PUT to this resource
QuickTime and adecompressor
are needed to see this picture.Implications of Uniform Interface
8/3/2019 Bill Burke
38/74
38
pImplications of Uniform Interface
Simplified
No stubs you have to generate distribute Nothing to install, maintain, upgrade
No vendor you have to pay big bucks to
QuickTime and adecompressor
are needed to see this picture.
8/3/2019 Bill Burke
39/74
39
Identity Operations
Complexity
Data format
QuickTime and adecompressor
are needed to see this picture.Implications of Uniform Interface
8/3/2019 Bill Burke
40/74
40
Implications of Uniform Interface
Interoperability
HTTP a stable protocol
WS-*, again, is a moving target
Ask CXF, Axis, and Metro how difficult Microsoftinteroperability has been
Focus on interoperability between applications ratherfocusing on the interoperability between vendors.
Familiarity
Operations and admins know how to secure, partition,route, and cache HTTP traffic
Leverage existing tools and infrastructure instead ofcreating new ones
QuickTime and adecompressor
are needed to see this picture.
Hypermedia or rather Links
8/3/2019 Bill Burke
41/74
41
Hypermedia, or rather Links
Links drive interactions
When a human uses a browser
No idea what the URI scheme is beforehand
Human just follows links
Google follows links to create search indexes
QuickTime and adecompressor
are needed to see this picture.
Implications of Links
8/3/2019 Bill Burke
42/74
42
Implications of Links
Links allow you to compose data
$199.9901/20/2010
QuickTime and adecompressor
are needed to see this picture.
Implications of Links
8/3/2019 Bill Burke
43/74
43
Implications of Links
Links allow URIs to become opaque
URIs become an implementation detail
RESTful systems usually have very few publishedURIs
URIs schemes can change without breaking clients
QuickTime and adecompressor
are needed to see this picture.
Implications of Links
8/3/2019 Bill Burke
44/74
44
Implications of Links
One URL for Order Entry System
Query base URI, then traverse links to interact
Request:GET /order-entry-system HTTP/1.1
Response:HTTP/1.1 200 OKContent-Type: application/xml
8/3/2019 Bill Burke
45/74
45
Statelessness
A RESTFul web service does not maintain sessions/conversations
on the server Doesnt mean a web service cant have state
REST mandates
That state be converted to resource state
Conversational state be held on client and transferred witheach request
QuickTime and adecompressor
are needed to see this picture.
Statelessness
8/3/2019 Bill Burke
46/74
46
Statelessness
Sessions are not linkable
You cant link a reference to a service that requires asession
A stateless application scales
Sessions require replication
Stateless services only require load balancing
QuickTime and adecompressor
are needed to see this picture.
REST in Conclusion
8/3/2019 Bill Burke
47/74
47
REST in Conclusion
REST answers questions of
Why does the Web scale?
Why is the Web so ubiquitous?
How can I apply the architecture of the Web to my applications?
Promises
Simplicity
Zero-footprint clients.
Interoperability
Platform independence
Change resistance
QuickTime and adecompressor
are needed to see this picture.
8/3/2019 Bill Burke
48/74
48
JAX-RS
RESTFul Web Services in Java
QuickTime and adecompressor
are needed to see this picture.
JAX-RS
8/3/2019 Bill Burke
49/74
49
JAX RS
JCP Specification
Required in Java EE 6
Annotation Framework
Allows you to map HTTP requests to Java methodinvocations
QuickTime and adecompressor
are needed to see this picture.
JAX-RS: GET /orders/3323
8/3/2019 Bill Burke
50/74
50
JAX RS: GET /orders/3323
@Path(/orders)public class OrderResource {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}}
QuickTime and adecompressor
are needed to see this picture.
JAX-RS Annotations
8/3/2019 Bill Burke
51/74
51
@Path
Defines URI mappings and templates
@Produces, @Consumes
What MIME types does the resource produce and
consume @GET, @POST, @DELETE, @PUT, @HEAD
Identifies which HTTP method the Java method isinterested in
QuickTime and adecompressor
are needed to see this picture.JAX-RS Parameter Annotations
8/3/2019 Bill Burke
52/74
52
@PathParam
Allows you to extract URI parameters/named URI template
segments @QueryParam
Access to specific parameter URI query string
@HeaderParam
Access to a specific HTTP Header
@CookieParam
Access to a specific cookie value
QuickTime and adecompressor
are needed to see this picture.
JAX RS GET / d /3323
8/3/2019 Bill Burke
53/74
53
@Path(/orders)public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}
}
Base URI path to resource
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX RS GET / d /3323
8/3/2019 Bill Burke
54/74
54
@Path(/orders)public class OrderService {
@Path(/{order-id})@GET@ProduceMime(application/xml)Order getOrder(@PathParam(order-id) int id) {
}
}
Additional URI pattern
that getOrder() method maps to
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX RS GET / d /3323
8/3/2019 Bill Burke
55/74
55
@Path(/orders)
public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}}
Defines a URI path segment pattern
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX RS: GET /orders/3323
8/3/2019 Bill Burke
56/74
56
@Path(/orders)
public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}}
HTTP method Java getOrder() maps
to
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX RS: GET /orders/3323
8/3/2019 Bill Burke
57/74
57
@Path(/orders)
public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}
}
Whats the CONTENT-TYPE
returned?
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX RS: GET /orders/3323
8/3/2019 Bill Burke
58/74
58
@Path(/orders)
public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}}
Inject value of URI segment into theidJava parameter
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX RS: GET /orders/3323
8/3/2019 Bill Burke
59/74
59
@Path(/orders)
public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)Order getOrder(@PathParam(order-id) int id) {
}}
Automatically convert URI string
segment into an integer
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX-RS: GET /orders/3323
8/3/2019 Bill Burke
60/74
60
@Path(/orders)
public class OrderService {
@Path(/{order-id})@GET@Produces(application/xml)OrdergetOrder(@PathParam(order-id)int id) {
}}
Content handlers can convert from
Java to Data Format
JAX-RS: GET /orders/3323
QuickTime and adecompressor
are needed to see this picture.
JAX-RS: POST /orders
8/3/2019 Bill Burke
61/74
61
@Path(/orders)
public class OrderService {
@POST@Consumes(application/xml)
void submitOrder(Order orderXml) {
}}
What CONTENT-TYPE is this method
expecting from client?
QuickTime and adecompressor
are needed to see this picture.
JAX-RS: POST /orders
8/3/2019 Bill Burke
62/74
62
@Path(/orders)public class OrderService {
@POST@Consumes(application/xml)
void submitOrder(Order orderXml) {
}}
Un-annotated parameters assumed
to be incoming message body.There can be only one!
JAX-RS: POST /orders
QuickTime and adecompressor
are needed to see this picture.
JAX-RS: POST /orders
8/3/2019 Bill Burke
63/74
63
@Path(/orders)public class OrderService {
@POST@Consumes(application/xml)
void submitOrder(Order orderXml) {
}}
Content handlers can convert from
data format into Java object
JAX RS: POST /orders
QuickTime and adecompressor
are needed to see this picture.
More on Content Handlers
8/3/2019 Bill Burke
64/74
64
Media type, annotations, object type are all used to
find a handler
@XmlElementpublic class Order {}
@Path(/orders)public class OrderService {
@POST@Consumes(application/xml)
void submitOrder(Order orderXml) {}
}
QuickTime and adecompressor
are needed to see this picture.
More on Content Handlers
8/3/2019 Bill Burke
65/74
65
JAXB and other simple types required by specification
JSON? Jackson project is a great provider
Atom, multipart, XOP and other formats available
You can write your own custom ones
QuickTime and adecompressor
are needed to see this picture.
Response Object
8/3/2019 Bill Burke
66/74
66
JAX-RS has a Response and ResponseBuilder class
Customize response code
Specify specific response headers
Specify redirect URLs
Work with variants
@GETResponse getOrder() {
ResponseBuilder builder =Response.status(200, order);
builder.type(text/xml).header(custom-header, 33333);
return builder.build();}
QuickTime and adecompressor
are needed to see this picture.
JAX-RS Content Negotiation
8/3/2019 Bill Burke
67/74
67
Matched up and chosen based on request ACCEPT header
Accept: application/json;q=1.0,application/xml;q=0.5
@GET@Produces(application/xml)String getXmlOrder() {}
@GET@Produces(application/json)String getJsonOrder() {}
QuickTime and adecompressor
are needed to see this picture.
ExceptionMappers
8/3/2019 Bill Burke
68/74
68
Map application thrown exceptions to a Response object
Implementations annotated by @Provider
public interface ExceptionMapper
{Response toResponse(E exception);
}
QuickTime and adecompressor
are needed to see this picture.
8/3/2019 Bill Burke
69/74
69
RESTFul Java Clients
QuickTime and adecompressor
are needed to see this picture.
RESTFul Java Clients
j URL
8/3/2019 Bill Burke
70/74
70
java.net.URL
Ugly, buggy, clumsy Apache HTTP Client
Full featured
Verbose
Not JAX-RS aware
Jersey and RESTEasy APIs
Similar in idea to Apache HTTP Client except JAX-RS aware
RESTEasy Client Proxy Framework
Define an interface, re-use JAX-RS annotations for sending requests
QuickTime and adecompressor
are needed to see this picture.RESTEasy Client Proxy Framework
8/3/2019 Bill Burke
71/74
71
@Path(/customers)
public interface CustomerService {
@GET
@Path({id})
@Produces(application/xml)
public Customer getCustomer(
@PathParam(id) String id);
}
CustomerService service =
ProxyFactory(CustomerService.class,
http://example.com);
Customer cust = service.getCustomer(3322);
QuickTime and adecompressor
are needed to see this picture.
RESTEasy
E b dd bl
8/3/2019 Bill Burke
72/74
72
Embeddable
Spring, EJB, Guice, and Seam integration Client Framework
Asynchronous HTTP (COMET)
Client and Server Side Caching
Interceptor model
GZIP encoding support
Data format support
Atom, JAXB, JSON, Multipart, XOP
QuickTime and adecompressor
are needed to see this picture.
JAX-RS Conclusions
8/3/2019 Bill Burke
73/74
73
Mapping HTTP requests using annotations
A la carte HTTP information
Nice content handlers
Nice routing
QuickTime and adecompressor
are needed to see this picture.
References
8/3/2019 Bill Burke
74/74
74
Links
http://jsr311.dev.java.net/
http://jboss.org/resteasy
http://rest-star.org
OReilly Books
RESTFul Java with JAX-RS by me
RESTful Web Services
RESTful Web Services Cookbook
QuickTime and adecompressor
are needed to see this picture.
http://jsr311.dev.java.net/http://jboss.org/resteasyhttp://java.dzone.com/articles/intro-resthttp://java.dzone.com/articles/intro-resthttp://java.dzone.com/articles/intro-resthttp://java.dzone.com/articles/intro-resthttp://jboss.org/resteasyhttp://jsr311.dev.java.net/