Click here to load reader

JAX-RS 2.0: RESTful Java on Steroids - Coding the · PDF file2011-11-17 · ... RESTful Java on Steroids What’s in JSR-339? Marek Potociar ... • JAX-RS 1.0 is Java API for RESTful

  • View
    214

  • Download
    0

Embed Size (px)

Text of JAX-RS 2.0: RESTful Java on Steroids - Coding the · PDF file2011-11-17 · ......

  • JAX-RS 2.0: RESTful Java on Steroids Whats in JSR-339?

    Marek Potociar JAX-RS 2.0 Specification Lead Oracle

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracles products remains at the sole discretion of Oracle.

  • Part I. How we got here

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    RESTFul Principles Assign everything an ID Link things together Use common set of methods Allow multiple representations Stateless communications

    Shortest REST Primer Ever

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    JAX-RS 1.0 is Java API for RESTful Services Current version: JAX-RS 1.1

    Goals POJO-Based Resource API Server-side

    HTTP Centric Entity Format Independence Container Independence Inclusion in Java EE But no hard dependency on Java EE though

    JAX-RS Intro

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @Path(atm/{cardId}")public class AtmResource {

    @GET @Path(balance") @Produces("text/plain") public String balance(@PathParam("cardId") String card, @QueryParam("pin") String pin) { return Double.toString(getBalance(card, pin)); }

    JAX-RS API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @Path(atm/{cardId}")public class AtmResource {

    @GET @Path(balance") @Produces("text/plain") public String balance(@PathParam("cardId") String card, @QueryParam("pin") String pin) { return Double.toString(getBalance(card, pin)); }

    JAX-RS API

    Resource path templates

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @Path(atm/{cardId}")public class AtmResource {

    @GET @Path(balance") @Produces("text/plain") public String balance(@PathParam("cardId") String card, @QueryParam("pin") String pin) { return Double.toString(getBalance(card, pin)); }

    JAX-RS API

    Resource path templates

    URI parameter injection

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @Path(atm/{cardId}")public class AtmResource {

    @GET @Path(balance") @Produces("text/plain") public String balance(@PathParam("cardId") String card, @QueryParam("pin") String pin) { return Double.toString(getBalance(card, pin)); }

    JAX-RS API

    Resource path templates

    URI parameter injection

    HTTP method binding

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @Path(atm/{cardId}")public class AtmResource {

    @GET @Path(balance") @Produces("text/plain") public String balance(@PathParam("cardId") String card, @QueryParam("pin") String pin) { return Double.toString(getBalance(card, pin)); }

    JAX-RS API

    Resource path templates

    Built-in Serialization

    URI parameter injection

    HTTP method binding

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @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); }}

    JAX-RS API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @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); }}

    JAX-RS API

    Custom Serialization

    Built-in Serialization

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    JAX-RS Most Wanted

    Filters Handlers

    Validation

    Asynchronous Processing

    Hypermedia

    Server Side Conneg

    JSR 330

    MVC Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    EG Formed in February 2011 Oracle Leads Marek Potociar / Santiago Pericas-Geertsen

    Expert Group: Jan Algermissen, Florent Benoit (OW2), Sergey Beryozkin

    (Talend), Adam Bien, Bill Burke (RedHat), Clinton Combs, Bill De Hora, Markus Karg, Sastri Malladi (Ebay), Julian Reschke, Guilherme Silveira, Dionysios Synodinos

    Early Draft published on Oct 21st, 2011

    JSR 339: JAX-RS 2.0

  • Part II. Where we are going

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    New in JAX-RS 2.0

    Filters Handlers

    Validation

    Asynchronous Processing

    Hypermedia

    Server Side Conneg

    JSR 330

    MVC Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Client API

    Filters Handlers

    Validation

    Asynchronous Processing

    Hypermedia

    Server Side Conneg

    JSR 330

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    HTTP client libraries too low level Sharing features with JAX-RS server API

    E.g., MBRs and MBWs Supported by some JAX-RS 1.x implementations

    Need for a standard

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Client API

    Client Resource Target atm

    Request Builder

    Resource Target {cardId}

    Resource Target balance

    Resource Target withdrawal

    Client Factory

    Response

    Invocation

    Configuration Configuration Configuration

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    // Get instance of ClientClient client = ClientFactory.newClient();

    // Get account balanceString bal = client.target("http://.../atm/{cardId}/balance") .pathParam("cardId", "111122223333") .queryParam("pin", "9876") .request("text/plain").get(String.class);

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    // Withdraw some moneyMoney mon = client.target("http://.../atm/{cardId}/withdrawal") .pathParam("cardId", "111122223333") .queryParam("pin", "9876") .request("application/json") .post(text("50.0"), Money.class);

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Invocation inv1 = client.target("http://.../atm/{cardId}/balance") .request(text/plain).buildGet();

    Invocation inv2 = client.target("http://.../atm/{cardId}/withdraw") .request("application/json") .buildPost(text("50.0"));

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Collection invocations = Arrays.asList(inv1, inv2);

    Collection responses = Collections.transform( invocations, new F() { public Response apply(Invocation inv) { return inv.invoke(); } });

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    // Create client and register MyProvider1Client client = ClientFactory.newClient();client.configuration().register(MyProvider1.class);

    // Create atm target; inherits MyProvider1Target atm = client.target("http://.../atm");// Register MyProvider2atm.configuration().register(MyProvider2.class);

    // Create balance target; inherits MyProvider1, MyProvider2Target balance = atm.path({cardId}/balance");// Register MyProvider3balance.configuration().register(MyProvider3.class);

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Filters & Handlers

    Filters Handlers

    Validation

    Asynchronous Processing

    Hypermedia

    Server Side Conneg

    JSR 330

    Client API

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Customize JAX-RS request/response processing Use Cases: Logging, Compression, Security, Etc.

    Shared by client and server APIs Replace existing proprietary support

    Provided by most JAX-RS 1.x implementations All using slightly different types or semantics

    Filters & Handlers

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    Non-wrapping filter chain Managed by the JAX-RS runtime Each filter decides to proceed or break the chain By returning FilterAction.NEXT or FilterAction.STOP

    Request Request RequestFilter interface Server-side specialty: PreMatchRequestFilter

    Response Response ResponseFilter interface

    Filters & Handlers

  • Copyright 2011, Oracle and/or its affiliates. All rights reserved.

    @Providerclass LoggingFilter implements RequestFilter, ResponseFilter {

    // RequestFilter implementation @Override public FilterAction preFilter(FilterContext ctx) throws IOException { logRequest(ctx.getRequest()); return FilterAction.NEXT;

Search related