JAX-RS.Next(): JAX-RS 2 and Beyond

33,301 views

Published on

JAX-RS 2.0 is a new standard Java API for RESTful Web services and a major leap forward in the features and use cases covered by the API. The purpose of this technical session is to elaborate on all the new features being introduced as part of this new major API revision. This session explores the new client API, concepts behind filters and interceptors API, and asynchronous processing support. Other new features it covers include data validation support and improved support for hypermedia and server-side content negotiation. The last part of the session also briefly outlines future plans and focus areas.

Published in: Technology, Business
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
33,301
On SlideShare
0
From Embeds
0
Number of Embeds
14,410
Actions
Shares
0
Downloads
242
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide
  • Use @Path to define web resources
    Use other annotations to map requests to methods
    Use method return value as response
    MBR and MBW help us behind the scenes
  • MBR and MBW can be user defined too
  • MBR and MBW can be user defined too
  • MBR and MBW can be user defined too
  • Suspend/Resume example
  • Async client API
  • Filters
  • Handlers
  • Bindings
  • Bindings example
  • Bindings example
  • Structural vs. Transitional links
  • Tansitional liks API example
  • Tansitional liks API example
  • Server-side content negotiation
  • Server-side content negotiation
  • JAX-RS.Next(): JAX-RS 2 and Beyond

    1. 1. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public1 JAX-RS.Next(): JAX-RS 2 and Beyond Reza Rahman Java EE/GlassFish Evangelist Reza.Rahman@Oracle.com @reza_rahman
    2. 2. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public2 JAX-RS Example @Path("/atm/{cardId}") public class AtmService { @GET @Path("/balance") @Produces("text/plain") public String balance( @PathParam("cardId") String card, @QueryParam("pin") String pin) { return Double.toString(getBalance(card, pin)); } ...
    3. 3. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public3 JAX-RS Example ... @POST @Path("/withdrawal") @Consumes("text/plain") @Produces("application/json") public Money withdraw( @PathParam("card") String card, @QueryParam("pin") String pin, String amount) { return getMoney(card, pin, amount); } }
    4. 4. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public4 JAX-RS Annotations
    5. 5. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public5 JAX-RS Annotations (Continued)
    6. 6. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public6Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public JAX-RS 2  Client API  Modularized Configuration  Asynchronous Processing  Filters/Interceptors  Hypermedia Support  Server-side Content Negotiation
    7. 7. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public7 Client API
    8. 8. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public8 Client API // Get instance of Client Client client = ClientBuilder.newClient(); // Get account balance String bal = client.target("http://.../atm/{cardId}/balance") .resolveTemplate("cardId", "111122223333") .queryParam("pin", "9876") .request("text/plain").get(String.class);
    9. 9. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public9 Client API // Withdraw some money Money money = client.target("http://.../atm/{cardId}/withdrawal") .resolveTemplate("cardId", "111122223333") .queryParam("pin", "9876") .request("application/json") .post(text("50.0"), Money.class);
    10. 10. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public10 Modularized Configuration
    11. 11. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public11 Modularized Configuration - Motivation client .register(JsonMessageBodyReader.class) .register(JsonMessageBodyWriter.class) .register(JsonpInterceptor.class) .property(“jsonp.callback.name”, “callback”) .property(“jsonp.callback.queryParam”, “true”) ... Client-side
    12. 12. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public12 Modularized Configuration - Motivation public class MyApp extends javax.ws.rs.core.Application { public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<…>(); ... classes.add(JsonMessageBodyReader.class); classes.add(JsonMessageBodyWriter.class); classes.add(JsonpInterceptor.class); ... return classes; } } Server-side
    13. 13. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public13 Modularized Configuration - Solution client .register(JsonMessageBodyReader.class) .register(JsonMessageBodyWriter.class) .register(JsonpInterceptor.class) .property(“jsonp.callback.name”, “callback”) .property(“jsonp.callback.queryParam”, “true”) ... JsonFeature jf = new JsonFeature().enableCallbackQueryParam(); client.register(jf); Client-side
    14. 14. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public14 Common Configuration - Solution public Set<Class<?>> getClasses() { ... classes.add(JsonMessageBodyReader.class); classes.add(JsonMessageBodyWriter.class); classes.add(JsonpInterceptor.class); ... } public Set<Class<?>> getClasses() { ... classes.add(JsonFeature.class); ... } Server-side
    15. 15. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public15 A Feature Example public void JsonFeature implements Feature { public boolean configure(FeatureContext context) { context.register(JsonMessageBodyReader.class) .register(JsonMessageBodyWriter.class) .register(JsonpInterceptor.class) .property(CALLBACK_NAME, calbackName) .property(USE_QUERY_PARAM, useQueryParam); return true; } }
    16. 16. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public16 Dynamic Feature public interface DynamicFeature { void configure(ResourceInfo ri, FeatureContext context); } public interface ResourceInfo { Method getResourceMethod(); Class<?> getResourceClass(); } Server-side only
    17. 17. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public17 Asynchronous Processing
    18. 18. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public18 Asynchronous Processing: Server-side @Stateless @Path("/async/longRunning") public class MyResource { @GET @Asynchronous public void longRunningOp(@Suspended AsyncResponse ar) { ar.setTimeoutHandler(new MyTimoutHandler()); ar.setTimeout(15, SECONDS); final String result = executeLongRunningOperation(); ar.resume(result); } }
    19. 19. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public19 Asynchronous Processing: Server-side public interface AsyncResponse { public void resume(Object/Throwable response); public void cancel(); public void cancel(int/Date retryAfter); public boolean isSuspended(); public boolean isCancelled(); public boolean isDone(); public void setTimeout(long time, TimeUnit unit); public void setTimeoutHandler(TimeoutHandler handler); public Collection<Class<?>> register(Class<?> callback); public Map<Class<?>, Collection<Class<?>>> register( Class<?> callback, Class<?>... callbacks); public Collection<Class<?>> register(Object callback); public Map<Class<?>, Collection<Class<?>>> register( Object callback, Object... callbacks); }
    20. 20. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public20 Asynchronous Processing: Client-side WebTarget target = client.target("http://.../balance”)… // Start async call and register callback Future<?> handle = target.request().async().get( new InvocationCallback<String>() { void complete(String balance) { … } void failed(InvocationException e) { … } }); // After waiting for too long… if (!handle.isDone()) handle.cancel(true);
    21. 21. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public21 Filters/Interceptors
    22. 22. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public22 Filters & Interceptors  Non-wrapping filter chain – Filters do not invoke next filter in the chain directly – managed by the JAX-RS runtime  Each filter decides to proceed or break the chain Filter each incoming/outgoing message  Request  Request – ContainerRequestFilter, ClientRequestFilter  Response  Response – ContainerResponseFilter, ClientResponseFilter  Server-side specialties – @PreMatching, DynamicFeature
    23. 23. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public23 Filters & Interceptors public class RequestLoggingFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) { log(requestContext); // non-wrapping => returns without invoking the next filter } ... } A Logging Filter Example
    24. 24. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public24 Filters & Interceptors  Invoked ONLY when/if entity processing occurs – Performance boost  Wrapping interceptor chain – Each interceptor invokes the next one in the chain via context.proceed() Intercept entity providers  MessageBodyReader interceptor – ReaderInterceptor interface  MessageBodyWriter interceptor – WriterInterceptor interface
    25. 25. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public25 Filters & Interceptors public class GzipInterceptor implements ReaderInterceptor { @Override Object aroundReadFrom(ReaderInterceptorContext ctx) { InputStream old = ctx.getInputStream(); ctx.setInputStream(new GZIPInputStream(old)); // wrapping => invokes the next interceptor Object entity = ctx.proceed(); ctx.setInputStream(old); return entity; } } A GZip Reader Interceptor Example
    26. 26. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public26 Application Filters & Interceptors Request Filter Filter NetworkTransport … … Response FilterFilter write(…) Writer Interceptor … MBW read(…) - optional … MBR Writer Interceptor Reader Interceptor Reader Interceptor
    27. 27. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public27 Response Application Filters & Interceptors Filter Filter Network … ResponseFilterFilter write(…) … MBW Writer Interceptor Writer Interceptor Filter Filter … RequestRequest read(…) - optional Reader Interceptor … MBR Reader Interceptor Filter Filter Resource Matching @PreMatching
    28. 28. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public28 Bindings & Priorities  Binding – Associating filters and interceptors with resource methods – Server-side concept  Priority – Declaring relative position in the execution chain – @Priority(int priority)  Shared concept by filters and interceptors Scoped Binding Global Binding Static @NameBinding Default @PreMatching Dynamic DynamicFeature N/A
    29. 29. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public29 Bindings @NameBinding @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(value = RetentionPolicy.RUNTIME) public @interface Logged {} @Provider @Logged @Priority(USER) public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter { … }
    30. 30. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public30 Bindings @Path("/greet/{name}") @Produces("text/plain") public class MyResourceClass { @Logged @GET public String hello(@PathParam("name") String name) { return "Hello " + name; } }
    31. 31. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public31 A DynamicFeature Example public void SecurityFeature implements DynamicFeature { public boolean configure(ResourceInfo ri, FeatureContext context) { String[] roles = getRolesAllowed(ri); if (roles != null) { context.register(new RolesAllowedFilter(roles)); } } ... } Server-side only
    32. 32. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public32 Hypermedia Support
    33. 33. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public33 Hypermedia Link: <http://.../orders/1/ship>; rel=ship, <http://.../orders/1/cancel>; rel=cancel ... <order id="1"> <customer>http://.../customers/11</customer> <address>http://.../customers/11/address/1</address> <items> <item> <product>http://.../products/111</product> <quantity>2</quantity> </item> <items> ... </order>
    34. 34. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public34 Hypermedia // Producer API (server-side) Link self = Link.fromMethod(MyResource.class, ”handleGet”) .build(); Link update = Link.fromMethod(MyResource.class, “handlePost”) .rel(”update”) .build(); ... Response res = Response.ok(order) .link("http://.../orders/1/ship", "ship") .links(self, update) .build();
    35. 35. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public35 Hypermedia Response order = client.target(…).request("application/xml").get(); // Consumer API (client-side) Link shipmentLink = order.getLink(“ship”); if (shipmentLink != null) { Response shipment = client.target(shipmentLink).post(null); … }
    36. 36. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public36 Content Negotiation
    37. 37. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public37 Server Side Content Negotiation GET http://.../widgets2 Accept: text/*; q=1 ... Path("widgets2") public class WidgetsResource2 { @GET @Produces("text/plain", "text/html") public Widgets getWidget() {...} }
    38. 38. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public38 Server Side Content Negotiation GET http://.../widgets2 Accept: text/*; q=1 ... Path("widgets2") public class WidgetsResource2 { @GET @Produces("text/plain; qs=0.5", "text/html; qs=0.75") public Widgets getWidget() {...} }
    39. 39. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public39 Try it Out! http://dlc.sun.com.edgesuite.net/glassfish/4.0.1/pro
    40. 40. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public40 JAX-RS 2 Implementations TomEE GlassFish
    41. 41. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public41 JAX-RS.next  CDI alignment  Server-Sent Events (SSE)  More Hypermedia (Structural Links)  Action-Oriented Web Framework?  Higher level client API?  Fixes and clarifications
    42. 42. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public42 Summary  Major improvements – Client API – Modularized Configuration – Asynchronous Processing – Filters/Interceptors – Hypermedia Support – Server-side Content Negotiation  Many minor API improvements – Bean Validation, Request / Response, URI builder, String Converters, @BeanParam, MultivaluedHashMap, GenericType, etc  JAX-RS.next spinning up now!
    43. 43. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public43 Learning More  Jersey User Guide – https://jersey.java.net/documentation/latest/user-guide.html  JAX-RS Transparent Expert Group – http://jax-rs-spec.java.net  Java EE 7 Reference Implementation – http://glassfish.org  JAX-RS Reference Implementation – https://jersey.java.net  The Aquarium – http://blogs.oracle.com/theaquarium
    44. 44. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Public44

    ×