dev@jsr311.java.net

Re: "Dual dispatch"

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 20 Apr 2007 18:33:26 +0200

Stefan Tilkov wrote:
> Taking an example from Paul:
>
> @ProduceMime("application/vnd.blinksale+xml")
> @HttpMethod
> Invoices getInvoices() {
> // ...
> }
>
> As Paul wrote, this assumes the "Invoices" JAXB bean has been created.
>
> Some questions:
> - Does this mean there can only be one XML "serialization" of "Invoices"?

One could do this:

     @ProduceMime("application/xml")
     @HttpMethod
     Invoices getInvoicesAsFeed() {
        ...
     }

     @ProduceMime("application/json")
     @HttpMethod
     Invoices getInvoicesAsJson() {
        ...
     }

     @ProduceMime("application/fastinfoset")
     @HttpMethod
     Invoices getInvoicesAsFI() {
        ...
     }

If JAXB marshalling supports the JSON or FastInfoset encoding then i
think this is possible.


> - How do I provide an alternative format, let's say JSON, YAML or HTML?

Do you mean using the same Invoices objects (like above) or different
objects? If the latter then that is already shown for returning an atom
feed.


> - Is there a way around the slightly strange "Invoices" JAXB bean, i.e.
> can I have an InvoicesResource and a class Invoice, but avoid "Invoices"?
>

I suppose in the simple case of:

   <invoices>
     <invoice>...</invoice>
     <invoice>...</invoice>
     <invoice>...</invoice>
   </invoices>

there could be a method:

  Collection<Invoice> getInvoices() {...}

where the runtime infers the root element of the collection. But when
there is further infoset as part of the collection then this would not
work (i.e. specified in XSD). Also i am not quite sure how this should
work for different encodings. Perhaps such a technique need only work
for XML infoset or JSON-based encodings?


> I believe we probably need some way for a "dual dispatch" style of
> consuming and producing a representation from an object, based on the
> Java type *and* the MIME content type - i.e. there may be more than one
> content type for a given Java type, and more than one Java type for a
> given content type. I don't believe the current proposal addresses this?
>

The streaming type provider [1] methods readFrom and writeTo have access
to the HTTP meta data so it is possible to support the reading/writing
using the same object but with different MIME types that represent
different encodings.

However i think the supports method also needs to include the mime type
with the Java type. So that it is possible for the developer to add new
support for existing types with new MIME types.

Paul.

> Stefan
>
> P.S.: This is not my original idea; credit is due to Spring Web Services
> creator Arjen Poutsma. Just for the record ;-)
>

OK :-)

Paul.

[1]
https://jsr311.dev.java.net/sketches/sketch2/javax/ws/rs/spi/TypeStreamingProvider.html

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109