introduction to jax-rs @ silicon valley code camp 2010

39
<Insert Picture Here> Introduction to JAX-RS Jitendra Kotamraju Oct 2010

Upload: arun-gupta

Post on 10-May-2015

2.971 views

Category:

Technology


1 download

DESCRIPTION

Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

TRANSCRIPT

Page 1: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

<Insert Picture Here>

Introduction to JAX-RS Jitendra Kotamraju

Oct 2010

Page 2: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

2

What this presentation is about ?

• Not the REST style per se– But, briefly ...

• The basics of JAX-RS

Page 3: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

3

Agenda

• What is JAX-RS ?

• JAX-RS Resources

• Standard Methods (uniform interface)

• JAX-RS Representations

• Lots of demos using JAX-RS API– @Path, HTTP methods, @Produces/@Consumes– XML, JSON, @xxParam, Response, Application,– Sub-resource locators, Injection etc

©2010 Oracle Corporation

Page 4: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

4

Very Short REST Primer:Buy This Book

Page 5: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

5

REST is an Architectural Style

Set of constraints you apply to the architecture of a distributed system

to induce desirable properties

Page 6: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

6

RESTful Web Services

Application of REST architectural style to services that utilize Web standards (URIs, HTTP, HTML,

XML, Atom, RDF etc.)

Page 7: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

7

Java API for RESTful Web Services (JAX-RS)

Standard annotation-driven API that aims to help developers build RESTful Web services in Java

Page 8: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

8

Status of JAX-RS

• JSR 311: JAX-RS 1.1 released Nov 2009• Part of Java EE 6; Not part of the Web profile!• Specifies integration with technologies:

– CDI 1.0– EBJ 3.1– Servlet 3.0

• Jersey implementation shipped with GlassFish 3.x• 7 implementations

– Apache CXF, Apache Wink, eXo, Jersey, RESTEasy, Restlet, Triaxrs

Page 9: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

9

Sample JAX-RS application

GlassFish

hello.war

JAX-RSresource

GET /hello/world HTTP/1.1

HTTP/1.1 200 OKContent-Type: text/xml

<hello world/>

Page 10: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

10

Resources are identified by URIs

↓Clients communicate with resources via requests using a

standard set of methods

↓Requests and responses contain resource representations

in formats identified by media types

↓Responses contain URIs that link to further resources

RESTful Application Cycle

Page 11: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

11

http://example.com/widgets/foo

http://example.com/customers/bar

http://example.com/customers/bar/orders/2

http://example.com/orders/101230/customer

Resources are identified by URIs

Page 12: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

12

JAX-RS Resources

• Resource == Java class– POJO, EJB Stateless, Singleton Session Beans

– No required interfaces

• ID provided by @Path annotation– Value is relative URI, base URI is provided by

deployment context or parent resource– Embedded parameters for non-fixed parts of the URI

– Annotate class or “sub-resource locator” method

Page 13: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

13

JAX-RS Resource URIs

@Path("properties")public class Props { @GET List<Prop> getProperties(...) {...}

}

http://.../context/properties

Page 14: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

14

JAX-RS Sub-resources

• Root resources can declare sub-resources that will match the unmatched part of the URI path

• Root resources implement sub-resource locator methods

Page 15: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

15

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("{name}") Prop getProperty(@PathParam("name")String p){…}}

GET http://.../context/properties/java.home

Page 16: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

16

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

Page 17: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

17

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

GET http://.../context/properties/java.home

Page 18: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

18

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

GET http://.../context/properties/java.home

Page 19: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

19

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

GET http://.../context/properties/java.home

Page 20: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

20

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

GET http://.../context/properties/java.tmp.dir

Page 21: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

21

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

GET http://.../context/properties/java.tmp.dir

Page 22: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

22

JAX-RS Sub-resources

@Path("properties")public class Props { @GET @Path("java.home") Prop getProperty() {…}

@Path("{name}") Object getProp(@PathParam("name")String name){ return new Dyna(name); }}public class Dyna { @GET Prop getProperty() {…}}

GET http://.../context/properties/java.tmp.dir

Page 23: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

23

JAX-RS Sub-resources summary

• Sub-resource classes are processed at runtime• Warning: easy to confuse sub-resource methods with

sub-resource locators

Page 24: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

24

Standard Set of Methods

PurposeMethod

RemoveDELETE

Update or create with a known IDPUT

Update or create without a known IDPOST

Read, possibly cachedGET

Page 25: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

25

JAX-RS Methods

• Annotate resource class methods with standard method– @GET, @PUT, @POST, @DELETE, @HEAD

– @HttpMethod meta-annotation allows extensions, e.g. WebDAV

• JAX-RS routes request to appropriate resource class and method

• Flexible method signatures, annotations on parameters specify mapping from request

• Return value mapped to response

Page 26: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

26

@Path("properties/{name}")public class Props {

@GET Prop get(@PathParam("name") String name) {...}

@PUT Prop set(@PathParam("name") String name, String value) {...}

}

JAX-RS HTTP Methods

Page 27: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

27

Resource Representations

• Representation format identified by media type. E.g.:– XML - application/properties+xml– JSON - application/properties+json– (X)HTML+microformats - application/xhtml+xml

• JAX-RS automates content negotiation– GET /fooAccept: application/properties+json

Page 28: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

28

Resource Representations

• Annotate methods or classes with static capabilities– @Produces, @Consumes

• Use Variant, VariantListBuilder and Request.selectVariant for dynamic capabilities

– Also supports language and encoding

Page 29: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

29

@GET@Produces("application/properties+xml")Prop getXml(@PathParam("name") String name) { ...}

@GET@Produces("text/plain")String getText(@PathParam("name") String name) { ...}

JAX-RS Resource Representations

Page 30: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

30

@POST@Consumes("application/xml")@Produces({"application/xml","application/json"})Customer getCustomer(Source id) { … return customer;}

@XmlRootElementclass Customer { public String first; public String last;}

JAX-RS Resource Representations

Page 31: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

31

Responses Contain Links

HTTP/1.1 201 CreatedDate: Wed, 03 Jun 2009 16:41:58 GMTServer: Apache/1.3.6Location: http://example.com/properties/fooContent-Type: application/order+xmlContent-Length: 184

<property self="http://example.com/properties/foo"> <parent ref="http://example.com/properties/bar"/> <name>Foo</name> <value>1</value></order>

Page 32: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

32

Responses Contain Links

• UriInfo provides information about deployment context, the request URI and the route to the resource

• UriBuilder provides facilities to easily construct URIs for resources

Page 33: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

33

Responses Contain Links

@Context UriInfo i;

SystemProperty p = ...UriBuilder b = i.getBaseUriBuilder();URI u = b.path(SystemProperties.class) .path(p.getName()).build();

List<URI> ancestors = i.getMatchedURIs();URI parent = ancestors.get(1);

Page 34: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

34

Demos

Page 35: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

35

The future of JAX-RS

• Possible features in scope for a JAX-RS 2.0 effort– Client API– Declarative hyperlinking– Model View Controller– Quality of Source– Form validation– Asynchronous/Comet/WebSocket– Improved integration with JSR-330 and @Inject– JAX-RS Modules

Page 36: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

36

Information

• JSR-311: http://jsr311.dev.java.net/• http://jersey.dev.java.net• mailto:[email protected]• http://glassfish.dev.java.net

Page 37: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

37

Questions ?

Page 38: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

©2010 Oracle Corporation

38

ServletJAX-RS application packaged in WAR like a servlet

For JAX-RS aware containersweb.xml can point to Application subclass

For non-JAX-RS aware containersweb.xml points to implementation-specific Servlet; and

an init-param identifies the Application subclass

Resource classes and providers can access Servlet request, context, config and response via injection

Page 39: Introduction to JAX-RS @ SIlicon Valley Code Camp 2010

39

Java EE

Resource class can be an EJB session or singleton bean

Providers can be an EJB stateless session or singleton bean

JAX-RS annotations on local interface or no-interface bean

If JCDI (JSR 299) also supported thenResource classes can be JCDI beans

Providers can be JCDI beans with application scope

Full access to facilities of native component model, e.g. resource injection