users@jersey.java.net

Re: [Jersey] two different parameters for PUT method

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 09 Jun 2009 11:22:20 +0200

On Jun 8, 2009, at 7:06 PM, Christopher Schmidt wrote:

>
> How is the best way to provide a PUT method that supports two
> different XML parameters (using the same URL path)?
>

By XML parameters do you mean two different types of XML document sent
by the client as a request entity?

If so, Jersey has support for unmarshalling a type of Object if the
JAXBContext is set up correctly with JAXB types for the set of
possible XML documents.

You can define the JAXBContext using an instance of
ContextResolver<JAXBContext> annotated with @Provider.

public class MyProvider implements ContextResolver<JAXBContext> {
   public JAXBContext getContext(Class<?> type) {
     if (type == Object.class) {
       return ... // The JAXBContext for the objects
     } else {
       return null;
     }
   }
}

Then you can have a method:

  @PUT
  @Consumes("application/xml")
  public void put(Object o) {
    if (o instanceof X) {
    } else if (o instanceof Y) {
    }
  }



Or alternatively you can do this yourself:

  @PUT
  @Consumes("application/xml")
  public void put(InputStream in) {
     // Unmarshall a JAXB object from in
  }

which may be better if different resources have different XML content
with the same XML root elements.

Hope this helps,
Paul.

> This is because we want to "connect" a legacy system which is not
> fully REST capable.
>
> Thanks CS
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>