JAX RS 2.0 - OTN Bangalore 2013

  • View
    208

  • Download
    0

Embed Size (px)

DESCRIPTION

JAX RS 2.0 - OTN Bangalore 2013

Text of JAX RS 2.0 - OTN Bangalore 2013

  • 1. JAX-RS 2.0A New Standard in RESTful Web Services DevelopmentJagadish RamuApplication Server GroupOracleCopyright 2013, Oracle and/or its affiliates. All rights reserved.

2. The following is intended to outline our general product direction. It is intendedfor information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and shouldnot be relied upon in making purchasing decisions. The development, release,and timing of any features or functionality described for Oracles productsremains at the sole discretion of Oracle.Copyright 2013, Oracle and/or its affiliates. All rights reserved. 3. Agendal REST? What? Why?l JAX-RS 1.1 The Good, The Bad, The Missingl JAX-RS 2.0 New Standard for RESTful Webl Services in Javal Client APIl Asynchronous HTTP processingl Filters & InterceptorsCopyright 2013, Oracle and/or its affiliates. All rights reserved. 4. Why RESTArchitectural style constraints which translate to system design propertiesClient-serverStatelessnessUniform InterfaceCacheabilityLayeringCode on DemandCopyright 2013, Oracle and/or its affiliates. All rights reserved.ScalabilitySimplicityEvolvability, ExtensibilityReliabilityPerformance, EfficiencyVisibility 5. Why RESTl HTTP is the Web communication standardl Support for HTTP-based I/O in all modern platforms & languagesl Hassle-free Interoperabilityl Zero Footprint Mobile device friendlyl Uniform set of HTTP methodsl Easy to understand and consume servicesl Data Representation Independencel Services tailored to the client needsCopyright 2013, Oracle and/or its affiliates. All rights reserved. 6. Agendal REST? What? Why?l JAX-RS 1.1 The Good, The Bad, The Missingl JAX-RS 2.0 New Standard for RESTful Webl Services in Javal Client APIl Asynchronous HTTP processingl Filters & InterceptorsCopyright 2013, Oracle and/or its affiliates. All rights reserved. 7. JAX-RS - Java API for RESTful ServicesStandard annotation-driven APIthat aims to help developersbuild RESTful Web services andtheir clients in JavaCopyright 2013, Oracle and/or its affiliates. All rights reserved.l POJO-Based Resourcel Classesl Programming modelagnosticl HTTP Centricl Maps HTTP requests toJava method invocationsl Entity Format Independencel Container Independencel Included in Java EE 8. JAX-RS Resourcespublic class OrderResource {public List getOrders() { }public Order getOrder(String orderId,Copyright 2013, Oracle and/or its affiliates. All rights reserved.String from) { }public CustomerResource customer() { }} 9. JAX-RS Resources@Path(orders)public class OrderResource {public List getOrders() { }@Path({id})public Order getOrder(String orderId,Copyright 2013, Oracle and/or its affiliates. All rights reserved.String from) { }@Path({id}/customer)public CustomerResource customer() { }} 10. JAX-RS Resources@Path(orders)public class OrderResource {@GETpublic List getOrders() { }@GET@Path({id})public Order getOrder(String orderId,Copyright 2013, Oracle and/or its affiliates. All rights reserved.String from) { }@Path({id}/customer)public CustomerResource customer() { }} 11. JAX-RS Resources@Produces(application/xml, text/plain)@Consumes(application/xml)@Path(orders)public class OrderResource {@GETpublic List getOrders() { }@GET@Path({id})public Order getOrder(String orderId,Copyright 2013, Oracle and/or its affiliates. All rights reserved.String from) { }@Path({id}/customer)public CustomerResource customer() { }} 12. JAX-RS Resources@Produces(application/xml, text/plain)@Consumes(application/xml)@Path(orders)public class OrderResource {@GETpublic List getOrders() { }@GET@Path({id})public Order getOrder(@PathParam(id) String orderId,Copyright 2013, Oracle and/or its affiliates. All rights reserved.@HeaderParam(From) @Default(unknown) String from) { }@Path({id}/customer)public CustomerResource customer() { }} 13. Agendal REST? What? Why?l JAX-RS 1.1 The Good, The Bad, The Missingl JAX-RS 2.0 New Standard for RESTful Webl Services in Javal Client APIl Asynchronous HTTP processingl Filters & InterceptorsCopyright 2013, Oracle and/or its affiliates. All rights reserved. 14. JAX-RS 2.0 Client APIl Motivationl HTTP client libraries too low levell Leveraging existing features of JAX-RS 1.x server APIl Entity providers, etc.l Provided by most major JAX-RS 1.x implementationsl Need for a standardl Fluent APIl Client Builder Client Web Target Request building Responsel URI templatingl Configuration inheritance & specializationCopyright 2013, Oracle and/or its affiliates. All rights reserved. 15. Main Client API Componentsl ClientBuilderl Bootstrapping client runtimel Clientl Main API entry pointl Connection management, configurablel WebTargetl URI, URI template abstraction (glorified URI)l ConfigurableCopyright 2013, Oracle and/or its affiliates. All rights reserved. 16. Accessing a serviceClient client = ClientBuilder.newClient();Copyright 2013, Oracle and/or its affiliates. All rights reserved. 17. Accessing a serviceClient client = ClientBuilder.newClient();WebTarget ordersTarget = client.target(http://example.com/eshop/orders);Copyright 2013, Oracle and/or its affiliates. All rights reserved. 18. Accessing a serviceClient client = ClientBuilder.newClient();WebTarget ordersTarget = client.target(http://example.com/eshop/orders);WebTarget orderTarget = ordersTarget.path({id});Copyright 2013, Oracle and/or its affiliates. All rights reserved. 19. Accessing a serviceClient client = ClientBuilder.newClient();WebTarget ordersTarget = client.target(http://example.com/eshop/orders);WebTarget orderTarget = ordersTarget.path({id});Order order = orderTarget.resolveTemplate(id, 1234)Copyright 2013, Oracle and/or its affiliates. All rights reserved..request(application/xml).get(Order.class); 20. Accessing a serviceOrder newOrder = new Order();Response response = ordersTarget.request(text/plain)Copyright 2013, Oracle and/or its affiliates. All rights reserved..post(Entity.xml(newOrder)); 21. Accessing a serviceOrder newOrder = new Order();Response response = ordersTarget.request(text/plain)Copyright 2013, Oracle and/or its affiliates. All rights reserved..post(Entity.xml(newOrder));if (response.getStatus() == 200) {String orderId = response.readEntity(String.class);Link invoiceLink = response.getLink(invoice);client.target(invoiceLink).request()} 22. Future-based Async Client APIFuture msg= client.target(http://example.com/messenger)Copyright 2013, Oracle and/or its affiliates. All rights reserved..request(application/xml).async().get(Message.class);while (!msg.isDone()) {// do something else}display(msg.get()); 23. Callback-based Async Client APIclient.target(http://example.com/messenger).request(application/xml).async().get(new InvocationCallback() {public void completed(Message msg) {display(msg);}public void failed(Throwable t) { }});// do something elseCopyright 2013, Oracle and/or its affiliates. All rights reserved. 24. Asynchronous JAX-RS Servicesl Blocking or long-running operationsl Off-load HTTP I/O container threadl Detach a request processing thread from responsel Represent a suspended client connection in APIl Suspend until a response is availablel Resume when the response is readyl Leverage Servlet 3.x async support (if available)Copyright 2013, Oracle and/or its affiliates. All rights reserved. 25. Asynchronous Processing: Server-side@Path(messenger)public class MessengerResource {public static Queue messageQueue = ;@GET@Produces(application/xml)public void getMessage(@Suspended AsyncResponse ar) {ar.setTimeoutHandler(new MyTimeoutHandler());ar.setTimeout(15, SECONDS);messageQueue.put(ar);}Copyright 2013, Oracle and/or its affiliates. All rights reserved. 26. Asynchronous Processing: Server-side@POST@Consumes(application/xml)public void postMessage(Message msg) {AsyncResponse ar = messageQueue.take();ar.resume(msg);}}Copyright 2013, Oracle and/or its affiliates. All rights reserved. 27. Asynchronous Processing: Server-sideSuspendedresume()/cancel()Running Responding CompletedTiming outTimeoutHandleronTimeout()setTimeout()Copyright 2013, Oracle and/or its affiliates. All rights reserved.resume()/cancel()CompletionCallbackonComplete() 28. JAX-RS 2.0 Filters & Interceptorsl Advanced use-casesl For framework, library or advanced developersl Customize JAX-RS request/response processingl Logging, Compression, Security, Etc.l Introduced for client and server APIsl Replace existing proprietary supportl Provided by most JAX-RS 1.x implementationsl All using slightly different types or semanticsCopyright 2013, Oracle and/or its affiliates. All rights reserved. 29. JAX-RS 2.0 Filtersl Non-wrapping filter chainl Filters do not invoke next filter in the chaindirectlyl managed by the JAX-RS runtimel Each filter decides to proceed or breakthe chain (abortWith in RequestFilters)Copyright 2013, Oracle and/or its affiliates. All rights reserved.l Request Processingl ContainerRequestFilterl ClientRequestFilterl Response Processingl ContainerResponseFilterl ClientResponseFilter 30. A Logging Filter Examplepublic class RequestLoggingFilter implements ContainerRequestFilter {@Overridepublic void filter(ContainerRequestContext requestContext) {log(requestContext);// non-wrapping => returns without invoking the next filter}private void log(ContainerRequestContext requestContext) { }}Copyright 2013, Oracle and/or its affiliates. All rights reserved. 31. JAX-RS 2.0 Interceptorsl Invoked ONLY if neededl when/if entity processing occursl Performance boostl Wrapping interceptor chainl Each interceptor invokes the next one in thechain via context.proceed()Copyright 2013, Oracle and/or its affiliates. All rights reserved.l MessageBodyReader interceptorl ReaderInterceptor interfacel MessageBodyWriter interceptorl WriterInterceptor interface 32. A GZip Reader Interceptor Examplepublic class GzipInterceptor implements